Skip to content

Commit 4ff4e42

Browse files
author
colorknight
committed
修改es查询数据的对象封装
1 parent a1d5fe3 commit 4ff4e42

File tree

1 file changed

+64
-42
lines changed

1 file changed

+64
-42
lines changed

moql-querier/src/main/java/org/datayoo/moql/querier/es/EsDataQuerier.java

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.datayoo.moql.*;
1212
import org.datayoo.moql.core.RecordSetImpl;
1313
import org.datayoo.moql.metadata.*;
14+
import org.datayoo.moql.operand.OperandContextArrayList;
1415
import org.datayoo.moql.operand.OperandFactory;
1516
import org.datayoo.moql.operand.factory.OperandFactoryImpl;
1617
import org.datayoo.moql.parser.MoqlParser;
@@ -377,55 +378,76 @@ protected void toMap(JsonObject jsonObject, Map<String, Object> record) {
377378
toMap((JsonObject) entry.getValue(), record);
378379
continue;
379380
}
380-
Object value = entry.getValue();
381-
if (value instanceof JsonArray) {
382-
toArrayRecord(entry.getKey(), (JsonArray) entry.getValue(), record);
383-
} else if (value instanceof JsonObject) {
384-
value = value.toString();
385-
record.put(entry.getKey(), value);
386-
} else if (entry.getValue() instanceof JsonPrimitive) {
387-
value = getValue((JsonPrimitive) entry.getValue());
388-
record.put(entry.getKey(), value);
389-
}
381+
JsonElement je = entry.getValue();
382+
record.put(entry.getKey(), getObject(je));
390383
}
391384
}
392385

393-
protected void toArrayRecord(String prefix, JsonArray array,
394-
Map<String, Object> record) {
395-
// 先把旧的数据集加入结果
396-
record.put(prefix, array.toString());
397-
Map<String, JsonArray> arrayMap = new HashMap<>();
398-
for (JsonElement element : array) {
399-
if (element instanceof JsonObject) {
400-
JsonObject jsonObject = (JsonObject) element;
401-
Set<Map.Entry<String, JsonElement>> set = jsonObject.entrySet();
402-
403-
for (Map.Entry<String, JsonElement> entry : set) {
404-
String key = prefix + "." + entry.getKey();
405-
if (arrayMap.get(key) == null) {
406-
JsonArray jsonArray = new JsonArray();
407-
jsonArray.add(entry.getValue());
408-
arrayMap.put(key, jsonArray);
409-
} else {
410-
JsonArray value = arrayMap.get(key);
411-
value.add(entry.getValue());
412-
arrayMap.put(key, value);
413-
}
414-
}
415-
}
416-
if (element instanceof JsonPrimitive) {
417-
if (record.get(prefix) == null) {
418-
Object value = getValue((JsonPrimitive) element);
419-
record.put(prefix, value);
420-
}
386+
protected Object getObject(JsonElement je) {
387+
if (je instanceof JsonObject) {
388+
return toMap((JsonObject) je);
389+
} else if (je instanceof JsonArray) {
390+
return toList((JsonArray) je);
391+
} else if (je instanceof JsonPrimitive) {
392+
return getValue((JsonPrimitive) je);
393+
}
394+
return null;
395+
}
421396

422-
}
397+
protected Map<String, Object> toMap(JsonObject jo) {
398+
Map<String, Object> map = new HashMap<>();
399+
for (Map.Entry<String, JsonElement> entry : jo.entrySet()) {
400+
JsonElement je = entry.getValue();
401+
map.put(entry.getKey(), getObject(je));
423402
}
424-
for (Map.Entry<String, JsonArray> entry : arrayMap.entrySet()) {
425-
toArrayRecord(entry.getKey(), entry.getValue(), record);
403+
return map;
404+
}
405+
406+
protected OperandContextArrayList toList(JsonArray ja) {
407+
OperandContextArrayList arrayList = new OperandContextArrayList(ja.size());
408+
for (int i = 0; i < ja.size(); i++) {
409+
JsonElement je = ja.get(i);
410+
arrayList.add(getObject(je));
426411
}
412+
return arrayList;
427413
}
428414

415+
// protected void toArrayRecord(String prefix, JsonArray array,
416+
// Map<String, Object> record) {
417+
// // 先把旧的数据集加入结果
418+
// record.put(prefix, array.toString());
419+
// Map<String, JsonArray> arrayMap = new HashMap<>();
420+
// for (JsonElement element : array) {
421+
// if (element instanceof JsonObject) {
422+
// JsonObject jsonObject = (JsonObject) element;
423+
// Set<Map.Entry<String, JsonElement>> set = jsonObject.entrySet();
424+
//
425+
// for (Map.Entry<String, JsonElement> entry : set) {
426+
// String key = prefix + "." + entry.getKey();
427+
// if (arrayMap.get(key) == null) {
428+
// JsonArray jsonArray = new JsonArray();
429+
// jsonArray.add(entry.getValue());
430+
// arrayMap.put(key, jsonArray);
431+
// } else {
432+
// JsonArray value = arrayMap.get(key);
433+
// value.add(entry.getValue());
434+
// arrayMap.put(key, value);
435+
// }
436+
// }
437+
// }
438+
// if (element instanceof JsonPrimitive) {
439+
// if (record.get(prefix) == null) {
440+
// Object value = getValue((JsonPrimitive) element);
441+
// record.put(prefix, value);
442+
// }
443+
//
444+
// }
445+
// }
446+
// for (Map.Entry<String, JsonArray> entry : arrayMap.entrySet()) {
447+
// toArrayRecord(entry.getKey(), entry.getValue(), record);
448+
// }
449+
// }
450+
429451
protected List<EntityMap> toAggregationEntityMaps(JsonObject jsonObject,
430452
List<ColumnDefinition> groupColumns) {
431453
List<EntityMap> entityMaps = new LinkedList<EntityMap>();
@@ -520,7 +542,7 @@ protected Object[] toRecord(Operand[] operands, EntityMap entityMap) {
520542
Object[] record = new Object[operands.length];
521543
for (int i = 0; i < operands.length; i++) {
522544
record[i] = entityMap.getEntity(operands[i].getName());
523-
// record[i] = operands[i].operate(entityMap);
545+
// record[i] = operands[i].operate(entityMap);
524546
}
525547
return record;
526548
}

0 commit comments

Comments
 (0)