Search in sources :

Example 21 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class EntityJerseyResourceIT method testDeleteTrait.

@Test
public void testDeleteTrait() throws Exception {
    String dbName = "db" + randomString();
    String tableName = "table" + randomString();
    Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(hiveDBInstance);
    Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    Id id = createInstance(hiveTableInstance);
    final String guid = id._getId();
    try {
        Assert.assertNotNull(UUID.fromString(guid));
    } catch (IllegalArgumentException e) {
        Assert.fail("Response is not a guid, " + guid);
    }
    String traitName = "PII_Trait" + randomString();
    TraitTypeDefinition piiTrait = TypesUtil.createTraitTypeDef(traitName, null, Collections.<String>emptySet());
    String traitDefinitionAsJSON = AtlasType.toV1Json(piiTrait);
    LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
    TypesDef typesDef = new TypesDef(Collections.emptyList(), Collections.emptyList(), Collections.singletonList(piiTrait), Collections.emptyList());
    createType(typesDef);
    Struct traitInstance = new Struct(traitName);
    atlasClientV1.addTrait(guid, traitInstance);
    assertEntityAudit(guid, EntityAuditEvent.EntityAuditAction.TAG_ADD);
    atlasClientV1.deleteTrait(guid, traitName);
    try {
        atlasClientV1.getTraitDefinition(guid, traitName);
        fail("Deleted trait definition shouldn't exist");
    } catch (AtlasServiceException e) {
        assertEquals(e.getStatus(), ClientResponse.Status.NOT_FOUND);
        assertEntityAudit(guid, EntityAuditEvent.EntityAuditAction.TAG_DELETE);
    }
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) AtlasServiceException(org.apache.atlas.AtlasServiceException) Id(org.apache.atlas.v1.model.instance.Id) Struct(org.apache.atlas.v1.model.instance.Struct) Test(org.testng.annotations.Test)

Example 22 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class EntityJerseyResourceIT method testGetTraitDefinitionForEntity.

