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