集群管理编辑
访问集群Java API,您需要调用 cluster()
方法从一个 AdminClient
:
ClusterAdminClient clusterAdminClient = client.admin().cluster();
在本指南中,我们将使用 client.admin().cluster()
.
集群的健康编辑
健康编辑
集群健康API允许获得一个非常简单的集群的健康状态,也可以给你一些技术信息集群状态每指数:
ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get(); //1
String clusterName = healths.getClusterName(); //2
int numberOfDataNodes = healths.getNumberOfDataNodes(); //3
int numberOfNodes = healths.getNumberOfNodes(); //4
for (ClusterIndexHealth health : healths) { //5
String index = health.getIndex(); //6
int numberOfShards = health.getNumberOfShards(); //7
int numberOfReplicas = health.getNumberOfReplicas(); //8
ClusterHealthStatus status = health.getStatus(); //9
}
等待状态编辑
您可以使用集群健康API来等待一个特定的状态对整个集群或对于一个给定的指标:
client.admin().cluster().prepareHealth() //1
.setWaitForYellowStatus() //2
.get();
client.admin().cluster().prepareHealth("company") //3
.setWaitForGreenStatus() //4
.get();
client.admin().cluster().prepareHealth("employee") //5
.setWaitForGreenStatus() //6
.setTimeout(TimeValue.timeValueSeconds(2)) //7
.get();
如果该指数没有预期的状态和你想失败在这种情况下,您需要显式地解释结果:
ClusterHealthResponse response = client.admin().cluster().prepareHealth("company")
.setWaitForGreenStatus() //1
.get();
ClusterHealthStatus status = response.getIndices().get("company").getStatus();
if (!status.equals(ClusterHealthStatus.GREEN)) {
throw new RuntimeException("Index is in " + status + " state"); //2
}
等待被绿色的指数 | |
如果不抛出异常 |