1、ClusterAdminClient
2、集群健康
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());
}
}
}
3、Wait for statusedit
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");
}
}
}
本文介绍如何使用Elasticsearch的ClusterAdminClient进行集群健康状态的检查,包括获取集群名称、节点数量、数据节点数量及各索引的健康状况。同时,展示了如何设置等待状态,确保指定索引达到绿色健康状态。
972

被折叠的 条评论
为什么被折叠?