@Test
public void testGetTraitDefinitionForEntity() throws Exception {
    String dbName = "db" + randomString();
    String tableName = "table" + randomString();
    Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbId = createInstance(hiveDBInstance);
    Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    Id id = createInstance(hiveTableInstance);
    final String guid = id._getId();
    try {
        Assert.assertNotNull(UUID.fromString(guid));
    } catch (IllegalArgumentException e) {
        Assert.fail("Response is not a guid, " + guid);
    }
    String traitName = "PII_Trait" + randomString();
    TraitTypeDefinition piiTrait = TypesUtil.createTraitTypeDef(traitName, null, Collections.<String>emptySet());
    TypesDef typesDef = new TypesDef(Collections.emptyList(), Collections.emptyList(), Collections.singletonList(piiTrait), Collections.emptyList());
    String traitDefinitionAsJSON = AtlasType.toV1Json(typesDef);
    LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
    createType(AtlasType.toV1Json(typesDef));
    Struct traitInstance = new Struct(traitName);
    atlasClientV1.addTrait(guid, traitInstance);
    Struct traitDef = atlasClientV1.getTraitDefinition(guid, traitName);
    Assert.assertEquals(traitDef.getTypeName(), traitName);
    List<Struct> allTraitDefs = atlasClientV1.listTraitDefinitions(guid);
    System.out.println(allTraitDefs.toString());
    Assert.assertEquals(allTraitDefs.size(), 8);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Id(org.apache.atlas.v1.model.instance.Id) Struct(org.apache.atlas.v1.model.instance.Struct) Test(org.testng.annotations.Test)

Example 23 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class BaseResourceIT method createHiveTableInstanceBuiltIn.

protected Referenceable createHiveTableInstanceBuiltIn(String dbName, String tableName, Id dbId) throws Exception {
    Map<String, Object> values = new HashMap<>();
    values.put(NAME, dbName);
    values.put(DESCRIPTION, "foo database");
    values.put(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, dbName);
    values.put("owner", "user1");
    values.put(CLUSTER_NAME, "cl1");
    values.put("parameters", Collections.EMPTY_MAP);
    values.put("location", "/tmp");
    Referenceable databaseInstance = new Referenceable(dbId._getId(), dbId.getTypeName(), values);
    Referenceable tableInstance = new Referenceable(HIVE_TABLE_TYPE_BUILTIN, CLASSIFICATION, PII_TAG, PHI_TAG, PCI_TAG, SOX_TAG, SEC_TAG, FINANCE_TAG);
    tableInstance.set(NAME, tableName);
    tableInstance.set(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, tableName);
    tableInstance.set("db", databaseInstance);
    tableInstance.set(DESCRIPTION, "bar table");
    tableInstance.set("lastAccessTime", "2014-07-11T08:00:00.000Z");
    tableInstance.set("type", "managed");
    tableInstance.set("level", 2);
    // enum
    tableInstance.set("tableType", 1);
    tableInstance.set("compressed", false);
    Struct traitInstance = (Struct) tableInstance.getTrait("classification");
    traitInstance.set("tag", "foundation_etl");
    Struct serde1Instance = new Struct("serdeType");
    serde1Instance.set(NAME, "serde1");
    serde1Instance.set("serde", "serde1");
    tableInstance.set("serde1", serde1Instance);
    Struct serde2Instance = new Struct("serdeType");
    serde2Instance.set(NAME, "serde2");
    serde2Instance.set("serde", "serde2");
    tableInstance.set("serde2", serde2Instance);
    List<String> traits = tableInstance.getTraitNames();
    Assert.assertEquals(traits.size(), 7);
    return tableInstance;
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 24 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class DataSetLineageJerseyResourceIT method testOutputsGraphForEntity.

@Test
public void testOutputsGraphForEntity() throws Exception {
    String tableId = atlasClientV1.getEntity(HIVE_TABLE_TYPE, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, salesFactTable).getId()._getId();
    ObjectNode results = atlasClientV1.getOutputGraphForEntity(tableId);
    Assert.assertNotNull(results);
    Struct resultsInstance = AtlasType.fromV1Json(results.toString(), Struct.class);
    Map<String, Object> vertices = (Map<String, Object>) resultsInstance.get("vertices");
    Assert.assertEquals(vertices.size(), 3);
    Object verticesObject = vertices.get(tableId);
    Struct vertex = null;
    if (verticesObject instanceof Map) {
        vertex = new Struct((Map) verticesObject);
    } else if (verticesObject instanceof Struct) {
        vertex = (Struct) verticesObject;
    }
    assertEquals(((Struct) vertex.get("vertexId")).get("state"), Id.EntityState.ACTIVE.name());
    Map<String, Struct> edges = (Map<String, Struct>) resultsInstance.get("edges");
    Assert.assertEquals(edges.size(), 4);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Map(java.util.Map) Struct(org.apache.atlas.v1.model.instance.Struct) Test(org.testng.annotations.Test)

Example 25 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class LineageUtils method toLineageStruct.

public static Struct toLineageStruct(AtlasLineageInfo lineageInfo, AtlasTypeRegistry registry) throws AtlasBaseException {
    Struct ret = new Struct();
    ret.setTypeName(Constants.TEMP_STRUCT_NAME_PREFIX + COUNTER.getAndIncrement());
    if (lineageInfo != null) {
        Map<String, AtlasEntityHeader> entities = lineageInfo.getGuidEntityMap();
        Set<AtlasLineageInfo.LineageRelation> relations = lineageInfo.getRelations();
        AtlasLineageInfo.LineageDirection direction = lineageInfo.getLineageDirection();
        Map<String, Struct> verticesMap = new HashMap<>();
        // Lineage Entities mapping -> verticesMap (vertices)
        for (String guid : entities.keySet()) {
            AtlasEntityHeader entityHeader = entities.get(guid);
            if (isDataSet(entityHeader.getTypeName(), registry)) {
                Map<String, Object> vertexIdMap = new HashMap<>();
                vertexIdMap.put(Constants.ATTRIBUTE_NAME_GUID, guid);
                vertexIdMap.put(Constants.ATTRIBUTE_NAME_STATE, (entityHeader.getStatus() == AtlasEntity.Status.ACTIVE) ? "ACTIVE" : "DELETED");
                vertexIdMap.put(Constants.ATTRIBUTE_NAME_TYPENAME, entityHeader.getTypeName());
                Object qualifiedName = entityHeader.getAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME);
                if (qualifiedName == null) {
                    qualifiedName = entityHeader.getDisplayText();
                }
                Map<String, Object> values = new HashMap<>();
                values.put(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, qualifiedName);
                values.put(VERTEX_ID_ATTR_NAME, constructResultStruct(vertexIdMap, true));
                values.put(AtlasClient.NAME, entityHeader.getDisplayText());
                verticesMap.put(guid, constructResultStruct(values, false));
            }
        }
        // Lineage Relations mapping -> edgesMap (edges)
        Map<String, List<String>> edgesMap = new HashMap<>();
        for (AtlasLineageInfo.LineageRelation relation : relations) {
            String fromEntityId = relation.getFromEntityId();
            String toEntityId = relation.getToEntityId();
            if (direction == AtlasLineageInfo.LineageDirection.INPUT) {
                if (!edgesMap.containsKey(toEntityId)) {
                    edgesMap.put(toEntityId, new ArrayList<String>());
                }
                edgesMap.get(toEntityId).add(fromEntityId);
            } else if (direction == AtlasLineageInfo.LineageDirection.OUTPUT) {
                if (!edgesMap.containsKey(fromEntityId)) {
                    edgesMap.put(fromEntityId, new ArrayList<String>());
                }
                edgesMap.get(fromEntityId).add(toEntityId);
            }
        }
        ret.set("vertices", verticesMap);
        ret.set("edges", edgesMap);
    }
    return ret;
}
Also used : AtlasLineageInfo(org.apache.atlas.model.lineage.AtlasLineageInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Struct(org.apache.atlas.v1.model.instance.Struct) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Struct (org.apache.atlas.v1.model.instance.Struct)36 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)22 Test (org.testng.annotations.Test)17 HashMap (java.util.HashMap)9 Map (java.util.Map)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)9 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)7 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)6 Id (org.apache.atlas.v1.model.instance.Id)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)3 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasServiceException (org.apache.atlas.AtlasServiceException)2 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 EntityChangeListenerV2 (org.apache.atlas.listener.EntityChangeListenerV2)2