ElasticSearch(四):使用Java连接ElasticSearch集群


 1 public class ESIndexMapping {  2     
 3     private static String host="192.168.56.3"; // 服务器地址
 4     private static int port=9300; // 端口
 5     
 6     public static final String CLUSTER_NAME = "my-application"; //集群名称
 7     
 8     private TransportClient client=null;  9     
10     private static Settings settings= Settings.builder() 11             .put("cluster.name",CLUSTER_NAME) 12             .put("client.transport.sniff", true) 13  .build(); 14     
15     //获取客户端
16     @SuppressWarnings({ "resource", "unchecked" }) 17  @Before 18     public void getClient() throws Exception { 19         try { 20             client = new PreBuiltTransportClient(settings) 21                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port)); 22         } catch (Exception e) { 23             // TODO Auto-generated catch block
24  e.printStackTrace(); 25  } 26  } 27     
28     //关闭客户端
29  @After 30     public void close() { 31         if(client!=null) { 32  client.close(); 33  } 34  } 35 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM