当前位置:   article > 正文

Elasticsearch api查询多行空格分隔数据java解析_elasticsearch用,分隔

elasticsearch用,分隔

查询结果

  1. index shard prirep state docs store ip node
  2. twitter04-700 0 p STARTED 0 261b 127.0.0.2 bdmssitdb48
  3. twitter05-127 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49
  4. twitter05-579 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49
  5. twitter02-357 0 p STARTED 0 261b 127.0.0.1 bdmssitdb49

解析工具类

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. public class EsResponseParseUtil {
  5. public static final byte separatorByte;
  6. static {
  7. byte[] separator = " ".getBytes();
  8. separatorByte = separator[0];
  9. }
  10. public static void main(String[] args) {
  11. EsResponseParseUtil test = new EsResponseParseUtil();
  12. List<HeadInfo> headInfos = test.parseHead("health status index uuid pri rep docs.count docs.deleted store.size pri.store.size");
  13. String[] strs = test.split("green open .kibana 8axK7P5nTKm1qSj3ljR2Aw 1 0 1 0 5.2kb 5.2kb", headInfos);
  14. for (String value : strs) {
  15. // if (value == null) value = "=";
  16. java.lang.System.out.println(value);
  17. }
  18. }
  19. public String[] split(String str, List<HeadInfo> headInfos) {
  20. byte[] lineBytes = str.getBytes();
  21. ArrayList<Integer> tabOffsets = new ArrayList<Integer>();
  22. byte preByte = separatorByte;
  23. for (int i = 0; i < lineBytes.length; i++) {
  24. if ((lineBytes[i] == separatorByte || preByte == separatorByte) && lineBytes[i] != preByte) {
  25. tabOffsets.add(i);
  26. }
  27. preByte = lineBytes[i];
  28. }
  29. tabOffsets.add(lineBytes.length);
  30. String[] result = new String[headInfos.size()];
  31. for (int j = 0; j < tabOffsets.size()-1; j += 2) {
  32. int startIndex = tabOffsets.get(j);
  33. int endIndex = tabOffsets.get(j + 1);
  34. for (HeadInfo headInfo : headInfos) {
  35. if (!(headInfo.start > endIndex || headInfo.end < startIndex)) {
  36. result[headInfo.index] = new String(lineBytes, startIndex, endIndex - startIndex);
  37. }
  38. }
  39. }
  40. return result;
  41. }
  42. public List<HeadInfo> parseHead(String str) {
  43. byte[] lineBytes = str.getBytes();
  44. ArrayList<Integer> tabOffsets = new ArrayList<Integer>();
  45. byte preByte = separatorByte;
  46. for (int i = 0; i < lineBytes.length; i++) {
  47. if ((lineBytes[i] == separatorByte || preByte == separatorByte) && lineBytes[i] != preByte) {
  48. tabOffsets.add(i);
  49. }
  50. preByte = lineBytes[i];
  51. }
  52. tabOffsets.add(lineBytes.length);
  53. int index = 0;
  54. List<HeadInfo> headInfos = new LinkedList<>();
  55. for (int j = 0; j < tabOffsets.size(); j += 2) {
  56. int startIndex = tabOffsets.get(j);
  57. int endIndex = tabOffsets.get(j + 1);
  58. HeadInfo headInfo = new HeadInfo();
  59. headInfo.setStart(startIndex);
  60. headInfo.setEnd(endIndex);
  61. headInfo.setIndex(index);
  62. headInfos.add(headInfo);
  63. index++;
  64. }
  65. return headInfos;
  66. }
  67. public class HeadInfo {
  68. String filedName;
  69. int start;
  70. int end;
  71. int index;
  72. public String getFiledName() {
  73. return filedName;
  74. }
  75. public void setFiledName(String filedName) {
  76. this.filedName = filedName;
  77. }
  78. public int getStart() {
  79. return start;
  80. }
  81. public void setStart(int start) {
  82. this.start = start;
  83. }
  84. public int getEnd() {
  85. return end;
  86. }
  87. public void setEnd(int end) {
  88. this.end = end;
  89. }
  90. public int getIndex() {
  91. return index;
  92. }
  93. public void setIndex(int index) {
  94. this.index = index;
  95. }
  96. }
  97. }

解析逻辑:

  1. RestClient restClient = BdesService.restClients().get(Integer.parseInt(clusterId));
  2. Response shardsInfoResponse = null;
  3. List<ShardsInfoDto> shardsList = new ArrayList();
  4. shardsInfoResponse = restClient.performRequest("GET", "_cat/shards?v");
  5. String shardsInfo = EntityUtils.toString(shardsInfoResponse.getEntity());
  6. LOGGER.info("shardsInfo:" + shardsInfo);
  7. String[] shardsInfoArray = shardsInfo.split("\n");
  8. EsResponseParseUtil esResponse = new EsResponseParseUtil();
  9. List<EsResponseParseUtil.HeadInfo> headInfos = esResponse.parseHead(shardsInfoArray[0]);
  10. for (int i = 1; i < shardsInfoArray.length; i++) {
  11. if (shardsInfoArray[i].startsWith(".")) {
  12. continue;
  13. }
  14. String[] split1 = esResponse.split(shardsInfoArray[i], headInfos);
  15. ShardsInfoDto shardsInfoDto = getShardsInfoDto(split1);
  16. if("RELOCATING".equals(shardsInfoDto.getShardStates())){
  17. //按空格分隔;
  18. String[] shardsLineArray = shardsInfoArray[i].split("\\s+");
  19. //取最后一位作为ToNodeName
  20. shardsInfoDto.setToNodeName(shardsLineArray[shardsLineArray.length-1]);
  21. }
  22. shardsList.add(shardsInfoDto);
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/325661
推荐阅读
相关标签
  

闽ICP备14008679号