1. 程式人生 > >記第一次java連elasticsearch出現的問題

記第一次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 >