2017-07-07 35 views
1

当我使用Elasticsearch 1.7.2版本时,我的java代码与tcp连接,并且效果很好,但是当我使用与Elasticsearch 5.4-3相同的代码时,它没有显示ImmutableSettings的定义。如何在java中将弹性搜索5.4连接到tcp?

Client client = null; 
    try { 
     Settings settings = ImmutableSettings.settingsBuilder() 
       .put("client.transport.ignore_cluster_name", true) 
       .put("client.transport.sniff", false) 
       .build(); 
    System.out.print("true"); 
     client = new TransportClient(settings) 
     .addTransportAddress(new InetSocketTransportAddress("10.196.2.215", 9300)); 
} 

也有在Elasticsearch没有TCP设置5.4-3 /配置/ elasticserch.yml文件

回答

2

还有,你在问题中指定的版本之间的重大变化。如果您想在5.4.3版本中成功创建连接,请参阅以下代码片段。

import org.elasticsearch.action.get.GetResponse; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
import org.elasticsearch.transport.client.PreBuiltTransportClient; 

import java.net.InetAddress; 
import java.net.UnknownHostException; 

public class ElasticSearchClientTest { 
    public void clientConnectionTest() throws UnknownHostException { 

     // Use any settings here (As you mentioned in the code) 
     Settings settings = Settings.builder() 
       .put("cluster.name", "elasticsearch") 
       .put("client.transport.sniff", true) 
       .put("sniffOnConnectionFault", true).build(); 

     TransportClient client = new PreBuiltTransportClient(settings); 

     // Change the ip address or the host name accordingly. 
     client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); 
    } 
} 

在pom.xml文件中使用以下依赖关系。

<dependency> 
    <groupId>org.elasticsearch</groupId> 
    <artifactId>elasticsearch</artifactId> 
    <version>5.4.3</version> 
    <scope>compile</scope> 
</dependency> 
<dependency> 
    <groupId>org.elasticsearch.client</groupId> 
    <artifactId>transport</artifactId> 
    <version>5.4.3</version> 
</dependency> 
+0

感谢@Hiran的评论...我已经试过,但它gaves我下面主要‘因为java.lang.UnsupportedClassVersionError例外“在线程异常’:组织/ elasticsearch /客户/运输/ TransportClient:不支持的主要.minor version 52.0“ – user2778724

+0

@ user2778724 - 这应该是maven错误,请从pom.xml文件中删除所有其他elasticsearch依赖项并重试。 –

+0

@ user2778724 - 只保留我在答案中提到的elasticsearch依赖关系,删除所有其他弹性搜索相关的依赖关系。 –