ElasticSearch 6.x 学习笔记:35.Java API之集群管理

https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-admin-cluster.html

1、ClusterAdminClient

ESUtil.java类中增加获取集群管理的ClusterAdminClient对象的方法

    /**
     * 获取集群管理的ClusterAdminClient对象
     */
    public static ClusterAdminClient getClusterAdminClient(){
        return getClient().admin().cluster();
    }

2、集群健康

package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;

public class ClusterAdminDemo {
    public static void main(String[] args){
        ClusterHealthResponse healths = ESUtil.getClusterAdminClient().prepareHealth().get();
        String clusterName = healths.getClusterName();
        System.out.println("clusterName="+clusterName);
        int numberOfDataNodes = healths.getNumberOfDataNodes();
        System.out.println("numberOfDataNodes="+numberOfDataNodes);
        int numberOfNodes = healths.getNumberOfNodes();
        System.out.println("numberOfNodes="+numberOfNodes);

        for (ClusterIndexHealth health : healths.getIndices().values()) {
            String index = health.getIndex();
            int numberOfShards = health.getNumberOfShards();
            int numberOfReplicas = health.getNumberOfReplicas();
            System.out.printf("index=%s,numberOfShards=%d,numberOfReplicas=%d\n",index,numberOfShards,numberOfReplicas);
            ClusterHealthStatus status = health.getStatus();
            System.out.println(status.toString());
        }
    }
}
clusterName=elasticsearch
numberOfDataNodes=1
numberOfNodes=1
index=mydate,numberOfShards=5,numberOfReplicas=1
YELLOW
index=website,numberOfShards=5,numberOfReplicas=1
YELLOW
index=test,numberOfShards=5,numberOfReplicas=1
YELLOW
index=my-index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=book,numberOfShards=5,numberOfReplicas=1
YELLOW
index=child_example,numberOfShards=5,numberOfReplicas=1
YELLOW
index=join_index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=blog,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index_1,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index_2,numberOfShards=5,numberOfReplicas=1
YELLOW
index=twitter,numberOfShards=5,numberOfReplicas=1
YELLOW
index=books,numberOfShards=5,numberOfReplicas=1
YELLOW
index=my_index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index1,numberOfShards=5,numberOfReplicas=1
YELLOW
index=logs,numberOfShards=5,numberOfReplicas=1
YELLOW

3、Wait for statusedit

You can use the cluster health API to wait for a specific status for the whole cluster or for a given index:

client.admin().cluster().prepareHealth()            
        .setWaitForYellowStatus()                   
        .get();
client.admin().cluster().prepareHealth("company")   
        .setWaitForGreenStatus()                    
        .get();

client.admin().cluster().prepareHealth("employee")  
        .setWaitForGreenStatus()                    
        .setTimeout(TimeValue.timeValueSeconds(2))  
        .get();
package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;

public class ClusterAdminDemo {
    public static void main(String[] args){
        ClusterHealthResponse response=ESUtil.getClusterAdminClient()
                .prepareHealth("website")
                .setWaitForGreenStatus()
                .get();

        ClusterHealthStatus status = response.getIndices().get("website").getStatus();
        if (!status.equals(ClusterHealthStatus.GREEN)) {
            throw new RuntimeException("Index is in " + status + " state");
        }
    }
}
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
Exception in thread "main" java.lang.RuntimeException: Index is in YELLOW state
    at cn.hadron.ClusterAdminDemo.main(ClusterAdminDemo.java:17)

Process finished with exit code 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值