1. 程式人生 > 程式設計 >Java基於elasticsearch實現叢集管理

Java基於elasticsearch實現叢集管理

這篇文章主要介紹了java基於elasticsearch實現叢集管理,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

本篇文章主要是檢視叢集中的相關資訊,具體請看程式碼和註釋

@Test
public void test45() throws UnknownHostException{
  //1、指定es叢集 cluster.name 是固定的key值,my-application是ES叢集的名稱
  Settings settings = Settings.builder().put("cluster.name","my-application").build();
  //2.建立訪問ES伺服器的客戶端
  TransportClient client = new PreBuiltTransportClient(settings)
      .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"),9300));
  //獲取叢集資訊
  ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().get();
  //獲取叢集名稱
  String clusterName = healthResponse.getClusterName();
  System.out.println(clusterName);
  //獲取存放資料的那些節點
  int numberOfDataNodes = healthResponse.getNumberOfDataNodes();
  System.out.println(numberOfDataNodes);
  //獲取節點的總數量
  int numberOfNodes = healthResponse.getNumberOfNodes();
  System.out.println(numberOfNodes);
  //獲取叢集中一共有多少索引
  for(ClusterIndexHealth health:healthResponse.getIndices().values()) {
    String index = health.getIndex();//當前索引名稱
    int numberOfShards = health.getNumberOfShards();//主分片
    int numberOfReplicas = health.getNumberOfReplicas();//副本
    ClusterHealthStatus status = health.getStatus();//得到當前的健康狀況
    System.out.println(status);//健康-綠色 一般-黃色 不健康-紅色
  }
  
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。