Java Code Examples for org.apache.atlas.type.AtlasType#fromV1Json()

The following examples show how to use org.apache.atlas.type.AtlasType#fromV1Json() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityJerseyResourceIT.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testUTF8() throws Exception {
    String              attrName            = randomUTF8();
    String              attrValue           = randomUTF8();
    String              classType           = randomString(); //Type names cannot be arbitrary UTF8 characters. See org.apache.atlas.type.AtlasTypeUtil#validateType()
    ClassTypeDefinition classTypeDefinition = TypesUtil.createClassTypeDef(classType, null, Collections.<String>emptySet(), TypesUtil.createUniqueRequiredAttrDef(attrName, AtlasBaseTypeDef.ATLAS_TYPE_STRING));
    TypesDef            typesDef            = new TypesDef(Collections.<EnumTypeDefinition>emptyList(), Collections.<StructTypeDefinition>emptyList(), Collections.<TraitTypeDefinition>emptyList(), Collections.singletonList(classTypeDefinition));

    createType(typesDef);

    Referenceable entityToCreate  = new Referenceable(classType, Collections.singletonMap(attrName, attrValue));
    Id            guid            = createInstance(entityToCreate);
    ObjectNode    response        = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API_V1.GET_ENTITY, null, guid._getId());
    Object        objResponse     = response.get(AtlasClient.DEFINITION);
    String        jsonResponse    = AtlasType.toJson(objResponse);
    Referenceable createdEntity   = AtlasType.fromV1Json(jsonResponse, Referenceable.class);
    Object        entityAttrValue = createdEntity.get(attrName);

    Assert.assertEquals(entityAttrValue, attrValue,
                        "attrName=" + attrName + "; attrValue=" + attrValue + "; entityToCreate=" + entityToCreate + "; entityId=" + guid + "; getEntityResponse_Obj=" + objResponse + "; getEntityResponse_Json=" + jsonResponse + "; getEntityResponse_Entity=" + createdEntity);
}
 
Example 2
Source File: DataSetLineageJerseyResourceIT.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testInputsGraph() throws Exception {
    String     tableId  = atlasClientV1.getEntity(HIVE_TABLE_TYPE, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, salesMonthlyTable).getId()._getId();
    ObjectNode response = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API_V1.LINEAGE_INPUTS_GRAPH, null, tableId, "/inputs/graph");

    Assert.assertNotNull(response);
    System.out.println("inputs graph = " + response);

    Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));

    JsonNode results = response.get(AtlasClient.RESULTS);
    Assert.assertNotNull(results);

    Struct resultsInstance = AtlasType.fromV1Json(results.toString(), Struct.class);
    Map<String, Struct> vertices = (Map<String, Struct>) resultsInstance.get("vertices");
    Assert.assertEquals(vertices.size(), 4);

    Map<String, Struct> edges = (Map<String, Struct>) resultsInstance.get("edges");
    Assert.assertEquals(edges.size(), 4);
}
 
Example 3
Source File: DataSetLineageJerseyResourceIT.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testOutputsGraph() throws Exception {
    String     tableId  = atlasClientV1.getEntity(HIVE_TABLE_TYPE, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, salesFactTable).getId()._getId();
    ObjectNode response = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API_V1.LINEAGE_INPUTS_GRAPH, null, tableId, "/outputs/graph");

    Assert.assertNotNull(response);
    System.out.println("outputs graph= " + response);

    Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));

    JsonNode results = response.get(AtlasClient.RESULTS);
    Assert.assertNotNull(results);

    Struct resultsInstance = AtlasType.fromV1Json(results.toString(), Struct.class);
    Map<String, Struct> vertices = (Map<String, Struct>) resultsInstance.get("vertices");
    Assert.assertEquals(vertices.size(), 3);

    Map<String, Struct> edges = (Map<String, Struct>) resultsInstance.get("edges");
    Assert.assertEquals(edges.size(), 4);
}
 
Example 4
Source File: DataSetLineageJerseyResourceIT.java    From atlas with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: AtlasInstanceConverter.java    From atlas with Apache License 2.0 6 votes vote down vote up
public AtlasEntitiesWithExtInfo toAtlasEntities(String[] jsonEntities) throws AtlasBaseException, AtlasException {
    Referenceable[] referenceables = new Referenceable[jsonEntities.length];

    for (int i = 0; i < jsonEntities.length; i++) {
        referenceables[i] = AtlasType.fromV1Json(jsonEntities[i], Referenceable.class);
    }

    AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) instanceFormatters.getConverter(TypeCategory.ENTITY);
    ConverterContext           context   = new ConverterContext();

    for (Referenceable referenceable : referenceables) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());

        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
        }

        AtlasEntity entity = converter.fromV1ToV2(referenceable, entityType, context);

        context.addEntity(entity);
    }

    AtlasEntitiesWithExtInfo ret = context.getEntities();

    return ret;
}
 
