Search in sources :

Example 11 with AtlasStruct

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

the class BaseResourceIT method createHiveTableInstanceV2.

protected AtlasEntity createHiveTableInstanceV2(AtlasEntity databaseInstance, String tableName) throws Exception {
    AtlasEntity tableInstance = new AtlasEntity(HIVE_TABLE_TYPE_V2);
    tableInstance.setClassifications(Arrays.asList(new AtlasClassification("classification"), new AtlasClassification("pii"), new AtlasClassification("phi"), new AtlasClassification("pci"), new AtlasClassification("sox"), new AtlasClassification("sec"), new AtlasClassification("finance")));
    tableInstance.setAttribute(NAME, tableName);
    tableInstance.setAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, tableName);
    tableInstance.setAttribute("db", AtlasTypeUtil.getAtlasObjectId(databaseInstance));
    tableInstance.setAttribute(DESCRIPTION, "bar table");
    tableInstance.setAttribute("lastAccessTime", "2014-07-11T08:00:00.000Z");
    tableInstance.setAttribute("type", "managed");
    tableInstance.setAttribute("level", 2);
    // enum
    tableInstance.setAttribute("tableType", "MANAGED");
    tableInstance.setAttribute("compressed", false);
    AtlasClassification classification = tableInstance.getClassifications().get(0);
    classification.setAttribute("tag", "foundation_etl");
    AtlasStruct serde1Instance = new AtlasStruct("serdeType");
    serde1Instance.setAttribute(NAME, "serde1");
    serde1Instance.setAttribute("serde", "serde1");
    tableInstance.setAttribute("serde1", serde1Instance);
    AtlasStruct serde2Instance = new AtlasStruct("serdeType");
    serde2Instance.setAttribute(NAME, "serde2");
    serde2Instance.setAttribute("serde", "serde2");
    tableInstance.setAttribute("serde2", serde2Instance);
    List<AtlasClassification> traits = tableInstance.getClassifications();
    Assert.assertEquals(traits.size(), 7);
    return tableInstance;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification)

Example 12 with AtlasStruct

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

the class ModelTestUtil method newStruct.

