8

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

//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());
4

2 回答 2

8

节点客户端基于多播。您的客户端和节点之间的网络必须位于启用多播的网络中。然后客户端将根据集群名称“发现”节点。

如果您需要连接到远程服务器(通过指定 IP 地址),您必须使用传输客户端

Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "myClusterName").build();
Client client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));
于 2014-12-01T19:30:18.393 回答
0

"ImmutableSettings cannot be resolved"在使用 ImmutableSettings 时得到了。

我的代码是:

Node node =nodeBuilder()
           .settings(ImmutableSettings.settingsBuilder().put("path.home",   "/home/amit/elasticsearch-2.1.0/bin"))
            .client(true)
            .node();
        Client client = node.client(); 
于 2016-02-05T07:13:49.223 回答