Elasticsearch 建立普通TransportClient 连接

package com.kuaifa.web.utils;

import java.net.InetAddress;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;


public class esClientBase {

   public static Client client = null;

   static {

      try {
         client = getConnection();
      } catch (Exception e) {
         throw new RuntimeException("ES连接错误,请核对代码及配置!", e);
      }
   }

   @SuppressWarnings({"resource", "unchecked"})
   public static TransportClient getConnection() throws Exception {
      // 设置集群名称
      Settings settings = Settings.builder().put("cluster.name", "xxxx").build();
      // 创建client 配置IP和端口
      TransportClient client = new PreBuiltTransportClient(settings)
             .addTransportAddresses(new TransportAddress(InetAddress.getByName("xxxxxxx"),
             10902));
      return client;
   }

}

你可能感兴趣的:(Elasticsearch 建立普通TransportClient 连接)