【仅供内部供应商使用,不提供对外解答和培训】

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

示例实现(对象类型)

面对这样的数据结构:

Code Block
title对象类型的数据结构
collapsetrue
{ 
    "value" : "word", 
    "name" : {
        "O" : "XXX", 
        "P" : "YYY"
    }
}

当需要把表格转换为二维表的时候,就需要处理name这样的Object类型的列,可以看出name是一个HashMap类型的结构,所以可以实现一个MapColumnResolver处理器,用于处理这一类的列:

Code Block
title处理对象类型的元素代码处理Map元素的示例代码
collapsetrue
import org.bson.Document;

import java.util.*;

public class MapColumnResolver extends AbstractColumnResolver {
    @Override
    public boolean accept(Object cell) {
        return cell instanceof Document;
    }

    @Override
    public void expandData(Document doc, List<String> columnNames, List<Integer> waitingColumnsIndex, 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) {
            Object data = doc.get(columnNames.get(index));
            if (data instanceof Document) {
                Document document = (Document)data;
                List<Object> array = new ArrayList<Object>();
                if (standard == null) {
                    for (Map.Entry<String, Object> entry : document.entrySet()) {
                        array.add(entry);
                    }
                    standard = array;
                }
                maxLength = Math.max(maxLength, array.size());
                group.put(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 (int index : waitingColumnsIndex) {
                    List<Object> array = group.get(index);
                    Map.Entry<String, Object> el = (Map.Entry<String, Object>)array.get(i);
                    row.set(index, array.size() > i ? el.getValue() : null);
                }
                rowDataCollections.add(row);
            }
        }
    }
}


注册方式

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插件和列处理插件,就可以实现自已的要求了。

...