Search in sources :

Example 51 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class DataSetLineageJerseyResourceIT method setupInstances.

private void setupInstances() throws Exception {
    HierarchicalTypeDefinition<TraitType> factTrait = TypesUtil.createTraitTypeDef("Fact", ImmutableSet.<String>of());
    HierarchicalTypeDefinition<TraitType> etlTrait = TypesUtil.createTraitTypeDef("ETL", ImmutableSet.<String>of());
    HierarchicalTypeDefinition<TraitType> dimensionTrait = TypesUtil.createTraitTypeDef("Dimension", ImmutableSet.<String>of());
    HierarchicalTypeDefinition<TraitType> metricTrait = TypesUtil.createTraitTypeDef("Metric", ImmutableSet.<String>of());
    createType(getTypesDef(null, null, ImmutableList.of(factTrait, etlTrait, dimensionTrait, metricTrait), null));
    salesDBName = "Sales" + randomString();
    Id salesDB = database(salesDBName, "Sales Database", "John ETL", "hdfs://host:8000/apps/warehouse/sales");
    List<Referenceable> salesFactColumns = ImmutableList.of(column("time_id", "int", "time id"), column("product_id", "int", "product id"), column("customer_id", "int", "customer id", "pii"), column("sales", "double", "product id", "Metric"));
    salesFactTable = "sales_fact" + randomString();
    Id salesFact = table(salesFactTable, "sales fact table", salesDB, "Joe", "MANAGED", salesFactColumns, "Fact");
    List<Referenceable> timeDimColumns = ImmutableList.of(column("time_id", "int", "time id"), column("dayOfYear", "int", "day Of Year"), column("weekDay", "int", "week Day"));
    Id timeDim = table("time_dim" + randomString(), "time dimension table", salesDB, "John Doe", "EXTERNAL", timeDimColumns, "Dimension");
    Id reportingDB = database("Reporting" + randomString(), "reporting database", "Jane BI", "hdfs://host:8000/apps/warehouse/reporting");
    Id salesFactDaily = table("sales_fact_daily_mv" + randomString(), "sales fact daily materialized view", reportingDB, "Joe BI", "MANAGED", salesFactColumns, "Metric");
    loadProcess("loadSalesDaily" + randomString(), "John ETL", ImmutableList.of(salesFact, timeDim), ImmutableList.of(salesFactDaily), "create table as select ", "plan", "id", "graph", "ETL");
    salesMonthlyTable = "sales_fact_monthly_mv" + randomString();
    Id salesFactMonthly = table(salesMonthlyTable, "sales fact monthly materialized view", reportingDB, "Jane BI", "MANAGED", salesFactColumns, "Metric");
    loadProcess("loadSalesMonthly" + randomString(), "John ETL", ImmutableList.of(salesFactDaily), ImmutableList.of(salesFactMonthly), "create table as select ", "plan", "id", "graph", "ETL");
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) Id(org.apache.atlas.typesystem.persistence.Id)

Example 52 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method addReverseReference.

