• (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();
       }
  • 相关阅读:
    java——阶段性整理(方法的重载重写和一些关键字)
    设计模式——单例模式
    source
    set和setenv
    c++编译加执行脚本
    python脚本小记
    转义字符
    istream_iterator/ostream_iterator
    字符串替换程序 p324
    程序编译后运行时的内存分配
  • 原文地址:https://www.cnblogs.com/javasl/p/12081829.html
Copyright © 2020-2023  润新知