记第一次java连elasticsearch出现的问题

 
 

public class Elasticseach {
TransportClient client = null;
public Elasticseach(){
//根据官方文档得知,如果集群名称改变,则在设置中设成配置文件里上设置的名字
Settings settings = Settings.builder()
.put("cluster.name", "cluemining_es_cluester").build();
//在linux 中elasticsearch/bin/config/elasticseach.yml 中设置集群名称

try {

//我用的是5.5.2版本,该版本与最新的6系列连接方式不同
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.0.0.42"), 9900))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.0.0.42"), 9901))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.0.0.42"), 9902));
SearchResponse response = client.prepareSearch().get();
System.out.println(response);

} catch (UnknownHostException e) {
e.printStackTrace();
}finally {
client.close();
}
}
public TransportClient getConnection(){
if (client == null) {
synchronized (Elasticseach.class) {
if (client == null) {
new Elasticseach();
}
}
}
return client;
}
}

 

注:mavan导elasticsearch时可能会出现依赖冲突,既org.elasticsearch.client
包中的netty4可能因版本问题出现java.lang.AbstractMethodError: org.elasticsearch.transport.TcpTransport.sendMessage问题
解决方法:将netty版本改成elasticsearch跟包一致,如下所示
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
<exclusions>
<exclusion>
<artifactId>transport-netty4-client</artifactId>
<groupId>org.elasticsearch.plugin</groupId>
</exclusion>
<exclusion>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>5.5.2</version>
</dependency>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值