处理ES的Cat命令结果集示例
以_cat/health 为例 一般的返回结果为
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana i2DCHBCIQpmNv844IfH86w 1 0 2 0 6.9kb 6.9kb
green open .monitoring-alerts-2 Qd_LW97SQmKJeeuS3tjnvg 1 0 1 0 7kb 7kb
green open .monitoring-data-2 hK915HnWRyuRKkgMiLkCaA 5 1 8 0 31.1kb 15.5kb
green open .watches 0my1zAAwRNe_G8rsd69WNA 1 0 0 0 160b 160b
green open action-20210318 LPw2WFbCSsK01OW1s1976w 1 0 0 0 159b 159b
green open action-20210319 oOUVkHM0SqKKfMTr09Ti8g 1 0 0 0 159b 159b
green open action-20210320 cUx0TcpiQi2yxrxpEr-hdQ 1 0 0 0 159b 159b
里面包含了10个数据项,第一行为表头,每一项的数据项的长度不固定,分割符可以视为是数量不等的空格符。 采用RESTClient进行操作
public static void getCustomIndex() throws IOException {
Response response = client.performRequest("GET", "_cat/indices?v&s=index");
List<String> indices = new ArrayList<>();
String all = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(all.getBytes(StandardCharsets.UTF_8))));
String line;
int i = 10;
String firstLine = bufferedReader.readLine();
if (null == firstLine) {
return;
}
while ((null != (line = bufferedReader.readLine())) && i > 0) {
line = line.replaceAll("\\s+", " ");
String[] a = line.split(" ");
if (!a[2].startsWith(".")) {
indices .add(a[2]);
}
i--;
}
indices.forEach(System.out::println);
}
}
对于其他的_cat命令也是可以用同样的思路去解析结果集的。
|