ElasticSearch Java API连接报错NoNodeAvailableException解决方案
如下代码:
public static void creatIndex(){
// 1、获取客户端对象,设置连接的集群名称
Settings settings= Settings.builder().put("cluster.name","elasticsearch").build();
client=new PreBuiltTransportClient(settings);
// 2、连接集群
try {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop-001"), 9300));
System.out.println(client.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
//1.创建索引(indices指数)
client.admin().indices().prepareCreate("blog").get();
//2.关闭连接
client.close();
}
运行报错:
NoNodeAvailableException
查看日志如下:
java.lang.IllegalStateException: Received message from unsupported version: [5.2.2] minimal compatible version is: [5.6.0]
可知是版本太低,至少要5.6.0以上版本才行
修改后的pom.xml文件如下:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
再次运行,成功!!!!!!
其他原因:
1.查看
cluster.name,是否是你服务器上在elasticsearch.yml上配置的
2.查看
port:9300
val client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("******"), 9300))
3.查看
服务器的防火墙,是否关了。
4.查看
如果设置client.transport.sniff为true,则表示客户端去嗅探整个cluster的状态,把集群中其它机器的ip地址加到客户端中