Example 6
Source File: AtlasClient.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * Get an entity given the entity id
 * @param entityType entity type name
 * @param attribute qualified name of the entity
 * @param value
 * @return result object
 * @throws AtlasServiceException
 */
public Referenceable getEntity(final String entityType, final String attribute, final String value)
        throws AtlasServiceException {
    final API api = API_V1.GET_ENTITY;
    ObjectNode jsonResponse = callAPIWithRetries(api, null, new ResourceCreator() {
        @Override
        public WebResource createResource() {
            WebResource resource = getResource(api);
            resource = resource.queryParam(TYPE, entityType);
            resource = resource.queryParam(ATTRIBUTE_NAME, attribute);
            resource = resource.queryParam(ATTRIBUTE_VALUE, value);
            return resource;
        }
    });
    String entityInstanceDefinition =  AtlasType.toJson(jsonResponse.get(AtlasClient.DEFINITION));
    return AtlasType.fromV1Json(entityInstanceDefinition, Referenceable.class);
}
 
Example 7
Source File: AbstractNotificationTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void assertEqualsMessageJson(String msgJsonActual, String msgJsonExpected) {
    Map<Object, Object> msgActual   = AtlasType.fromV1Json(msgJsonActual, Map.class);
    Map<Object, Object> msgExpected = AtlasType.fromV1Json(msgJsonExpected, Map.class);

    msgActual.remove("msgCreationTime");
    msgExpected.remove("msgCreationTime");

    assertEquals(msgActual, msgExpected);
}
 
Example 8
Source File: TypesJerseyResourceIT.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = "testSubmit")
public void testGetDefinition() throws Exception {
    for (HierarchicalTypeDefinition typeDefinition : typeDefinitions) {
        System.out.println("typeName = " + typeDefinition.getTypeName());

        ObjectNode response = atlasClientV1.callAPIWithBodyAndParams(AtlasClient.API_V1.LIST_TYPES, null, typeDefinition.getTypeName());

        Assert.assertNotNull(response);
        Assert.assertNotNull(response.get(AtlasClient.DEFINITION));
        Assert.assertNotNull(response.get(AtlasClient.REQUEST_ID));

        TypesDef typesDef = AtlasType.fromV1Json(AtlasType.toJson(response.get(AtlasClient.DEFINITION)), TypesDef.class);

        List<? extends HierarchicalTypeDefinition> hierarchicalTypeDefs = Collections.emptyList();

        if (typeDefinition instanceof ClassTypeDefinition) {
            hierarchicalTypeDefs = typesDef.getClassTypes();
        } else if (typeDefinition instanceof TraitTypeDefinition) {
            hierarchicalTypeDefs = typesDef.getTraitTypes();
        }

        for (HierarchicalTypeDefinition hierarchicalTypes : hierarchicalTypeDefs) {
            for (AttributeDefinition attrDef : hierarchicalTypes.getAttributeDefinitions()) {
                if (NAME.equals(attrDef.getName())) {
                    assertEquals(attrDef.getIsIndexable(), true);
                    assertEquals(attrDef.getIsUnique(), true);
                }
            }
        }
    }
}
 
Example 9
Source File: AtlasClient.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * Get all trait definitions for an entity
 * @param guid GUID of the entity
 * @return List<String> trait definitions of the traits associated to the entity
 * @throws AtlasServiceException
 */
public List<Struct> listTraitDefinitions(final String guid) throws AtlasServiceException {
    ObjectNode        jsonResponse    = callAPIWithBodyAndParams(API_V1.GET_ALL_TRAIT_DEFINITIONS, null, guid, TRAIT_DEFINITIONS);
    List<ObjectNode>  traitDefList    = extractResults(jsonResponse, AtlasClient.RESULTS, new ExtractOperation<ObjectNode, ObjectNode>());
    ArrayList<Struct> traitStructList = new ArrayList<>();
    for (ObjectNode traitDef : traitDefList) {
        Struct traitStruct = AtlasType.fromV1Json(traitDef.toString(), Struct.class);
        traitStructList.add(traitStruct);
    }
    return traitStructList;
}
 
