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");
}
}
}