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();
}
}
}