本篇博客主要是查看集群中的相關信息,具體請看代碼和注釋
@Test public void test45() throws UnknownHostException{ //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //獲取集群信息 ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().get(); //獲取集群名稱 String clusterName = healthResponse.getClusterName(); System.out.println(clusterName); //獲取存放數據的那些節點 int numberOfDataNodes = healthResponse.getNumberOfDataNodes(); System.out.println(numberOfDataNodes); //獲取節點的總數量 int numberOfNodes = healthResponse.getNumberOfNodes(); System.out.println(numberOfNodes); //獲取集群中一共有多少索引 for(ClusterIndexHealth health:healthResponse.getIndices().values()) { String index = health.getIndex();//當前索引名稱 int numberOfShards = health.getNumberOfShards();//主分片 int numberOfReplicas = health.getNumberOfReplicas();//副本 ClusterHealthStatus status = health.getStatus();//得到當前的健康狀況 System.out.println(status);//健康-綠色 一般-黃色 不健康-紅色 } }
關於java操作elasticsearch的操作在此就告一段落,后期本人將書寫java調用微信分享的應用博客,對后期博客感興趣的朋友可以關注交流,轉發請說明出處,本人的博客地址為:https://www.cnblogs.com/chenyuanbo/
技術在於交流!