前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch 6.x 学习笔记:35.Java API之集群管理

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

作者头像
程裕强
发布2022-05-06 19:24:43
2040
发布2022-05-06 19:24:43
举报

https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-admin-cluster.html

1、ClusterAdminClient

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

代码语言:javascript
复制
    /**
     * 获取集群管理的ClusterAdminClient对象
     */
    public static ClusterAdminClient getClusterAdminClient(){
        return getClient().admin().cluster();
    }

2、集群健康

代码语言:javascript
复制
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());
        }
    }
}
代码语言:javascript
复制
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

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:

代码语言:javascript
复制
client.admin().cluster().prepareHealth()            
        .setWaitForYellowStatus()                   
        .get();
client.admin().cluster().prepareHealth("company")   
        .setWaitForGreenStatus()                    
        .get();

client.admin().cluster().prepareHealth("employee")  
        .setWaitForGreenStatus()                    
        .setTimeout(TimeValue.timeValueSeconds(2))  
        .get();
代码语言:javascript
复制
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");
        }
    }
}
代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、ClusterAdminClient
  • 2、集群健康
  • 3、Wait for statusedit
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档