ElasticSearch 6.x 学习笔记:35.Java API之集群管理

1、ClusterAdminClient

ESUtil.java类中增加获取集群管理的ClusterAdminClient对象的方法

    /**
     * 获取集群管理的ClusterAdminClient对象
     */
    public static ClusterAdminClient getClusterAdminClient(){
        return getClient().admin().cluster();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、集群健康

package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;

public class ClusterAdminDemo {
    public static void main(String[] args){
        ClusterHealthResponse healths = ESUtil.getClusterAdminClient().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());
        }
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
clusterName=elasticsearch
numberOfDataNodes=1
numberOfNodes=1
index=mydate,numberOfShards=5,numberOfReplicas=1
YELLOW
index=website,numberOfShards=5,numberOfReplicas=1
YELLOW
index=test,numberOfShards=5,numberOfReplicas=1
YELLOW
index=my-index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=book,numberOfShards=5,numberOfReplicas=1
YELLOW
index=child_example,numberOfShards=5,numberOfReplicas=1
YELLOW
index=join_index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=blog,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index_1,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index_2,numberOfShards=5,numberOfReplicas=1
YELLOW
index=twitter,numberOfShards=5,numberOfReplicas=1
YELLOW
index=books,numberOfShards=5,numberOfReplicas=1
YELLOW
index=my_index,numberOfShards=5,numberOfReplicas=1
YELLOW
index=index1,numberOfShards=5,numberOfReplicas=1
YELLOW
index=logs,numberOfShards=5,numberOfReplicas=1
YELLOW
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3、Wait for statusedit

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();
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;

public class ClusterAdminDemo {
    public static void main(String[] args){
        ClusterHealthResponse response=ESUtil.getClusterAdminClient()
                .prepareHealth("website")
                .setWaitForGreenStatus()
                .get();

        ClusterHealthStatus status = response.getIndices().get("website").getStatus();
        if (!status.equals(ClusterHealthStatus.GREEN)) {
            throw new RuntimeException("Index is in " + status + " state");
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

no modules loaded 

loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin] 

loaded plugin [org.elasticsearch.join.ParentJoinPlugin] 

loaded plugin [org.elasticsearch.percolator.PercolatorPlugin] 

loaded plugin [org.elasticsearch.script.mustache.MustachePlugin] 

loaded plugin [org.elasticsearch.transport.Netty4Plugin] 

Exception in thread “main” java.lang.RuntimeException: Index is in YELLOW state 

    at cn.hadron.ClusterAdminDemo.main(ClusterAdminDemo.java:17)

Process finished with exit code 1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10




猜你喜欢

转载自blog.csdn.net/u011428598/article/details/81082246
今日推荐