Search in sources :

Example 41 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class XmlFieldReader method getFieldsForPath.

private List<Field> getFieldsForPath(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, Element node, Field field, XmlPath path, int depth) throws AtlasException {
    List<Field> fields = new ArrayList<>();
    List<XmlSegmentContext> segments = path.getXmlSegments(false);
    if (segments.size() < depth) {
        throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
    }
    if (segments.size() == depth) {
        if (!(field instanceof XmlEnumField) && field.getFieldType() == FieldType.COMPLEX) {
            FieldGroup group = (FieldGroup) field;
            populateChildFields(session, xmlNamespaces, node, group, path);
            fields.add(group);
        } else {
            XmlField xmlField = new XmlField();
            AtlasXmlModelFactory.copyField(field, xmlField, true);
            if (field instanceof XmlEnumField && xmlField.getFieldType() == FieldType.COMPLEX) {
                // enum has COMPLEX by default
                xmlField.setFieldType(FieldType.STRING);
            }
            copyValue(session, xmlNamespaces, segments.get(depth - 1), node, xmlField);
            // reset index for subfields
            xmlField.setIndex(null);
            fields.add(xmlField);
        }
        return fields;
    }
    // segments.size() > depth
    XmlSegmentContext segment = segments.get(depth);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Now processing segment: " + segment.getName());
    }
    if (depth == 0) {
        if (segment.getName().startsWith(XmlIOHelper.getNodeNameWithoutNamespaceAlias(node))) {
            Optional<String> rootNamespace = Optional.empty();
            if (segment.getNamespace() != null) {
                rootNamespace = getNamespace(xmlNamespaces, segment.getNamespace());
            }
            if (!rootNamespace.isPresent() || rootNamespace.get().equals(node.getNamespaceURI())) {
                // "/XOA/contact<>/firstName", skip.
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping root segment: " + segment);
                }
                if (segments.size() > 1) {
                    depth = 1;
                    segment = segments.get(depth);
                }
            }
        }
    }
    if (segment.isAttribute() && segments.size() == depth + 1) {
        // if last segment is attribute
        List<Field> attrFields = getFieldsForPath(session, xmlNamespaces, node, field, path, depth + 1);
        fields.addAll(attrFields);
        return fields;
    }
    String fieldName = segment.getName();
    String fieldNamespace = segment.getNamespace();
    Optional<String> namespace = getNamespace(xmlNamespaces, fieldNamespace);
    List<Element> children = XmlIOHelper.getChildrenWithNameStripAlias(fieldName, namespace, node);
    if (children == null || children.isEmpty()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Skipping source value set, couldn't find children with name '" + fieldName + "', for segment: " + segment);
        }
        return fields;
    }
    if (segment.getCollectionType() == CollectionType.NONE) {
        List<Field> childFields = getFieldsForPath(session, xmlNamespaces, children.get(0), field, path, depth + 1);
        fields.addAll(childFields);
        return fields;
    }
    // collection
    Integer index = segment.getCollectionIndex();
    if (index != null) {
        if (index < children.size()) {
            List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(index), field, path, depth + 1);
            fields.addAll(arrayFields);
        } else if (LOG.isDebugEnabled()) {
            LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
        }
    } else {
        // if index not included, iterate over all
        for (int i = 0; i < children.size(); i++) {
            Field itemField;
            if (field instanceof FieldGroup) {
                itemField = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
                AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth + 1, i);
            } else {
                itemField = AtlasXmlModelFactory.cloneField((XmlField) field, false);
                AtlasPath itemPath = new AtlasPath(field.getPath());
                itemPath.setCollectionIndex(depth + 1, i);
                itemField.setPath(itemPath.toString());
            }
            List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(i), itemField, new XmlPath(itemField.getPath()), depth + 1);
            fields.addAll(arrayFields);
        }
    }
    return fields;
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) XmlField(io.atlasmap.xml.v2.XmlField) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) AtlasException(io.atlasmap.api.AtlasException) XmlSegmentContext(io.atlasmap.xml.core.XmlPath.XmlSegmentContext) Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) XmlEnumField(io.atlasmap.xml.v2.XmlEnumField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 42 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class KafkaConnectFieldReader method populateCollectionItems.

private FieldGroup populateCollectionItems(AtlasInternalSession session, List<Object> values, Field field) throws AtlasException {
    FieldGroup group = AtlasModelFactory.createFieldGroupFrom(field, true);
    for (int i = 0; i < values.size(); i++) {
        AtlasPath itemPath = new AtlasPath(group.getPath());
        List<SegmentContext> segments = itemPath.getSegments(true);
        itemPath.setCollectionIndex(segments.size() - 1, i);
        if (field instanceof FieldGroup) {
            FieldGroup itemGroup = AtlasKafkaConnectModelFactory.cloneFieldGroup((FieldGroup) field);
            AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size() - 1, i);
            populateChildFields(session, (Struct) values.get(i), itemGroup);
            group.getField().add(itemGroup);
        } else {
            KafkaConnectField itemField = AtlasKafkaConnectModelFactory.cloneField((KafkaConnectField) field, false);
            itemField.setPath(itemPath.toString());
            Object converted = conversionService.convertType(values.get(i), itemField.getFormat(), itemField.getFieldType(), null);
            itemField.setValue(converted);
            group.getField().add(itemField);
        }
    }
    return group;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField)

