【仅供内部供应商使用,不提供对外解答和培训】
Table of Contents |
---|
MongoDB中,列的类型不同(Array类型,Object类型),在转换成为二维表的时候,就会有不同的处理方式。
从MongoDB数据集插件8.34+版本,提供接口可自行开发插件,处理更为复杂的类型的列。
Code Block | ||||
---|---|---|---|---|
| ||||
public interface ColumnResolver extends Mutable { String MARK_STRING = "ColumnResolver"; int CURRENT_LEVEL = 1; boolean checkIfConditionMeet(ColumnResolver resolver); /** * 是否需要处理该类型的元素 * * @param cell doc查询出来的对象 * @return 需要做扩展处理则返回true,否则返回false */ boolean accept(Object cell); /** * 增加新的列 * * @param key 列名 * @param cell 内容 */ List<FieldObject> expandColumn(String key, Object cell); /** * 扩展特殊列,增加新的行 * * @param doc MongoDB的文档查询对象 * @param columnNames 列民集合列名集合 * @param waitingColumnsIndexcolumnResolverMap 需要特殊处理的列集合需要特殊处理的列以及处理器集合 * @param rowDataCollections 扩展出来的行数据集合 * @param rowData 当前行数据 */ void expandData(Document doc, List<String> columnNames, Map<FieldObject, List<Integer>ColumnResolver> waitingColumnsIndex, columnResolverMap, List<List<Object>> rowDataCollections, List<Object> rowData); } |
...
我们以数组元素为例子,面对这样的数据结构:
Code Block | ||||
---|---|---|---|---|
| ||||
{ "name" : "李四", "age" : 15, "language" : [ "英语", "数学", "物理" ], "region" : [ "新疆", "海南", "台湾" ], "country" : "US" } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
import org.bson.Document; import java.util.*; public class ArrayColumnResolver extends AbstractColumnResolver { @Override public boolean accept(Object cell) { return cell instanceof ArrayList && !(((ArrayList) cell).get(0) instanceof ListDocument); } @Override public voidList<FieldObject> expandDataexpandColumn(DocumentString doc, List<String> columnNames, List<Integer> waitingColumnsIndex, List<List<Object>> rowDataCollections, List<Object> rowData) { key, Object cell) { List<FieldObject> data = new ArrayList<FieldObject>(); fillRowDataByArray(doc, columnNames, waitingColumnsIndex, rowDataCollections, rowData)data.add(FieldObject.create(key)); return data; } @Override privatepublic void fillRowDataByArrayexpandData(Document doc, List<String> columnNames, List<Integer> waitingColumnsIndexMap<FieldObject, ColumnResolver> columnResolverMap, List<List<Object>> rowDataCollections, List<Object> rowData) { Map<Integer, List<Object>> group = new HashMap<Integer, List<Object>>(); List<Object> standard = null; int maxLength = 0; for (int index : waitingColumnsIndex) {Map.Entry<FieldObject, ColumnResolver> entry : columnResolverMap.entrySet()) { if (!checkIfConditionMeet(entry.getValue())) { continue; } FieldObject fieldObject = entry.getKey(); Object data = doc.get(columnNamesfieldObject.getgetOriginalName(index)); if (data instanceof ListArrayList) { List<Object> array = (List<Object>) data; if (standard == null) { standard = array; } maxLength = Math.max(maxLength, array.size()); group.put(fieldObject.getIndex(index), array); } } if (standard != null) { for (int i = 0; i < maxLength; i++) { List<Object> row = new ArrayList<Object>(Arrays.asList(new Object[rowData.size()])); Collections.copy(row, rowData); for (intFieldObject indexfieldObject : waitingColumnsIndexcolumnResolverMap.keySet()) { List<Object> array = group.get(fieldObject.getIndex(index)); row.set(indexfieldObject.getIndex(), array.size() > i ? array.get(i) : null); } rowDataCollections.add(row); } } } } |
我们以数组元素为例子,面对这样的数据结构:
Code Block | ||||
---|---|---|---|---|
| ||||
{
"TEST_ID" : "8376",
"Section_Name" : "大三李",
"ItemList" : [
{
"Item_Name" : "AAA",
"Item_Value" : "123"
},
{
"Item_Name" : "BBB",
"Item_Value" : "456"
},
{
"Item_Name" : "CCC",
"Item_Value" : "789"
}
]
} |
希望展现成下面形式的二维表:
同样可以通过实现接口com.fr.plugin.db.mongo.expand.ColumnResolver来实现,示例代码如下:
Code Block | ||||
---|---|---|---|---|
| ||||
public class ArrayMapColumnResolver extends AbstractColumnResolver { @Override public boolean accept(Object cell) { return cell instanceof ArrayList && ((ArrayList) cell).get(0) instanceof Document; } @Override public List<FieldObject> expandColumn(String key, Object cell) { List<FieldObject> expand = new ArrayList<FieldObject>(); if (cell instanceof ArrayList) { Document document = (Document) ((ArrayList) cell).get(0); for (String name : document.keySet()) { expand.add(FieldObject.create(key, name)); } } return expand; } @Override public void expandData(Document doc, List<String> columnNames, Map<FieldObject, ColumnResolver> columnResolverMap, List<List<Object>> rowDataCollections, List<Object> rowData) { boolean expanded = false; List<List<Object>> rowList = new ArrayList<List<Object>>(); for (Map.Entry<FieldObject, ColumnResolver> entry : columnResolverMap.entrySet()) { if (!checkIfConditionMeet(entry.getValue())) { continue; } FieldObject fieldObject = entry.getKey(); List<Document> children = (ArrayList<Document>) doc.get(fieldObject.getOriginalName()); for (int i = 0, size = children.size(); i < size; i ++) { Document document = children.get(i); if (!expanded) { List<Object> row = new ArrayList<Object>(Arrays.asList(new Object[rowData.size()])); Collections.copy(row, rowData); row.set(fieldObject.getIndex(), document.get(fieldObject.getCurrentName())); rowDataCollections.add(row); rowList.add(row); } else { rowList.get(i).set(fieldObject.getIndex(), document.get(fieldObject.getCurrentName())); } } expanded = true; } } } |
Code Block |
---|
<dependence> <Item key="com.fr.solution.plugin.db.mongo" type="plugin"/> </dependence> <extra-core> <ColumnResolver class="com.fr.plugin.db.mongo.expand.impl.ArrayColumnResolver"/> </extra-core> |
同时安装MongoDB插件和列处理插件,就可以实现自已的要求了。
...