赞
踩
查询结果
- index shard prirep state docs store ip node
- twitter04-700 0 p STARTED 0 261b 127.0.0.2 bdmssitdb48
- twitter05-127 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49
- twitter05-579 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49
- twitter02-357 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49
解析工具类
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
-
- public class EsResponseParseUtil {
- public static final byte separatorByte;
-
- static {
- byte[] separator = " ".getBytes();
- separatorByte = separator[0];
- }
-
- public static void main(String[] args) {
- EsResponseParseUtil test = new EsResponseParseUtil();
- List<HeadInfo> headInfos = test.parseHead("health status index uuid pri rep docs.count docs.deleted store.size pri.store.size");
- String[] strs = test.split("green open .kibana 8axK7P5nTKm1qSj3ljR2Aw 1 0 1 0 5.2kb 5.2kb", headInfos);
- for (String value : strs) {
- // if (value == null) value = "=";
- java.lang.System.out.println(value);
- }
- }
-
- public String[] split(String str, List<HeadInfo> headInfos) {
- byte[] lineBytes = str.getBytes();
- ArrayList<Integer> tabOffsets = new ArrayList<Integer>();
- byte preByte = separatorByte;
- for (int i = 0; i < lineBytes.length; i++) {
- if ((lineBytes[i] == separatorByte || preByte == separatorByte) && lineBytes[i] != preByte) {
- tabOffsets.add(i);
- }
- preByte = lineBytes[i];
- }
- tabOffsets.add(lineBytes.length);
- String[] result = new String[headInfos.size()];
- for (int j = 0; j < tabOffsets.size()-1; j += 2) {
- int startIndex = tabOffsets.get(j);
- int endIndex = tabOffsets.get(j + 1);
- for (HeadInfo headInfo : headInfos) {
- if (!(headInfo.start > endIndex || headInfo.end < startIndex)) {
- result[headInfo.index] = new String(lineBytes, startIndex, endIndex - startIndex);
- }
- }
- }
- return result;
- }
-
- public List<HeadInfo> parseHead(String str) {
- byte[] lineBytes = str.getBytes();
- ArrayList<Integer> tabOffsets = new ArrayList<Integer>();
- byte preByte = separatorByte;
- for (int i = 0; i < lineBytes.length; i++) {
- if ((lineBytes[i] == separatorByte || preByte == separatorByte) && lineBytes[i] != preByte) {
- tabOffsets.add(i);
- }
- preByte = lineBytes[i];
- }
- tabOffsets.add(lineBytes.length);
- int index = 0;
- List<HeadInfo> headInfos = new LinkedList<>();
- for (int j = 0; j < tabOffsets.size(); j += 2) {
- int startIndex = tabOffsets.get(j);
- int endIndex = tabOffsets.get(j + 1);
- HeadInfo headInfo = new HeadInfo();
- headInfo.setStart(startIndex);
- headInfo.setEnd(endIndex);
- headInfo.setIndex(index);
- headInfos.add(headInfo);
- index++;
-
- }
- return headInfos;
- }
-
- public class HeadInfo {
- String filedName;
- int start;
- int end;
- int index;
-
- public String getFiledName() {
- return filedName;
- }
-
- public void setFiledName(String filedName) {
- this.filedName = filedName;
- }
-
- public int getStart() {
- return start;
- }
-
- public void setStart(int start) {
- this.start = start;
- }
-
- public int getEnd() {
- return end;
- }
-
- public void setEnd(int end) {
- this.end = end;
- }
-
- public int getIndex() {
- return index;
- }
-
- public void setIndex(int index) {
- this.index = index;
- }
- }
- }
解析逻辑:
- RestClient restClient = BdesService.restClients().get(Integer.parseInt(clusterId));
- Response shardsInfoResponse = null;
- List<ShardsInfoDto> shardsList = new ArrayList();
- shardsInfoResponse = restClient.performRequest("GET", "_cat/shards?v");
- String shardsInfo = EntityUtils.toString(shardsInfoResponse.getEntity());
- LOGGER.info("shardsInfo:" + shardsInfo);
- String[] shardsInfoArray = shardsInfo.split("\n");
- EsResponseParseUtil esResponse = new EsResponseParseUtil();
- List<EsResponseParseUtil.HeadInfo> headInfos = esResponse.parseHead(shardsInfoArray[0]);
- for (int i = 1; i < shardsInfoArray.length; i++) {
- if (shardsInfoArray[i].startsWith(".")) {
- continue;
- }
- String[] split1 = esResponse.split(shardsInfoArray[i], headInfos);
- ShardsInfoDto shardsInfoDto = getShardsInfoDto(split1);
- if("RELOCATING".equals(shardsInfoDto.getShardStates())){
- //按空格分隔;
- String[] shardsLineArray = shardsInfoArray[i].split("\\s+");
- //取最后一位作为ToNodeName
- shardsInfoDto.setToNodeName(shardsLineArray[shardsLineArray.length-1]);
- }
- shardsList.add(shardsInfoDto);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。