繁体   English   中英

如何在Java中将Elastic Search 5.4连接到TCP?

[英]How to connect Elastic search 5.4 to tcp in java?

这是我在使用Elasticsearch版本1.7.2时与tcp连接的Java代码,它可以正常工作,但是当我在Elasticsearch 5.4-3中使用相同的代码时,它没有显示ImmutableSettings的定义。

  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 5.4-3 / config / elasticserch.yml文件中也没有tcp设置

您在问题中指定的版本之间有重大变化。 如果要在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM