None of the configured nodes are available

Stella981
• 阅读 698
  1. 今天写了一段ES的测试代码,如下:

    package elasticSearch;

    import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.Map;

    import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits;

    import entity.Person; import util.JsonUtils;

    public class ESTest {

    private Client client;
    
    public ESTest() {
        this("localhost");
    }
    
    public ESTest(String ip) {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "huiTest")
                .build();
        client = TransportClient.builder().settings(settings).build();
        client = ((TransportClient) client).addTransportAddress(
                new InetSocketTransportAddress(new InetSocketAddress(ip, 9300)));
    
    }
    
    public IndexResponse createIndex(String indexName, String type,
            List<String> jsonArray) {
    
        IndexRequestBuilder builder = client.prepareIndex(indexName, type);
        for (String jsonTemp : jsonArray) {
            System.out.println(jsonTemp);
            builder = builder.setSource(jsonTemp);
        }
        IndexResponse response = builder.execute().actionGet();
        return response;
    
    }
    
    public List<Person> search(QueryBuilder queryBuilder, String indexName, String type) {
        List<Person> persons = new ArrayList<>();
    
        SearchResponse sResponse = client.prepareSearch(indexName).setTypes(type)
                .setQuery(queryBuilder).execute().actionGet();
        SearchHits hits = sResponse.getHits();
    
        System.out.println(hits.getTotalHits());
        Double salary = 0.0;
        SearchHit[] hitArray = hits.hits();
        for (SearchHit hit : hitArray) {
            Map<String, Object> map = hit.getSource();
    
            Integer age = (Integer) map.get("age");
            String name = (String) map.get("name");
            Object salaryO = map.get("salary");
            if (salaryO instanceof Integer) {
                salary = (Integer) salaryO * 1.0;
            } else {
                salary = (Double) salaryO;
            }
            Person p = new Person(name, age, salary);
    
            persons.add(p);
        }
        return persons;
    }
    
    public static void main(String[] args) {
        ESTest esTest = new ESTest();
    
        List<Person> persons = JsonUtils.getData(10);
        List<String> jsonArray = JsonUtils.covertList2String(persons);
    
        String indexName = "liuhui'sindex";// 要小写
        String type = "liuhui'sType";
         esTest.createIndex(indexName, type, jsonArray);
    
        // QueryBuilder builder = QueryBuilders.matchAllQuery();
        // QueryBuilder builder = QueryBuilders.termQuery("name", "hui10");
        QueryBuilder builder = QueryBuilders.termQuery("age", "20");
    
        List<Person> list = esTest.search(builder, indexName, type);
        System.out.println(list);
    
    }
    

    }

运行时报错如下:

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300}]]
     at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
     at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
     at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
     at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:288)
     at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:359)
     at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:86)
     at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:56)
     at elasticSearch.ESTest.search( ESTest.java:60)
     at elasticSearch.ESTest.main( ESTest.java:99)

造成这一异常的原因,仅我所知的,有以下几个:

  1. 运行该客户端时 elasticsearch.bat未启动;

  2. cluster.name:huiTest未在ES的安装目录config下的elasticsearch.yml中配置,即如果代码里使用了put("cluster.name","huiTest"),则一定要在elasticsearch.yml中添加一行配置:cluster.name:huiTest,因为cluster.name默认是elasticsearch。当然也可以不使用put("cluster.name","huiTest"),或者使用put("cluster.name","elasticsearch");这里要注意如果修改了elasticsearch.yml文件,一定要重启elasticsearch.bat;

  3. 客户端端口不是用的9300,而是9200.9200是为http协议准备的,9300才是客户端使用的;

    以上3条逐个排查,多半问题就解决了;

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java多线程读取、操作List集合
import java.util.ArrayList;import java.util.List;import org.apache.commons.lang3.ArrayUtils;public class Test_4 {/ 多线程处理list
Wesley13 Wesley13
2年前
java 帮助类
package com.quincy.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.
Stella981 Stella981
2年前
POI导入大excel文件
package me.shanzhi.test;import java.io.InputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apac
Wesley13 Wesley13
2年前
Java公钥私钥签名工具包
package com.locator.encryption;import java.io.ByteArrayOutputStream;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPa
Wesley13 Wesley13
2年前
Excel导出工具类.
 Excel导出工具类.POIimport java.io.OutputStream;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.uti
Stella981 Stella981
2年前
Base64工具类
package com.locator.encryption;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStr
Wesley13 Wesley13
2年前
Java容器——UnsupportedOperationException
    先看一个例子:import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.List;pub
Stella981 Stella981
2年前
Gallery实现流畅的新闻滚动 方法复写
package com.ename.views;import android.content.Context;import android.util.AttributeSet;import android.view.KeyEvent;import android.view.MotionEvent;
Stella981 Stella981
2年前
Android GridView 添加 HeadView
package com.example.test;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;i
Stella981 Stella981
2年前
Android从相机或相册获取图片裁剪
package com.only.android.app; import java.io.File; import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterfa