java使用elasticsearch实现集群管理

本篇博客主要是查看集群中的相关信息,具体请看代码和注释

@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/

技术在于交流!

猜你喜欢

转载自www.cnblogs.com/chenyuanbo/p/10296858.html
今日推荐