Example 43 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class KafkaConnectFieldReader method nestComplexCollection.

private void nestComplexCollection(AtlasInternalSession session, List<Object> collection, FieldGroup parent, int depth) throws AtlasException {
    AtlasPath path = new AtlasPath(parent.getPath());
    SegmentContext segment = path.getSegments(true).get(depth);
    if (segment.getCollectionIndex() != null) {
        int index = segment.getCollectionIndex();
        Struct struct = (Struct) collection.get(index);
        populateChildFields(session, struct, parent);
        return;
    }
    List<Field> processed = new LinkedList<>();
    for (int index = 0; index < collection.size(); index++) {
        FieldGroup itemGroup = AtlasKafkaConnectModelFactory.cloneFieldGroup(parent);
        AtlasPath.setCollectionIndexRecursively(itemGroup, depth, index);
        processed.add(itemGroup);
        Struct struct = (Struct) collection.get(index);
        populateChildFields(session, struct, itemGroup);
    }
    parent.getField().clear();
    parent.getField().addAll(processed);
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) LinkedList(java.util.LinkedList) Struct(org.apache.kafka.connect.data.Struct)

Example 44 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class KafkaConnectInspector method createDocument.

private KafkaConnectDocument createDocument(org.apache.kafka.connect.data.Schema schema) {
    KafkaConnectDocument doc = AtlasKafkaConnectModelFactory.createKafkaConnectDocument();
    doc.setRootSchemaType(schema.type());
    doc.setName(schema.name());
    Schema connectSchema = schema;
    AtlasPath path;
    if (Type.ARRAY == connectSchema.type()) {
        path = new AtlasPath(AtlasPath.PATH_SEPARATOR + AtlasPath.PATH_LIST_SUFFIX);
        doc.setCollectionType(CollectionType.LIST);
        connectSchema = connectSchema.valueSchema();
    } else if (Type.MAP == connectSchema.type()) {
        path = new AtlasPath(AtlasPath.PATH_SEPARATOR + AtlasPath.PATH_MAP_SUFFIX);
        doc.setCollectionType(CollectionType.MAP);
        connectSchema = connectSchema.valueSchema();
    } else {
        path = new AtlasPath("");
    }
    doc.setPath(path.toString());
    if (connectSchema.parameters() != null) {
        doc.setEnumeration(true);
        List<KafkaConnectEnumField> symbols = doc.getEnumFields().getKafkaConnectEnumField();
        for (Entry<String, String> entry : connectSchema.parameters().entrySet()) {
            if ("io.confluent".equals(entry.getKey())) {
                continue;
            }
            KafkaConnectEnumField f = new KafkaConnectEnumField();
            f.setName(entry.getValue());
            symbols.add(f);
        }
        doc.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
    } else if (!connectSchema.type().isPrimitive()) {
        doc.setFieldType(FieldType.COMPLEX);
        List<KafkaConnectField> children = populateFields(connectSchema.fields(), path);
        doc.getFields().getField().addAll(children);
    } else {
        doc.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
    }
    return doc;
}
Also used : Schema(org.apache.kafka.connect.data.Schema) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectEnumField(io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField) ArrayList(java.util.ArrayList) List(java.util.List) KafkaConnectDocument(io.atlasmap.kafkaconnect.v2.KafkaConnectDocument)