public static AtlasStruct newStruct(AtlasStructDef structDef, AtlasTypeRegistry typesRegistry) {
    AtlasStruct ret = null;
    AtlasStructType structType = typesRegistry.getStructTypeByName(structDef.getName());
    if (structType != null) {
        ret = structType.createDefaultValue();
    } else {
        LOG.error("failed to get struct-type {}", structDef.getName());
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasStructType(org.apache.atlas.type.AtlasStructType)

Example 13 with AtlasStruct

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

the class TestAtlasStructType method testStructTypeDefaultValue.

@Test
public void testStructTypeDefaultValue() {
    AtlasStruct defValue = structType.createDefaultValue();
    assertNotNull(defValue);
    assertEquals(defValue.getTypeName(), structType.getTypeName());
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) Test(org.testng.annotations.Test)

Example 14 with AtlasStruct

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

the class FullTextMapperV2 method mapAttribute.

private void mapAttribute(Object value, AtlasEntityExtInfo entityExtInfo, StringBuilder sb, Set<String> processedGuids) throws AtlasBaseException {
    if (value instanceof AtlasObjectId) {
        if (followReferences) {
            AtlasObjectId objectId = (AtlasObjectId) value;
            AtlasEntity entity = entityExtInfo.getEntity(objectId.getGuid());
            if (entity != null) {
                map(entity, entityExtInfo, sb, processedGuids);
            }
        }
    } else if (value instanceof List) {
        List valueList = (List) value;
        for (Object listElement : valueList) {
            mapAttribute(listElement, entityExtInfo, sb, processedGuids);
        }
    } else if (value instanceof Map) {
        Map valueMap = (Map) value;
        for (Object key : valueMap.keySet()) {
            mapAttribute(key, entityExtInfo, sb, processedGuids);
            mapAttribute(valueMap.get(key), entityExtInfo, sb, processedGuids);
        }
    } else if (value instanceof Enum) {
        Enum enumValue = (Enum) value;
        sb.append(enumValue.name()).append(FULL_TEXT_DELIMITER);
    } else if (value instanceof AtlasStruct) {
        AtlasStruct atlasStruct = (AtlasStruct) value;
        for (Map.Entry<String, Object> entry : atlasStruct.getAttributes().entrySet()) {
            sb.append(entry.getKey()).append(FULL_TEXT_DELIMITER);
            mapAttribute(entry.getValue(), entityExtInfo, sb, processedGuids);
        }
    } else {
        sb.append(String.valueOf(value)).append(FULL_TEXT_DELIMITER);
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) List(java.util.List) Map(java.util.Map)

Example 15 with AtlasStruct

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

the class AtlasDeleteHandlerV1Test method testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes.

/**
     * Verify deleting entities that are the target of a unidirectional class array reference
     * from a struct or trait instance.
     */
@Test
public void testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes() throws Exception {
    // Define class types.
    AtlasStructDef.AtlasAttributeDef[] structTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structTargetDef = new AtlasEntityDef("StructTarget", "StructTarget_description", "1.0", Arrays.asList(structTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] traitTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef traitTargetDef = new AtlasEntityDef("TraitTarget", "TraitTarget_description", "1.0", Arrays.asList(traitTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] structContainerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("struct", "TestStruct", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structContainerDef = new AtlasEntityDef("StructContainer", "StructContainer_description", "1.0", Arrays.asList(structContainerAttributes), Collections.<String>emptySet());
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] structDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<StructTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("nestedStructs", "array<NestedStruct>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef structDef = new AtlasStructDef("TestStruct", "TestStruct_desc", "1.0", Arrays.asList(structDefAttributes));
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] nestedStructDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef nestedStructDef = new AtlasStructDef("NestedStruct", "NestedStruct_desc", "1.0", Arrays.asList(nestedStructDefAttributes));
    AtlasStructDef.AtlasAttributeDef[] traitDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasClassificationDef traitDef = new AtlasClassificationDef("TestTrait", "TestTrait_desc", "1.0", Arrays.asList(traitDefAttributes));
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(structDef, nestedStructDef), ImmutableList.<AtlasClassificationDef>of(traitDef), ImmutableList.<AtlasEntityDef>of(structTargetDef, traitTargetDef, structContainerDef));
    typeDefStore.createTypesDef(typesDef);
    // Create instances of class, struct, and trait types.
    final AtlasEntity structTargetEntity = new AtlasEntity("StructTarget");
    final AtlasEntity traitTargetEntity = new AtlasEntity("TraitTarget");
    final AtlasEntity structContainerEntity = new AtlasEntity("StructContainer");
    AtlasStruct structInstance = new AtlasStruct("TestStruct");
    AtlasStruct nestedStructInstance = new AtlasStruct("NestedStruct");
    Struct traitInstance = new Struct("TestTrait");
    structContainerEntity.setAttribute("struct", structInstance);
    structInstance.setAttribute("target", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(structTargetEntity)));
    structInstance.setAttribute("nestedStructs", ImmutableList.of(nestedStructInstance));
    AtlasEntity.AtlasEntitiesWithExtInfo structCreationObj = new AtlasEntity.AtlasEntitiesWithExtInfo();
    structCreationObj.addEntity(structContainerEntity);
    structCreationObj.addEntity(traitTargetEntity);
    structCreationObj.addReferredEntity(structTargetEntity);
    init();
    AtlasEntityStream entityStream = new AtlasEntityStream(structCreationObj);
    EntityMutationResponse response = entityStore.createOrUpdate(entityStream, false);
    Assert.assertEquals(response.getCreatedEntities().size(), 3);
    final List<String> structTarget = metadataService.getEntityList("StructTarget");
    Assert.assertEquals(structTarget.size(), 1);
    final String structTargetGuid = structTarget.get(0);
    final List<String> traitTarget = metadataService.getEntityList("TraitTarget");
    Assert.assertEquals(traitTarget.size(), 1);
    final String traitTargetGuid = traitTarget.get(0);
    final List<String> structContainerTarget = metadataService.getEntityList("StructContainer");
    Assert.assertEquals(structContainerTarget.size(), 1);
    String structContainerGuid = structContainerTarget.get(0);
    // Add TestTrait to StructContainer instance
    traitInstance.set("target", ImmutableList.of(new Id(traitTargetGuid, 0, "TraitTarget")));
    TraitType traitType = typeSystem.getDataType(TraitType.class, "TestTrait");
    ITypedStruct convertedTrait = traitType.convert(traitInstance, Multiplicity.REQUIRED);
    metadataService.addTrait(structContainerGuid, convertedTrait);
    // Verify that the unidirectional references from the struct and trait instances
    // are pointing at the target entities.
    final ITypedReferenceableInstance structContainerConvertedEntity = metadataService.getEntityDefinition(structContainerGuid);
    Object object = structContainerConvertedEntity.get("struct");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof ITypedStruct);
    ITypedStruct struct = (ITypedStruct) object;
    object = struct.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), structTargetGuid);
    IStruct trait = structContainerConvertedEntity.getTrait("TestTrait");
    Assert.assertNotNull(trait);
    object = trait.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), traitTargetGuid);
    init();
    // Delete the entities that are targets of the struct and trait instances.
    EntityMutationResponse entityResult = entityStore.deleteByIds(new ArrayList<String>() {

        {
            add(structTargetGuid);
            add(traitTargetGuid);
        }
    });
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(extractGuids(entityResult.getDeletedEntities()).containsAll(Arrays.asList(structTargetGuid, traitTargetGuid)));
    assertEntityDeleted(structTargetGuid);
    assertEntityDeleted(traitTargetGuid);
    assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(structContainerGuid);
    init();
    // Delete the entity which contains nested structs and has the TestTrait trait.
    entityResult = entityStore.deleteById(structContainerGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(extractGuids(entityResult.getDeletedEntities()).contains(structContainerGuid));
    assertEntityDeleted(structContainerGuid);
    // Verify all TestStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestStruct"));
    // Verify all NestedStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "NestedStruct"));
    // Verify all TestTrait trait vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestTrait"));
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) IStruct(org.apache.atlas.typesystem.IStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) IStruct(org.apache.atlas.typesystem.IStruct) 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