Example 10
Source File: TypeConverterUtil.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static AtlasTypesDef toAtlasTypesDef(String typeDefinition, AtlasTypeRegistry registry) throws AtlasBaseException {
    AtlasTypesDef ret = new AtlasTypesDef();

    try {
        if (StringUtils.isEmpty(typeDefinition)) {
            throw new AtlasBaseException(INVALID_TYPE_DEFINITION, typeDefinition);
        }

        TypesDef typesDef = AtlasType.fromV1Json(typeDefinition, TypesDef.class);
        if (CollectionUtils.isNotEmpty(typesDef.getEnumTypes())) {
            List<AtlasEnumDef> enumDefs = toAtlasEnumDefs(typesDef.getEnumTypes());
            ret.setEnumDefs(enumDefs);
        }

        if (CollectionUtils.isNotEmpty(typesDef.getStructTypes())) {
            List<AtlasStructDef> structDefs = toAtlasStructDefs(typesDef.getStructTypes());
            ret.setStructDefs(structDefs);
        }

        if (CollectionUtils.isNotEmpty(typesDef.getClassTypes())) {
            List<AtlasEntityDef> entityDefs = toAtlasEntityDefs(typesDef.getClassTypes(), registry);
            ret.setEntityDefs(entityDefs);
        }

        if (CollectionUtils.isNotEmpty(typesDef.getTraitTypes())) {
            List<AtlasClassificationDef> classificationDefs = toAtlasClassificationDefs(typesDef.getTraitTypes());
            ret.setClassificationDefs(classificationDefs);
        }

    } catch (Exception e) {
        LOG.error("Invalid type definition = {}", typeDefinition, e);
        throw new AtlasBaseException(INVALID_TYPE_DEFINITION, typeDefinition);
    }

    return ret;
}
 
Example 11
Source File: DataSetLineageJerseyResourceIT.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testInputsGraphForEntity() throws Exception {
    String tableId = atlasClientV1.getEntity(HIVE_TABLE_TYPE, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME,
            salesMonthlyTable).getId()._getId();
    ObjectNode results = atlasClientV1.getInputGraphForEntity(tableId);
    Assert.assertNotNull(results);

    Struct resultsInstance = AtlasType.fromV1Json(results.toString(), Struct.class);
    resultsInstance.normalize();

    Map<String, Object> vertices = (Map<String, Object>) resultsInstance.get("vertices");
    Assert.assertEquals(vertices.size(), 4);

    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);
}
 
Example 12
Source File: AtlasClient.java    From atlas with Apache License 2.0 4 votes vote down vote up
public TypesDef getType(String typeName) throws AtlasServiceException {
    ObjectNode response = callAPIWithBodyAndParams(API_V1.GET_TYPE, null, typeName);
    String     typeJson = AtlasType.toJson(response.get(DEFINITION));
    return AtlasType.fromV1Json(typeJson, TypesDef.class);
}
 
Example 13
Source File: EntityResult.java    From atlas with Apache License 2.0 4 votes vote down vote up
public static EntityResult fromString(String json) {
    return AtlasType.fromV1Json(json, EntityResult.class);
}
 
Example 14
Source File: EntityAuditEvent.java    From atlas with Apache License 2.0 4 votes vote down vote up
@JsonIgnore
public void setEntityDefinition(String entityDefinition) {
    this.entityDefinition = AtlasType.fromV1Json(entityDefinition, Referenceable.class);
}
 
Example 15
Source File: EntityAuditEvent.java    From atlas with Apache License 2.0 4 votes vote down vote up
@JsonIgnore
public static EntityAuditEvent fromString(String eventString) {
    return AtlasType.fromV1Json(eventString, EntityAuditEvent.class);
}
 
Example 16
Source File: AtlasClient.java    From atlas with Apache License 2.0 2 votes vote down vote up
/**
 * Get an entity given the entity id
 * @param guid entity id
 * @return result object
 * @throws AtlasServiceException
 */
public Referenceable getEntity(String guid) throws AtlasServiceException {
    ObjectNode jsonResponse = callAPIWithBodyAndParams(API_V1.GET_ENTITY, null, guid);
    String entityInstanceDefinition = AtlasType.toJson(jsonResponse.get(AtlasClient.DEFINITION));
    return AtlasType.fromV1Json(entityInstanceDefinition, Referenceable.class);
}
 
Example 17
Source File: AtlasClient.java    From atlas with Apache License 2.0 2 votes vote down vote up
/**
 * Get trait definition for a given entity and traitname
 * @param guid GUID of the entity
 * @param traitName
 * @return trait definition
 * @throws AtlasServiceException
 */
public Struct getTraitDefinition(final String guid, final String traitName) throws AtlasServiceException {
    ObjectNode jsonResponse = callAPIWithBodyAndParams(API_V1.GET_TRAIT_DEFINITION, null, guid, TRAIT_DEFINITIONS, traitName);

    return AtlasType.fromV1Json(AtlasType.toJson(jsonResponse.get(AtlasClient.RESULTS)), Struct.class);
}