ElasticSearch6.X version Java Api Chinese detailed explanation (1) TransportClient client connection method

 TransportClient uses the transport module to remotely connect to an elasticsearch cluster. It does not join the cluster, but only gets one or more initial transport ip addresses and polls with them on each action (though most operations are probably "two hop"  operations).

// on startup

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));

// on shutdown

client.close();

Note that if you use a different name than "elasticsearch", you must reset the cluster name.

Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);
//Add transport addresses and do something with the client...

TransportClient comes with a cluster sniffing feature that allows it to dynamically add new hosts and remove old ones. When sniffing is enabled, TransportClient will connect to nodes in its internal node list, which is built by calling addTransportAddress. After this, TransportClient will call the internal cluster status API on these nodes to discover available data nodes. The client's internal node list will be replaced with only these data nodes. By default, this list is refreshed every 5 seconds. Note that the IP addresses to which the sniffer connects are declared as published addresses in the search configuration of these nodes.

Keep in mind that if the node is not a data node, the list may not include the original node it is connected to. For example, if you initially connect to a master node, after sniffing, no further requests will go to the master node, but to any data nodes. The reason TransportClient does not include non-data nodes is to avoid sending search traffic to master-only nodes.

To enable sniffing, set client.transport.sniffto true:


Settings settings = Settings.builder()
        .put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);

Other transport client level settings include:

client.transport.ignore_cluster_name

Set to true to ignore cluster name validation of connected nodes.

 (since 0.19.4)

client.transport.ping_timeout

The time to wait for a ping response from a node. Defaults to 5s.

client.transport.nodes_sampler_interval The time to wait for a ping response from a node. Defaults to 5s.

Attach the connection code I wrote in the past two days:

public class SearchEsUtils implements SearchEsUtilsInterface {

	TransportClient transportClient;
	// index library name
	String index = "ori_epotential_2018";
	//type name
	String type = "ori_epotential";
	//cluster name
	String es_cluster = "es_cluster";
	//Cluster connection IP address
	String es_ip = "192.168.0.14";
	//Cluster connection port number
	Integer es_port = 9300;
	
	
	
	public SearchEsUtils() {
		try {
			init();
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}



	/**
	 * @throws IOException
	 * Initialize ES client connection
	 */
	public void init() throws IOException{
		/**
         * 1: Specify the cluster configuration information through the setting object
         */  
        Settings setting = Settings.builder()
            .put("cluster.name", es_cluster)//指定集群名称  
            .put("client.transport.sniff", true)//启动嗅探功能  
            .build();  
        
        /** 
         * 2:创建客户端 
         * 通过setting来创建,若不指定则默认链接的集群名为elasticsearch 
         * 链接使用TCP协议即9300 
         */  
        
        transportClient = new PreBuiltTransportClient(setting)
        		.addTransportAddress(new TransportAddress(InetAddress.getByName(es_ip), es_port));

        /** 
         * 3:查看集群信息 
         * 注意我的集群结构是: 
         *   131的elasticsearch.yml中指定为主节点不能存储数据, 
         *   128的elasticsearch.yml中指定不为主节点只能存储数据。 
         * 所有控制台只打印了192.168.79.128,只能获取数据节点 
         *  
         */  
        List<DiscoveryNode> connectedNodes = transportClient.connectedNodes();  
        for(DiscoveryNode node : connectedNodes)  
        {  
            System.out.println(node.getHostAddress());  
        }  
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565587&siteId=291194637