elasticsearch1.x,2.x,5.x with the iteration of the version, in addition to the system upgrade, the Java API has also made a relatively large adjustment, that is, the 1.X API in 2.X and 5.X or even the future 6.X version is not common.
The version used in this example is 5.6.5
I. Add MAVEN dependencies First
<Dependency> <groupId>Org.elasticsearch.client</groupId> <Artifactid>Transport</Artifactid> <version>5.6.5</version> </Dependency>
Two. Connection mode
Settings Settings = Settings.builder (). Put ("Cluster.name", "my-application"). Build (); // set the cluster name New Prebuilttransportclient (Settings). addtransportaddress (new inetsockettransportaddress ( Inetaddress.getbyname ("10.10.10.5"), 9300);
The output client comes with a cluster sniffing feature that dynamically adds a new host and removes the old host, and when this feature is enabled, the transport client connects to the node in its internal node list
Sniffer open mode:
Settings Settings = Settings.builder (). Put ("Client.transport.sniff", true). Build ();
Transportclient client = new prebuilttransportclient (settings);
Other transport client settings include:
Client.transport.ignore_cluster_name: Set to True to ignore the checksum of the cluster name when the node is connected
Client.transport.ping_timeout: Set the wait time for a node to get response, default 5 Seconds
Client.transport.nodes_sampler_interval: How long does it take to get/ping list of nodes and connect, default 5 seconds
Three. View cluster information
list<discoverynode> nodes = client.connectednodes (); for (Discoverynode node:nodes) { System.out.println (node.gethostaddress ()); System.out.println (Node.getname ()); System.out.println (Node.getversion ()); }
Elasticsearch Java API Configuration test