private <V, E> void addReverseReference(AtlasVertex<V, E> vertex, String reverseAttributeName, AtlasEdge<V, E> edge) throws AtlasException {
    String typeName = GraphHelper.getTypeName(vertex);
    Id id = GraphHelper.getIdFromVertex(typeName, vertex);
    AtlasVertex<V, E> reverseVertex = edge.getInVertex();
    String reverseTypeName = GraphHelper.getTypeName(reverseVertex);
    Id reverseId = GraphHelper.getIdFromVertex(reverseTypeName, reverseVertex);
    IDataType reverseType = typeSystem.getDataType(IDataType.class, reverseTypeName);
    AttributeInfo reverseAttrInfo = TypesUtil.getFieldMapping(reverseType).fields.get(reverseAttributeName);
    if (reverseAttrInfo.dataType().getTypeCategory() == TypeCategory.MAP) {
        // If the reverse reference is a map, what would be used as the key?
        // Not supporting automatic update of reverse map references.
        LOG.debug("Automatic update of reverse map reference is not supported - reference = {}", GraphHelper.getQualifiedFieldName(reverseType, reverseAttributeName));
        return;
    }
    String propertyName = GraphHelper.getQualifiedFieldName(reverseType, reverseAttributeName);
    String reverseEdgeLabel = GraphHelper.EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge<V, E> reverseEdge = graphHelper.getEdgeForLabel(reverseVertex, reverseEdgeLabel);
    AtlasEdge<V, E> newEdge = null;
    if (reverseEdge != null) {
        newEdge = updateClassEdge(reverseVertex, reverseEdge, id, vertex, reverseAttrInfo, reverseEdgeLabel);
    } else {
        newEdge = addClassEdge(reverseVertex, vertex, reverseEdgeLabel);
    }
    switch(reverseAttrInfo.dataType().getTypeCategory()) {
        case CLASS:
            if (reverseEdge != null && !reverseEdge.getId().toString().equals(newEdge.getId().toString())) {
                // Disconnect old reference
                deleteHandler.deleteEdgeReference(reverseEdge, reverseAttrInfo.dataType().getTypeCategory(), reverseAttrInfo.isComposite, true);
            }
            break;
        case ARRAY:
            // Add edge ID to property value
            List<String> elements = reverseVertex.getProperty(propertyName, List.class);
            if (elements == null) {
                elements = new ArrayList<>();
                elements.add(newEdge.getId().toString());
                reverseVertex.setProperty(propertyName, elements);
            } else {
                if (!elements.contains(newEdge.getId().toString())) {
                    elements.add(newEdge.getId().toString());
                    reverseVertex.setProperty(propertyName, elements);
                }
            }
            break;
    }
    RequestContext requestContext = RequestContext.get();
    GraphHelper.setProperty(reverseVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
    requestContext.recordEntityUpdate(reverseId._getId());
}
Also used : Id(org.apache.atlas.typesystem.persistence.Id) RequestContext(org.apache.atlas.RequestContext)

Example 53 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class TypedInstanceToGraphMapper method findExistingVertices.

private Map<Id, AtlasVertex> findExistingVertices(Collection<IReferenceableInstance> instances) throws AtlasException {
    VertexLookupContext context = new VertexLookupContext(this);
    Map<Id, AtlasVertex> result = new HashMap<>();
    for (IReferenceableInstance instance : instances) {
        context.addInstance(instance);
    }
    List<Id> instancesToLoad = new ArrayList<>(context.getInstancesToLoadByGuid());
    List<String> guidsToLoad = Lists.transform(instancesToLoad, new Function<Id, String>() {

        @Override
        public String apply(Id instance) {
            Id id = getExistingId(instance);
            return id.id;
        }
    });
    Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToLoad);
    List<String> missingGuids = new ArrayList<>();
    for (int i = 0; i < instancesToLoad.size(); i++) {
        String guid = guidsToLoad.get(i);
        AtlasVertex instanceVertex = instanceVertices.get(guid);
        if (instanceVertex == null) {
            missingGuids.add(guid);
            continue;
        }
        Id instance = instancesToLoad.get(i);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found vertex {} for instance {}", string(instanceVertex), instance);
        }
        result.put(instance, instanceVertex);
    }
    if (missingGuids.size() > 0) {
        throw new EntityNotFoundException("Could not find entities in the repository with the following GUIDs: " + missingGuids);
    }
    for (Map.Entry<ClassType, List<IReferenceableInstance>> entry : context.getInstancesToLoadByUniqueAttribute().entrySet()) {
        ClassType type = entry.getKey();
        List<IReferenceableInstance> instancesForClass = entry.getValue();
        List<AtlasVertex> correspondingVertices = graphHelper.getVerticesForInstancesByUniqueAttribute(type, instancesForClass);
        for (int i = 0; i < instancesForClass.size(); i++) {
            IReferenceableInstance inst = instancesForClass.get(i);
            AtlasVertex vertex = correspondingVertices.get(i);
            result.put(getExistingId(inst), vertex);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class VertexLookupContext method addAdditionalInstance.

private void addAdditionalInstance(ITypedReferenceableInstance instance) {
    if (instance == null) {
        return;
    }
    Id id = mapper.getExistingId(instance);
    if (!id.isAssigned()) {
        return;
    }
    guidsToLookup.add(id);
}
Also used : Id(org.apache.atlas.typesystem.persistence.Id)

Example 55 with Id

use of org.apache.atlas.typesystem.persistence.Id in project incubator-atlas by apache.

the class AtlasObjectIdConverter method fromV2ToV1.

@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
    Id ret = null;
    if (v2Obj != null) {
        if (v2Obj instanceof Map) {
            Map v2Map = (Map) v2Obj;
            String idStr = (String) v2Map.get(AtlasObjectId.KEY_GUID);
            String typeName = type.getTypeName();
            if (StringUtils.isEmpty(idStr)) {
                throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND);
            }
            ret = new Id(idStr, 0, typeName);
        } else if (v2Obj instanceof AtlasObjectId) {
            // transient-id
            ret = new Id(((AtlasObjectId) v2Obj).getGuid(), 0, type.getTypeName());
        } else if (v2Obj instanceof AtlasEntity) {
            AtlasEntity entity = (AtlasEntity) v2Obj;
            ret = new Id(((AtlasObjectId) v2Obj).getGuid(), 0, type.getTypeName());
        } else {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_CATEGORY_INVALID, type.getTypeCategory().name());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) Map(java.util.Map)

Aggregations

Id (org.apache.atlas.typesystem.persistence.Id)94 Referenceable (org.apache.atlas.typesystem.Referenceable)50 Test (org.testng.annotations.Test)37 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)28 List (java.util.List)17 ArrayList (java.util.ArrayList)12 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)12 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)12 ImmutableList (com.google.common.collect.ImmutableList)10 TraitType (org.apache.atlas.typesystem.types.TraitType)10 JSONObject (org.codehaus.jettison.json.JSONObject)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AtlasServiceException (org.apache.atlas.AtlasServiceException)7 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)7 Struct (org.apache.atlas.typesystem.Struct)7 ClassType (org.apache.atlas.typesystem.types.ClassType)7 AtlasException (org.apache.atlas.AtlasException)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)6 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5