这是最后一篇了,查看es集群的状态。
话不多说,直接贴代码:
@Test
public void esStatus() throws UnknownHostException {
// 指定es集群;查看 elasticsearch.yml -- put("cluster.name",集群名称)
Settings settings = Settings.builder().put("cluster.name","elastricsearch").build();
// 创建访问es服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName("192.168.180.235"),9300));
ClusterHealthResponse healths = client.admin().cluster().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()
);
}
// 关闭客户端
client.close();
}
结果截图: