weixin_45468370 2021-05-18 10:11 采纳率: 0%
浏览 82

jestClient 查看索引信息

jestClient 怎么可以查看ES某个索引的信息 如:是否处于关闭状态,索引中数据的大小

  • 写回答

1条回答 默认 最新

  • 程序yang 全栈领域优质创作者 2023-02-23 11:02
    关注

    仅供参考:
    你可以使用Elasticsearch的REST API来查询ES某个索引的信息。具体地,你可以使用_cluster/state API来获取集群状态信息,然后从中提取特定索引的信息。下面是使用JestClient查询ES某个索引信息的示例代码:

    import io.searchbox.client.JestClient;
    import io.searchbox.client.JestResult;
    import io.searchbox.core.Search;
    import io.searchbox.core.SearchResult;
    
    import java.io.IOException;
    
    public class ESIndexInfo {
        private JestClient jestClient;
    
        public ESIndexInfo(JestClient jestClient) {
            this.jestClient = jestClient;
        }
    
        /**
         * 判断索引是否处于关闭状态
         */
        public boolean isIndexClosed(String indexName) throws IOException {
            String uri = "/_cluster/state/" + indexName;
            JestResult result = jestClient.execute(new Search.Builder(uri).build());
            if (result.isSucceeded()) {
                return result.getJsonObject().getAsJsonObject("metadata").getAsJsonObject("indices").getAsJsonObject(indexName).get("state").getAsString().equals("close");
            } else {
                throw new IOException("Failed to get index state for " + indexName + ": " + result.getErrorMessage());
            }
        }
    
        /**
         * 获取索引中数据的大小
         */
        public long getIndexSize(String indexName) throws IOException {
            String uri = "/" + indexName + "/_stats";
            JestResult result = jestClient.execute(new Search.Builder(uri).build());
            if (result.isSucceeded()) {
                return result.getJsonObject().getAsJsonObject("_all").getAsJsonObject("primaries").getAsJsonObject("store").get("size_in_bytes").getAsLong();
            } else {
                throw new IOException("Failed to get index stats for " + indexName + ": " + result.getErrorMessage());
            }
        }
    }
    

    在上述代码中,isIndexClosed方法使用_cluster/state API来获取索引的状态信息,然后从中提取出该索引的状态是否为关闭。getIndexSize方法使用_stats API来获取索引的统计信息,然后从中提取出该索引的数据大小。

    需要注意的是,上述代码使用的是JestClient来查询ES索引信息。如果你使用的是其他的Elasticsearch客户端,则可以使用对应的API来进行查询。

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