/** * 集羣健康狀態 */ public static void healthStatus() { //集羣管理對象 ClusterHealthResponse response = getAdminClient().prepareHealth().get(); System.out.println("cluster_name=" + response.getClusterName()); System.out.println("number_of_data_node=" + response.getNumberOfDataNodes()); System.out.println("number_of_node=" + response.getNumberOfNodes()); for (ClusterIndexHealth health : response.getIndices().values()) { System.out.printf("index=%s,number_of_shards=%d,number_of_replicas=%d,status=%s\n", health.getIndex(), health.getNumberOfShards(),health.getNumberOfReplicas(), health.getStatus().toString()); } }
測試node
public static void main(String[] args) { healthStatus(); }
執行結果web
cluster_name=es_cluster number_of_data_node=3 number_of_node=3 index=telegraph,number_of_shards=5,number_of_replicas=1,status=GREEN index=sfpay_log,number_of_shards=5,number_of_replicas=1,status=GREEN index=megacorp,number_of_shards=5,number_of_replicas=1,status=GREEN index=website,number_of_shards=5,number_of_replicas=1,status=GREEN index=.kibana,number_of_shards=1,number_of_replicas=1,status=GREEN index=my_index,number_of_shards=5,number_of_replicas=1,status=GREEN index=all-log,number_of_shards=5,number_of_replicas=1,status=GREEN index=db_audit,number_of_shards=5,number_of_replicas=1,status=GREEN index=information,number_of_shards=5,number_of_replicas=1,status=GREEN index=logstash-2018.04.10,number_of_shards=5,number_of_replicas=1,status=GREEN index=ssh_opt,number_of_shards=5,number_of_replicas=1,status=GREEN index=my_person,number_of_shards=5,number_of_replicas=1,status=GREEN
在超時時間內,等待獲取須要健康狀態ssh
client.admin().cluster().prepareHealth() .setWaitForYellowStatus() .get(); client.admin().cluster().prepareHealth("telegraph") .setWaitForGreenStatus() .get(); client.admin().cluster().prepareHealth("telegraph") .setWaitForGreenStatus() .setTimeout(TimeValue.timeValueSeconds(2)) .get();
/** * 阻塞到指定時間,獲取指定健康狀態集羣狀態 */ public static void waitHealthStatus() { //設置等待green狀態,超時時間300秒 ClusterHealthResponse response = getAdminClient().prepareHealth("telegraph") .setWaitForGreenStatus() .setTimeout(TimeValue.timeValueSeconds(300)) .get(); ClusterHealthStatus status = response.getIndices().get("telegraph").getStatus(); System.out.println(status); }