今天交付 ES 管理平台,因为 ES 有两套集群,分别是5.x 和 6.x 为了代码的通用性,需要把 Transport Client 的相关操作全部废弃,改为直接调用 rest api
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.3version>
dependency>
<dependency>
<groupId>com.arronlonggroupId>
<artifactId>httpclientutilartifactId>
<version>1.0.4version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.3.8version>
dependency>
/**
* 获取所有索引index
* @param ip
* @return
*/
public static List<IndexInfo> getAllIndex(String ip) {
HttpConfig config = HttpConfig.custom().url("http://"+ip+":9200/_cat/indices?format=json&pretty");
String jsonStr = null;
try {
jsonStr = HttpClientUtil.get(config);
} catch (HttpProcessException e) {
e.printStackTrace();
return null;
}
JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
List<IndexInfo> list = new ArrayList<>();
List<Map> maps = jsonArray.toList(Map.class);
for(Map map : maps) {
IndexInfo index = new IndexInfo();
index.setHealth ((String)map.get("health"));
index.setStatus ((String)map.get("status"));
index.setIndex ((String)map.get("index"));
index.setUuid ((String)map.get("uuid"));
index.setPri ((String)map.get("pri"));
index.setRep ((String)map.get("rep"));
index.setDocs ((String)map.get("docs.count"));
index.setDeleted ((String)map.get("docs.deleted"));
index.setStoreSize ((String)map.get("store.size"));
index.setPriStoreSize ((String)map.get("pri.store.size"));
list.add(index);
}
return list;
}
查询结果解释
/**
* 查看集群的健康状态
*
* @param ip
* @return
*/
public static List<Cluster> heathInfo(String ip) {
HttpConfig config = HttpConfig.custom().url("http://" + ip + ":9200/_cat/health?format=json&pretty");
String jsonStr = null;
try {
jsonStr = HttpClientUtil.get(config);
} catch (HttpProcessException e) {
e.printStackTrace();
return null;
}
JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
List<Cluster> list = new ArrayList<>();
List<Map> maps = jsonArray.toList(Map.class);
for (Map map : maps) {
Cluster cluster = new Cluster();
cluster.setCluster((String) map.get("cluster"));
cluster.setStatus((String) map.get("status"));
cluster.setNodeTotal((String) map.get("node.total"));
cluster.setNodeData((String) map.get("node.data"));
cluster.setShards((String) map.get("shards"));
cluster.setPri((String) map.get("pri"));
cluster.setRelo((String) map.get("relo"));
cluster.setInit((String) map.get("init"));
cluster.setUnassign((String) map.get("unassign"));
cluster.setPendingTasks((String) map.get("pending_tasks"));
cluster.setMaxTaskWaitTime((String) map.get("max_task_wait_time"));
cluster.setActiveShardsPercent((String) map.get("active_shards_percent"));
list.add(cluster);
}
return list;
}
/**
* 获取索引基本信息
*/
public static JSONObject getIndexInfo(String ip, String index) {
// 获取索引的别名,字段,创建时间
HttpConfig config = HttpConfig.custom().url("http://" + ip + ":9200/" + index);
String jsonStr = null;
try {
jsonStr = HttpClientUtil.get(config);
} catch (HttpProcessException e) {
e.printStackTrace();
return null;
}
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
JSONObject fields = jsonObject.getJSONObject(index).getJSONObject("mappings").getJSONObject("CrmLog").getJSONObject("properties");
JSONObject aliases = jsonObject.getJSONObject(index).getJSONObject("aliases");
JSONObject jsonIndex = jsonObject.getJSONObject(index).getJSONObject("settings").getJSONObject("index");
// 时间戳
String dateStr = jsonIndex.getStr("creation_date");
Date date = new Date(Long.parseLong(dateStr));
// 分片数
String shards = (String) jsonIndex.get("number_of_shards");
// 主分片数
String pri = (String) jsonIndex.get("number_of_replicas");
// 获取索引所占内存大小
config = HttpConfig.custom().url("http://" + ip + ":9200/" + index + "/_stats");
try {
jsonStr = HttpClientUtil.get(config);
} catch (HttpProcessException e) {
e.printStackTrace();
return null;
}
jsonObject = JSONUtil.parseObj(jsonStr);
Integer byteNum = (Integer) jsonObject.getJSONObject("indices").getJSONObject(index).getJSONObject("primaries").getJSONObject("store").get("size_in_bytes");
Integer docs = (Integer) jsonObject.getJSONObject("indices").getJSONObject(index).getJSONObject("primaries").getJSONObject("docs").get("count");
// 整合输出结果
String size;
BigDecimal bigDecimal = new BigDecimal(byteNum);
BigDecimal mb = bigDecimal.divide(BigDecimal.valueOf(1024 * 1024));
BigDecimal gb = mb.divide(BigDecimal.valueOf(1024));
if (gb.intValue() / 10 > 0) {
size = gb.setScale(2, RoundingMode.HALF_UP) + " gb";
} else {
size = mb.setScale(2, RoundingMode.HALF_UP) + " mb";
}
JSONObject result = new JSONObject();
list = getFields(fields);
String[] aliaseArr = getAliases(aliases);
result.putOpt("alise", aliaseArr);
result.putOpt("date", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
result.putOpt("shards", shards);
result.putOpt("pri", pri);
result.putOpt("size", size);
result.putOpt("docs", docs);
return result;
}
/**
* 解析别名
* @param aliases
* @return
*/
private static String[] getAliases(JSONObject aliases) {
String[] aliaseArr = {};
if(aliases.size() > 0) {
aliaseArr = aliases.toString()
.replace("{", "")
.replace("}", "")
.replace(",", "")
.replace("\"", "")
.split(":");
}
return aliaseArr;
}
/**
* 解析出字段list
* @param jsonObject
* @return
*/
private static List<ColumnModel> getFields(JSONObject jsonObject) {
Map map = JSONUtil.toBean(jsonObject, Map.class);
Set set = map.keySet();
List<ColumnModel> list = new ArrayList<>();
for(Object field : set) {
String fname = (String) field;
String type = jsonObject.getJSONObject(fname).getStr("type");
ColumnModel columnModel = new ColumnModel();
columnModel.setColumnName(fname);
columnModel.setTypeName(type);
list.add(columnModel);
}
return list;
}
未完待续