elasticsearch - 如何使用 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);
}
}
更新
- 确切地说,我真正想要的是
IndexMetaData
给定索引的对象 - stackoverflow 上有一个类似的问题,但答案不是我想要的。
解决方案
推荐阅读
- c# - 动态 Gridview CalendarExtender 不工作
- xamarin - Xamarin 表单配置文件
- java - 如何将数据源连接到 rhpam 中的数据输入分配源?
- sql - ORA-06550: ORA-00942: 表或视图不存在错误,当在创建它的同一脚本上引用表时
- asp.net-core - Identity4 反向通道注销 SSO/SLO 不适用于所有客户端
- apache-kafka - USER1:初始化端口 0 EAL:错误 - 退出代码:1 原因:无法设置 TX 队列;port=0, err=无效参数 (fastcapa)
- vba - VBA/Powershell 推送参数和返回结果
- html - 缩短 jQuery 动画加载器
- javascript - 然后转换为异步等待javascript
- sql - 在不同条件下将两个 SQL 与 group & count 联合起来