Search in sources :

Example 1 with AtlasStruct

use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.

the class AtlasEntityGraphDiscoveryV1 method visitStruct.

void visitStruct(AtlasStructType structType, Object val) throws AtlasBaseException {
    if (structType == null || val == null) {
        return;
    }
    AtlasStruct struct;
    if (val instanceof AtlasStruct) {
        struct = (AtlasStruct) val;
    } else if (val instanceof Map) {
        Map attributes = AtlasTypeUtil.toStructAttributes((Map) val);
        struct = new AtlasStruct(structType.getTypeName(), attributes);
    } else {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_STRUCT_VALUE, val.toString());
    }
    for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
        AtlasType attrType = attribute.getAttributeType();
        Object attrVal = struct.getAttribute(attribute.getName());
        visitAttribute(attrType, attrVal);
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) Map(java.util.Map)

Example 2 with AtlasStruct

use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.

the class AtlasStructFormatConverter method fromV1ToV2.

@Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
    AtlasStruct ret = null;
    if (v1Obj != null) {
        AtlasStructType structType = (AtlasStructType) type;
        if (v1Obj instanceof Map) {
            final Map v1Map = (Map) v1Obj;
            final Map v1Attribs = (Map) v1Map.get(ATTRIBUTES_PROPERTY_KEY);
            if (MapUtils.isNotEmpty(v1Attribs)) {
                ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs, converterContext));
            } else {
                ret = new AtlasStruct(type.getTypeName());
            }
        } else if (v1Obj instanceof IStruct) {
            IStruct struct = (IStruct) v1Obj;
            Map<String, Object> v1Attribs = null;
            try {
                v1Attribs = struct.getValuesMap();
            } catch (AtlasException excp) {
                LOG.error("IStruct.getValuesMap() failed", excp);
            }
            ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs, converterContext));
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", v1Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasException(org.apache.atlas.AtlasException) HashMap(java.util.HashMap) Map(java.util.Map) IStruct(org.apache.atlas.typesystem.IStruct)

Example 3 with AtlasStruct

use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.

the class AtlasStructFormatConverter method fromV2ToV1.

@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
    Struct ret = null;
    if (v2Obj != null) {
        AtlasStructType structType = (AtlasStructType) type;
        if (v2Obj instanceof Map) {
            final Map v2Map = (Map) v2Obj;
            final Map v2Attribs;
            if (v2Map.containsKey(ATTRIBUTES_PROPERTY_KEY)) {
                v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY);
            } else {
                v2Attribs = v2Map;
            }
            if (MapUtils.isNotEmpty(v2Attribs)) {
                ret = new Struct(type.getTypeName(), fromV2ToV1(structType, v2Attribs, converterContext));
            } else {
                ret = new Struct(type.getTypeName());
            }
        } else if (v2Obj instanceof AtlasStruct) {
            AtlasStruct struct = (AtlasStruct) v2Obj;
            ret = new Struct(type.getTypeName(), fromV2ToV1(structType, struct.getAttributes(), converterContext));
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasStruct", v2Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasStructType(org.apache.atlas.type.AtlasStructType) HashMap(java.util.HashMap) Map(java.util.Map) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 4 with AtlasStruct

use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.

the class EntityGraphRetriever method mapVertexToStruct.

private AtlasStruct mapVertexToStruct(AtlasVertex entityVertex, String edgeLabel, AtlasEdge edge, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
    AtlasStruct ret = null;
    if (edge == null) {
        edge = graphHelper.getEdgeForLabel(entityVertex, edgeLabel);
    }
    if (GraphHelper.elementExists(edge)) {
        final AtlasVertex referenceVertex = edge.getInVertex();
        ret = new AtlasStruct(GraphHelper.getTypeName(referenceVertex));
        mapAttributes(referenceVertex, ret, entityExtInfo);
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 5 with AtlasStruct

use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.

the class AtlasEntityStoreV1Test method testArrayOfStructs.

@Test(dependsOnMethods = "testCreate")
public void testArrayOfStructs() throws Exception {
    //Modify array of structs
    AtlasEntity tableEntity = new AtlasEntity(tblEntity.getEntity());
    AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntitiesWithExtInfo(tableEntity);
    List<AtlasStruct> partitions = new ArrayList<AtlasStruct>() {

        {
            add(new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part1"));
            add(new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part2"));
        }
    };
    tableEntity.setAttribute("partitions", partitions);
    init();
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
    //add a new element to array of struct
    partitions.add(new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part3"));
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
    //remove one of the struct values
    init();
    partitions.remove(1);
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
    //Update struct value within array of struct
    init();
    partitions.get(0).setAttribute(TestUtilsV2.NAME, "part4");
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
    //add a repeated element to array of struct
    partitions.add(new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part4"));
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
    // Remove all elements. Should set array attribute to null
    partitions.clear();
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)21 Map (java.util.Map)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 HashMap (java.util.HashMap)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)6 Test (org.testng.annotations.Test)5 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)4 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)4 BeforeTest (org.testng.annotations.BeforeTest)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)3 AtlasStructType (org.apache.atlas.type.AtlasStructType)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 IStruct (org.apache.atlas.typesystem.IStruct)3 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)2 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)2 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)2