Example 45 with AtlasPath

use of io.atlasmap.core.AtlasPath in project atlasmap by atlasmap.

the class KafkaConnectInspector method populateFields.

private List<KafkaConnectField> populateFields(List<org.apache.kafka.connect.data.Field> kcFields, AtlasPath parentPath) {
    List<KafkaConnectField> answer = new ArrayList<>();
    for (org.apache.kafka.connect.data.Field connectField : kcFields) {
        KafkaConnectField field;
        AtlasPath path = parentPath.clone();
        CollectionType collectionType = CollectionType.NONE;
        Schema connectSchema = connectField.schema();
        if (Type.ARRAY == connectSchema.type()) {
            path.appendField(connectField.name() + AtlasPath.PATH_LIST_SUFFIX);
            collectionType = CollectionType.LIST;
            connectSchema = connectSchema.valueSchema();
        } else if (Type.MAP == connectSchema.type()) {
            path.appendField(connectField.name() + AtlasPath.PATH_MAP_SUFFIX);
            collectionType = CollectionType.MAP;
            connectSchema = connectSchema.valueSchema();
        } else {
            path.appendField(connectField.name());
        }
        if (connectSchema.parameters() != null) {
            KafkaConnectComplexType complex = AtlasKafkaConnectModelFactory.createKafkaConnectComplexType();
            complex.setKafkaConnectEnumFields(new KafkaConnectEnumFields());
            complex.setEnumeration(true);
            List<KafkaConnectEnumField> symbols = complex.getKafkaConnectEnumFields().getKafkaConnectEnumField();
            boolean first = true;
            for (Entry<String, String> entry : connectSchema.parameters().entrySet()) {
                // FIXME dirty hack - it seems the first entry is for some control, the others are the actual values
                if (first) {
                    first = false;
                    continue;
                }
                KafkaConnectEnumField f = new KafkaConnectEnumField();
                f.setName(entry.getValue());
                symbols.add(f);
            }
            field = complex;
        } else if (connectSchema.type().isPrimitive()) {
            field = AtlasKafkaConnectModelFactory.createKafkaConnectField();
            field.setFieldType(KafkaConnectUtil.getFieldType(connectSchema.type()));
        } else {
            KafkaConnectComplexType complex = AtlasKafkaConnectModelFactory.createKafkaConnectComplexType();
            List<KafkaConnectField> children = populateFields(connectSchema.fields(), path);
            if ("io.confluent.connect.avro.Union".equals(connectSchema.name())) {
                // We don't support union until it's built into Kafka Connect Schema
                complex.setStatus(FieldStatus.UNSUPPORTED);
            }
            complex.getKafkaConnectFields().getKafkaConnectField().addAll(children);
            field = complex;
        }
        field.setName(connectField.name());
        field.setPath(path.toString());
        field.setCollectionType(collectionType);
        answer.add(field);
    }
    return answer;
}
Also used : KafkaConnectComplexType(io.atlasmap.kafkaconnect.v2.KafkaConnectComplexType) KafkaConnectEnumFields(io.atlasmap.kafkaconnect.v2.KafkaConnectEnumFields) Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) CollectionType(io.atlasmap.v2.CollectionType) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectEnumField(io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField) ArrayList(java.util.ArrayList) List(java.util.List) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField)

Aggregations

AtlasPath (io.atlasmap.core.AtlasPath)49 Field (io.atlasmap.v2.Field)27 FieldGroup (io.atlasmap.v2.FieldGroup)26 AtlasException (io.atlasmap.api.AtlasException)17 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)14 JavaField (io.atlasmap.java.v2.JavaField)12 ArrayList (java.util.ArrayList)12 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)11 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)11 JsonField (io.atlasmap.json.v2.JsonField)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)4 Method (java.lang.reflect.Method)4 List (java.util.List)4 Test (org.junit.Test)4 SourceAddress (io.atlasmap.java.test.SourceAddress)3 SourceOrder (io.atlasmap.java.test.SourceOrder)3 KafkaConnectEnumField (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField)3 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)3 Head (io.atlasmap.spi.AtlasInternalSession.Head)3