ClusterAdminClient clusterAdminClient = client.admin().cluster();

Cluster Health

ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get(); 
String clusterName = healths.getClusterName();              
int numberOfDataNodes = healths.getNumberOfDataNodes();     
int numberOfNodes = healths.getNumberOfNodes();             
for (ClusterIndexHealth health : healths.getIndices().values()) { 
    String index = health.getIndex();                       
    int numberOfShards = health.getNumberOfShards();        
    int numberOfReplicas = health.getNumberOfReplicas();    
    ClusterHealthStatus status = health.getStatus();        
}

Wait for status

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();

 

If the index does not have the expected status and you want to fail in that case, you need to explicitly interpret the result:

ClusterHealthResponse response = client.admin().cluster().prepareHealth("company")
        .setWaitForGreenStatus()    
        .get();

ClusterHealthStatus status = response.getIndices().get("company").getStatus();
if (!status.equals(ClusterHealthStatus.GREEN)) {
    throw new RuntimeException("Index is in " + status + " state"); 
}



相关文章:

  • 2021-08-23
  • 2022-12-23
  • 2021-12-14
  • 2022-01-22
  • 2021-12-09
  • 2022-03-07
  • 2018-10-28
  • 2022-02-05
猜你喜欢
  • 2022-12-23
  • 2021-12-30
  • 2021-06-22
  • 2021-11-10
  • 2021-11-23
  • 2022-01-18
  • 2022-02-12
相关资源
相似解决方案