Search in sources :

Example 1 with GdbEdge

use of com.alibaba.datax.plugin.writer.gdbwriter.model.GdbEdge in project DataX by alibaba.

the class DefaultGdbMapper method forElement.

private static BiConsumer<Record, GdbElement> forElement(final MappingRule rule) {
    final boolean numPattern = rule.isNumPattern();
    final List<BiConsumer<Record, GdbElement>> properties = new ArrayList<>();
    for (final MappingRule.PropertyMappingRule propRule : rule.getProperties()) {
        final Function<Record, String> keyFunc = forStrColumn(numPattern, propRule.getKey());
        if (propRule.getValueType() == ValueType.STRING) {
            final Function<Record, String> valueFunc = forStrColumn(numPattern, propRule.getValue());
            properties.add((r, e) -> {
                e.addProperty(keyFunc.apply(r), valueFunc.apply(r), propRule.getPType());
            });
        } else {
            final Function<Record, Object> valueFunc = forObjColumn(numPattern, propRule.getValue(), propRule.getValueType());
            properties.add((r, e) -> {
                e.addProperty(keyFunc.apply(r), valueFunc.apply(r), propRule.getPType());
            });
        }
    }
    if (rule.getPropertiesJsonStr() != null) {
        final Function<Record, String> jsonFunc = forStrColumn(numPattern, rule.getPropertiesJsonStr());
        properties.add((r, e) -> {
            final String propertiesStr = jsonFunc.apply(r);
            final JSONObject root = (JSONObject) JSONObject.parse(propertiesStr);
            final JSONArray propertiesList = root.getJSONArray("properties");
            for (final Object object : propertiesList) {
                final JSONObject jsonObject = (JSONObject) object;
                final String key = jsonObject.getString("k");
                final String name = jsonObject.getString("v");
                final String type = jsonObject.getString("t");
                final String card = jsonObject.getString("c");
                if (key == null || name == null) {
                    continue;
                }
                addToProperties(e, key, name, type, card);
            }
        });
    }
    final BiConsumer<Record, GdbElement> ret = (r, e) -> {
        final String label = forStrColumn(numPattern, rule.getLabel()).apply(r);
        String id = forStrColumn(numPattern, rule.getId()).apply(r);
        if (rule.getImportType() == Key.ImportType.EDGE) {
            final String to = forStrColumn(numPattern, rule.getTo()).apply(r);
            final String from = forStrColumn(numPattern, rule.getFrom()).apply(r);
            if (to == null || from == null) {
                log.error("invalid record to: {} , from: {}", to, from);
                throw new IllegalArgumentException("to or from missed in edge");
            }
            ((GdbEdge) e).setTo(to);
            ((GdbEdge) e).setFrom(from);
            // generate UUID for edge
            if (id == null) {
                id = UUID.randomUUID().toString();
            }
        }
        if (id == null || label == null) {
            log.error("invalid record id: {} , label: {}", id, label);
            throw new IllegalArgumentException("id or label missed");
        }
        e.setId(id);
        e.setLabel(label);
        properties.forEach(p -> p.accept(r, e));
    };
    return ret;
}
Also used : VERTEX(com.alibaba.datax.plugin.writer.gdbwriter.Key.ImportType.VERTEX) UUID(java.util.UUID) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Configuration(com.alibaba.datax.common.util.Configuration) Key(com.alibaba.datax.plugin.writer.gdbwriter.Key) JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Matcher(java.util.regex.Matcher) Record(com.alibaba.datax.common.element.Record) GdbElement(com.alibaba.datax.plugin.writer.gdbwriter.model.GdbElement) GdbEdge(com.alibaba.datax.plugin.writer.gdbwriter.model.GdbEdge) BiConsumer(java.util.function.BiConsumer) GdbVertex(com.alibaba.datax.plugin.writer.gdbwriter.model.GdbVertex) JSONObject(com.alibaba.fastjson.JSONObject) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) Record(com.alibaba.datax.common.element.Record) JSONObject(com.alibaba.fastjson.JSONObject) BiConsumer(java.util.function.BiConsumer) GdbElement(com.alibaba.datax.plugin.writer.gdbwriter.model.GdbElement)

Aggregations

Record (com.alibaba.datax.common.element.Record)1 Configuration (com.alibaba.datax.common.util.Configuration)1 Key (com.alibaba.datax.plugin.writer.gdbwriter.Key)1 VERTEX (com.alibaba.datax.plugin.writer.gdbwriter.Key.ImportType.VERTEX)1 GdbEdge (com.alibaba.datax.plugin.writer.gdbwriter.model.GdbEdge)1 GdbElement (com.alibaba.datax.plugin.writer.gdbwriter.model.GdbElement)1 GdbVertex (com.alibaba.datax.plugin.writer.gdbwriter.model.GdbVertex)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Slf4j (lombok.extern.slf4j.Slf4j)1