(26)ElasticSearch java項目中獲取集群、索引信息


  下面的代碼展示了如何獲取集群信息和索引信息

@Test
    public void testCluster() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build(); 
        //創建客戶端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get();
        String clusterName = healths.getClusterName();
        //輸出集群名
        System.out.println("clusterName = :"+clusterName);
        int numberOfDataNodes = healths.getNumberOfDataNodes();
        //輸出節點數量
        System.out.println("numberOfDataNodes = :"+numberOfDataNodes);
        //輸出每個索引信息
        for(ClusterIndexHealth health:healths.getIndices().values()) {
            String index = health.getIndex();
            int numberOfShards = health.getNumberOfShards();
            int numberOfReplicas = health.getNumberOfReplicas();
            System.out.println("index = "+index);//索引名
            System.out.println("numberOfShards = "+numberOfShards);//分片數量
            System.out.println("numberOfReplicas = "+numberOfReplicas);//副本數量
            
            ClusterHealthStatus clusterHealthStatus = health.getStatus();
            System.out.println("clusterHealthStatus = "+clusterHealthStatus.toString());//健康狀態
        }
        client.close();
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM