Elasticsearch查询分片信息

package com.easy.es.monitor;

import com.easy.es.common.CommonUtils;
import com.easy.es.network.HttpClientResp;
import com.easy.es.network.HttpUtils;
import lombok.Data;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author a.du on 2022/10/31.
 */
public class Shards {
    //index                                  shard prirep state      docs    store ip             node
    private static final String API = "/_cat/shards";

    public static List<ShardInfo> getShardInfo(String host, String port, Map<String, String> params) throws IOException, URISyntaxException {
        HttpClientResp httpClientResp = HttpUtils.doGet(CommonUtils.getUrl(host, port, API), params);
        if (200 == httpClientResp.getCode()) {
            String[] result = httpClientResp.getContent().split("\n");
            List<ShardInfo> shardInfoList = new ArrayList<>();
            for (int i = 1; i < result.length; i++) {
                ShardInfo shardInfo = new ShardInfo();
                String[] temp = result[i].split("\\s+");
                //10.101.129.205 12463 6.3.2 - 10.101.129.205:11463
                shardInfo.setIndexName(temp[0]);
                shardInfo.setShard(temp[1]);
                shardInfo.setPrirep(temp[2]);
                shardInfo.setState(temp[3]);
                shardInfo.setDocs(temp[4]);
                shardInfo.setStore(temp[5]);
                shardInfo.setIp(temp[6]);
                shardInfo.setNode(temp[7]);
                shardInfoList.add(shardInfo);
            }
            return shardInfoList;
        }
        return null;
    }
}

@Data
class ShardInfo {
    /**
     * 索引名称
     */
    private String indexName;
    /**
     * 分片编号
     */
    private String shard;
    /**
     * 主分片:p 副本分片:r
     */
    private String prirep;
    /**
     * 状态:
     * INITIALIZING: 正在恢复
     * RELOCATING: 迁移中
     * STARTED: 已启动
     * UNASSIGNED: 未分配
     */
    private String state;
    /**
     * 文档数量
     */
    private String docs;
    /**
     * 占用磁盘空间
     */
    private String store;
    /**
     * ip
     */
    private String ip;
    /**
     * 节点
     */
    private String node;
}

测试用例:
public class Main {

    public static void main(String[] args) {
     
            Map<String, String> params = new HashMap<>();
            params.put("index", "ocontent_20220905");
            System.out.println(JSON.toJSONString(Shards.getShardInfo("10.10.19.24", "1463",params)));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值