TransportClient
doesn’t join the cluster; it connects remotely to an Elasticsearch cluster
using the transport module. It communicates to the nodes in the cluster using
Round Robin fashion.
Following
snippet is used to initialize TransportClient.
TransportClient client = TransportClient.builder().build(); client.addTransportAddress(new InetSocketTransportAddress("host1", 9300)).addTransportAddress(new InetSocketTransportAddress("host2", 9300));
If
clustername is different than "elasticsearch", you have to set the
clustername externally.
The client
allows to sniff the rest of the cluster, and add those into its list of
machines to use. In this case, note that the IP addresses used will be the ones
that the other nodes were started with. In order to enable it, set the
client.transport.sniff to true.
TransportClientUtil.java
Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build(); TransportClient client = TransportClient.builder().settings(settings).build();
Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).put("client.transport.sniff", true).build(); TransportClient client = TransportClient.builder().settings(settings).build();
Other setting parameters used are:
Parameter
|
Description
|
client.transport.ignore_cluster_name
|
Set to
true to ignore cluster name validation of connected nodes.
|
client.transport.ping_timeout
|
The time
to wait for a ping response from a node. Defaults to 5s.
|
client.transport.nodes_sampler_interval
|
How often
to sample / ping the nodes listed and connected. Defaults to 5s.
|
package com.self_learn.util; import static com.self_learn.util.IPUtil.isValidHosts; import static com.self_learn.util.IPUtil.isValidPorts; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import com.google.common.base.Preconditions; public class TransportClientUtil { private static Map<Map<String, Integer>, Client> localMap = new HashMap<>(); /** * Take machine name and port addresses as map and return transport client. * Key is host name, value is port number * * @throws UnknownHostException */ public static Client getTransportClient(String clusterName, Map<String, Integer> map) throws UnknownHostException { Preconditions.checkNotNull(clusterName, "clusterName shouldn't be empty"); Preconditions.checkNotNull(map, "Map shouldn't be empty"); if (localMap.containsKey(map)) return localMap.get(map); Preconditions.checkState(isValidHostPorts(map), "Map contains invalid host (or) port"); Settings settings = ImmutableSettings.settingsBuilder() .put("cluster.name", clusterName) .put("client.transport.sniff", true).build(); TransportClient client = new TransportClient(settings); InetSocketTransportAddress addresses[] = getInetSocketTransportAddresses(map); client.addTransportAddresses(addresses); localMap.put(map, client); return client; } private static boolean isValidHostPorts(Map<String, Integer> map) { Set<String> hostNames = map.keySet(); Set<Integer> ports = new HashSet<>(map.values()); if (!isValidHosts(hostNames.toArray(new String[hostNames.size()]))) return false; if (!isValidPorts(ports.toArray(new Integer[ports.size()]))) return false; return true; } private static InetSocketTransportAddress[] getInetSocketTransportAddresses( Map<String, Integer> map) throws UnknownHostException { InetSocketTransportAddress addresses[] = new InetSocketTransportAddress[map .size()]; int count = 0; Set<String> keys = map.keySet(); for (String key : keys) { InetAddress addr = InetAddress.getByName(key); InetSocketTransportAddress address = new InetSocketTransportAddress( addr, map.get(key)); addresses[count] = address; } return addresses; } public static Client getLocalTransportClient(String clusterName) throws UnknownHostException { Settings settings = ImmutableSettings.settingsBuilder() .put("cluster.name", clusterName) .put("client.transport.sniff", true).build(); TransportClient client = new TransportClient(settings); InetAddress addr = InetAddress.getByName("127.0.0.1"); InetSocketTransportAddress address = new InetSocketTransportAddress( addr, 9300); client.addTransportAddress(address); return client; } }
You can
create transport client to a local node like below.
Map<String,
Integer> map = new HashMap<>();
map.put("127.0.0.1",
9300);
Client
client = TransportClientUtil.getTransportClient(clusterName, map);
(OR)
/* Get
client instance for cluster */
Client client
= TransportClientUtil.getLocalTransportClient(clusterName, 9300);
Both the
snippets create Transport client for a local node.
No comments:
Post a Comment