如何使用Elasticsearch Node Client Java连接到远程服务器

时间:2014-12-01 18:53:21

标签: java elasticsearch server client elasticsearch-java-api

我正在尝试使用Elasticsearch Java NodeBuilder Client连接到我的服务器。但是我没有看到任何指定我的服务器地址和端口的选项(就像我们可以在Transport Client中使用addNewTransportAddress("serveraddress", port))那样。。如何让Node Client连接到我的服务器?代码如下所示,我在哪里提到要连接的服务器地址?

//On Startup
Node node = nodeBuilder()
        .clusterName("elasticsearch")
        .data(false) //No shards allocated; or can set client to true
        .client(true) //No shards allocated; or can set data to false
        .node();

//Node Client
Client client = node.client();

//Get API       
GetResponse response = client.prepareGet("indexname", "type", "id")
        .execute()
        .actionGet();

System.out.println("----------------Index Output Begin----------------");
System.out.println("Index Name: " + response.getIndex());
System.out.println("Type: " + response.getType());
System.out.println("Document ID: " + response.getId());
System.out.println("Document Version: " + response.getVersion());
System.out.println("Source: " + response.getSourceAsString());

2 个答案:

答案 0 :(得分:8)

节点客户端基于多播。 客户端和节点之间的网络必须位于具有多播启用的网络中。然后客户将发现"节点基于群集名称。

如果您需要连接到远程服务器(通过指定IP地址),则必须使用transport client

Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "myClusterName").build();
Client client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));

答案 1 :(得分:0)

当我使用ImmutableSettings时,我得到了"ImmutableSettings cannot be resolved"

我的代码是:

Node node =nodeBuilder()
           .settings(ImmutableSettings.settingsBuilder().put("path.home",   "/home/amit/elasticsearch-2.1.0/bin"))
            .client(true)
            .node();
        Client client = node.client();