首页 > 解决方案 > 如何使用 Java API 获取 ClusterState 或 IndexMetaData

问题描述

我正在使用 ES 7.12,并且我有以下简单的代码,我想获取给定 Id 的 shardId,但我被困在如何获取ClusterState

应用程序代码如下,有人可以帮助如何处理该ClusterState对象吗?非常感谢!


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.routing.OperationRouting;

public class RoutingTest {
    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9201, "http")));
        String id = "123";
        String indexName = "idx_001";
        //TODO how to get the clusterState
        ClusterState clusterState = null;
        IndexMetaData indexMetaData = clusterState.metaData().index(indexName);
        int shardId = OperationRouting.generateShardId(indexMetaData, id, null);
        System.out.println(shardId);
    }
}

更新

  1. 确切地说,我真正想要的是IndexMetaData给定索引的对象
  2. stackoverflow 上有一个类似的问题,但答案不是我想要的。

如何使用 RestHighLevelClient 获取集群状态或索引元数据?

标签: elasticsearch

解决方案


推荐阅读