(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();
   }
原文地址:https://www.cnblogs.com/javasl/p/12081829.html