文章目录
- **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}]]
- 案例二、由于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}]]
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}