Elasticsearch常见报错和处理方法

java客户端连接elasticsearch报错:Exception in thread “main” NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{weOTC4XgTLe1d-_ApCpjmg}{localhost}{127.0.0.1:8200}]]

案例一、由于java客户端配置es端口不正确,导致报Exception in thread “main” NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{weOTC4XgTLe1d-_ApCpjmg}{localhost}{127.0.0.1:8200}]]

详细报错信息:

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{weOTC4XgTLe1d-_ApCpjmg}{localhost}{127.0.0.1:8200}]]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:352)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:248)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:57)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:394)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:396)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:385)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:45)
	at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:52)
	at com.troll.bigdata.component.example.elasticsearch.example.ConnectES.main(ConnectES.java:58)

报错代码详细:

import com.google.gson.Gson;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ConnectES {
    public static void main(String[] args) {

        /**
         * 官方参考链接
         * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
         */
        // 读取ES配置
        String host = "localhost";
        int port = 8200;  //  *说明:* 虚拟机端口映射为 9200->8200,9300->8300

        // 打印es连接信息
        System.out.println("host:" + host + ",port:" + port);

//        // 获取settings
        Settings settings = Settings.builder()
                .put("client.transport.sniff", false)
                .put("cluster.name", "troll_es_dev").build();

        // 客户端对象
        TransportClient client = null;

        // 建立ES连接
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        // json拼装
        String json = "{" +
                "\"uid\":1,"+
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";

        // 执行api
        IndexResponse response = client.prepareIndex("twitter", "_doc","1")
                .setSource(json, XContentType.JSON)
                .get();

        // 提取返回值,放入map,便于查看
        Map<String,Object> rep = new HashMap<String, Object>();
        // Index name
        rep.put("_index",response.getIndex());
        // Type name
        rep.put("_type",response.getType());
        // Document ID (generated or not)
        rep.put("_id",response.getId());
        // Version
        rep.put("_version",response.getVersion());
        // status has stored current instance statement.
        rep.put("status",response.status().getStatus());

        // 打印返回值,转json是为了方便查看
        Gson gson = new Gson();
        System.out.println(gson.toJson(rep));

        // 释放客户端
        client.close();

    }
}

分析问题,需要注意端口号为9300对应的端口,而非9200对应的端口;关于elasticsearch 9200端口和9300端口的区别为:

9200作为Http协议,主要用于外部通讯
9300作为Tcp协议,jar之间就是通过tcp协议通讯
ES集群之间是通过9300进行通讯

这里将8200改为8300即可。

端口号修改后输出为:

{"_index":“twitter-new”,"_type":"_doc","_id":“1”,"_version":11,“status”:200}

案例二、由于elasticsearch java客户端settings设置不正确导致报Exception in thread “main” NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{weOTC4XgTLe1d-_ApCpjmg}{localhost}{127.0.0.1:8200}]]

报错信息:

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{mgnUEG8FSQGqiYp6XPo4hA}{localhost}{127.0.0.1:8300}]]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:352)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:248)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:57)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:394)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:396)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:385)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:45)
	at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:52)
	at com.troll.bigdata.component.example.elasticsearch.example.ConnectES.main(ConnectES.java:59)

报错代码如下:

import com.google.gson.Gson;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ConnectES {
    public static void main(String[] args) {

        /**
         * 官方参考链接
         * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
         */
        // 读取ES配置
        String host = "localhost";
        int port = 8300;

        // 打印es连接信息
        System.out.println("host:" + host + ",port:" + port);

        // 客户端对象
        TransportClient client = null;

        // 建立ES连接
        try {
            client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        // json拼装
        String json = "{" +
                "\"uid\":1,"+
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";

        // 执行api
        IndexResponse response = client.prepareIndex("twitter", "_doc","1")
                .setSource(json, XContentType.JSON)
                .get();

        // 提取返回值,放入map,便于查看
        Map<String,Object> rep = new HashMap<String, Object>();
        // Index name
        rep.put("_index",response.getIndex());
        // Type name
        rep.put("_type",response.getType());
        // Document ID (generated or not)
        rep.put("_id",response.getId());
        // Version
        rep.put("_version",response.getVersion());
        // status has stored current instance statement.
        rep.put("status",response.status().getStatus());

        // 打印返回值,转json是为了方便查看
        Gson gson = new Gson();
        System.out.println(gson.toJson(rep));

        // 释放客户端
        client.close();

    }
}

此处由于elasticsearch java客户端为设置settings导致,将一下代码进行修改即可:

        // 客户端对象
       TransportClient client = null;

       // 建立ES连接
       try {
           client = new PreBuiltTransportClient(Settings.EMPTY)
                   .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }

改为:

       // 获取settings
       Settings settings = Settings.builder()
               .put("client.transport.sniff", false)
               .put("cluster.name", "troll_es_dev").build();

       // 客户端对象
       TransportClient client = null;

       // 建立ES连接
       try {
           client = new PreBuiltTransportClient(settings)
                   .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }

处理后,问题解决:

{"_index":“twitter-new”,"_type":"_doc","_id":“1”,"_version":11,“status”:200}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时空琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值