Java连接elasticsearch集群 NoNodeAvailableException 解决方法
来源:互联网 发布:淘宝客服怎么发视频 编辑:程序博客网 时间:2024/06/07 03:26
以下是java连接elasticsearch集群,新增一个index的客户端代码
package com.yyfq.report.test;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.HashMap;import java.util.Map;import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; public class ElasticsearchTest { public static void main(String[] argv) throws UnknownHostException{
//客户端连接elasticsearch代码 Map<String, String> map = new HashMap(); map.put("cluster.name", "elasticsearch"); Settings.Builder settings = Settings.builder().put(map).put("client.transport.ignore_cluster_name", true); Client client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("127.0.0.1"), Integer.parseInt("9200")));
// 新增一个index IndexResponse response = client.prepareIndex("twitter", "tweet") .setId("1") .setSource("{'counter':'2','tags':'Add one more!''}") .execute() .actionGet(); System.out.println("response.version():"+response.getVersion()); //关闭客户端 client.close(); } }执行以上代码时会抛出一个错误:Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available......
解决方法:
1. 端口号不能与HTTP设置的端口号一致!!!!
2. 集权名称如果与elasticsearch.yml 中定义的一致,或者默认的elasticsearh, setting必须为Setting.Empty
下面是改正过的连接方案。
public class ElasticsearchTest { public static void main(String[] argv) throws NumberFormatException, UnknownHostException{ /* //设置新集群名称 Settings settings = Settings.builder() .put("cluster.name", "newCluster") .put("node.name","newNode").build();*/ //创建client TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //搜索数据 GetResponse response = client.prepareGet("twitter", "tweet", "1").execute().actionGet(); //输出结果 System.out.println(response.getSourceAsString()); //关闭client client.close(); } }
3. 如果不是端口号和集群名称的问题,请检查你的jar包版本是否对应elasticsearch版本
我用的elasticsearch 5.1.1, 对应的Maven jar包依赖
希望这篇文章能帮到你!
2 0
- Java连接elasticsearch集群 NoNodeAvailableException 解决方法
- java连接ElasticSearch集群
- ElasticSearch NoNodeAvailableException
- Elasticsearch NoNodeAvailableException 问题解决
- ElasticSearch Java API(一)连接集群
- ElasticSearch NoNodeAvailableException 基于5.x
- org.elasticsearch.client.transport.NoNodeAvailableException
- 异常org.elasticsearch.client.transport.NoNodeAvailableException
- org.elasticsearch.client.transport.NoNodeAvailableException。。解决办法
- 查看那些连接elasticsearch集群
- Java连接elasticsearch
- elasticsearch 搭建-集群-java应用
- org.elasticsearch.client.transport.NoNodeAvailableException: No node available
- ElasticSearch: NoNodeAvailableException[None of the configured nodes are available...]
- Elasticsearch NoNodeAvailableException None of the configured nodes are available
- Elasticsearch NoNodeAvailableException[None of the configured nodes are available
- Elasticsearch Java Client连接池
- elasticsearch for java 认证连接
- Java面试_非技术问答
- 最简单侧滑实现
- nginx根据客户端ip动态代理后端服务-TCP
- APP瘦身之webp实践
- spring bean 根据包路径扫描对象加载过程
- Java连接elasticsearch集群 NoNodeAvailableException 解决方法
- javax.el.ELException: Cannot convert 2015-12-03 18:50 of type class java.lang.String to class java.u
- centos 7 更换yum源
- MySQL与MariaDB基于GTID的主从同步
- centos不同服务器间移动文件
- 数字签名与数字证书
- html01序章,页面是个什么鬼?
- i++的理解
- CentOS7通过apache搭建yum仓库