Search in sources :

Example 1 with AttributeValueMap

use of org.apache.atlas.util.AttributeValueMap in project atlas by apache.

the class GraphHelper method getInstancesForVertex.

// finds the instance(s) that correspond to the given vertex
private Collection<IndexedInstance> getInstancesForVertex(Map<String, AttributeValueMap> map, AtlasVertex foundVertex) {
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Object vertexValue = foundVertex.getProperty(propertyName, Object.class);
        Collection<IndexedInstance> instances = valueMap.get(vertexValue);
        if (instances != null && instances.size() > 0) {
            // the user requested).
            return instances;
        }
    // try another attribute
    }
    return Collections.emptyList();
}
Also used : AttributeValueMap(org.apache.atlas.util.AttributeValueMap) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Example 2 with AttributeValueMap

use of org.apache.atlas.util.AttributeValueMap in project atlas by apache.

the class GraphHelper method getVerticesForInstancesByUniqueAttribute.

/**
 * Finds vertices that match at least one unique attribute of the instances specified.  The AtlasVertex at a given index in the result corresponds
 * to the IReferencableInstance at that same index that was passed in.  The number of elements in the resultant list is guaranteed to match the
 * number of instances that were passed in.  If no vertex is found for a given instance, that entry will be null in the resultant list.
 *
 * @param classType
 * @param instancesForClass
 * @return
 * @throws AtlasException
 */
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(AtlasEntityType classType, List<? extends Referenceable> instancesForClass) throws AtlasException {
    // For each attribute, need to figure out what values to search for and which instance(s)
    // those values correspond to.
    Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
    for (AtlasAttribute attributeInfo : classType.getUniqAttributes().values()) {
        String propertyKey = attributeInfo.getQualifiedName();
        AttributeValueMap mapForAttribute = new AttributeValueMap();
        for (int idx = 0; idx < instancesForClass.size(); idx++) {
            Referenceable instance = instancesForClass.get(idx);
            Object value = instance.get(attributeInfo.getName());
            mapForAttribute.put(value, instance, idx);
        }
        map.put(propertyKey, mapForAttribute);
    }
    AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
    if (map.isEmpty()) {
        // no unique attributes
        return Arrays.asList(result);
    }
    // construct gremlin query
    AtlasGraphQuery query = graph.query();
    query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getTypeName());
    query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
    List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
    // that matches the value in some instance.
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        AtlasGraphQuery orChild = query.createChildQuery();
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Set<Object> values = valueMap.getAttributeValues();
        if (values.size() == 1) {
            orChild.has(propertyName, values.iterator().next());
        } else if (values.size() > 1) {
            orChild.in(propertyName, values);
        }
        orChildren.add(orChild);
    }
    if (orChildren.size() == 1) {
        AtlasGraphQuery child = orChildren.get(0);
        query.addConditionsFrom(child);
    } else if (orChildren.size() > 1) {
        query.or(orChildren);
    }
    Iterable<AtlasVertex> queryResult = query.vertices();
    for (AtlasVertex matchingVertex : queryResult) {
        Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
        for (IndexedInstance wrapper : matches) {
            result[wrapper.getIndex()] = matchingVertex;
        }
    }
    return Arrays.asList(result);
}
Also used : AttributeValueMap(org.apache.atlas.util.AttributeValueMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Example 3 with AttributeValueMap

use of org.apache.atlas.util.AttributeValueMap in project incubator-atlas by apache.

the class GraphHelper method getVerticesForInstancesByUniqueAttribute.

/**
 * Finds vertices that match at least one unique attribute of the instances specified.  The AtlasVertex at a given index in the result corresponds
 * to the IReferencableInstance at that same index that was passed in.  The number of elements in the resultant list is guaranteed to match the
 * number of instances that were passed in.  If no vertex is found for a given instance, that entry will be null in the resultant list.
 *
 * @param classType
 * @param instancesForClass
 * @return
 * @throws AtlasException
 */
public List<AtlasVertex> getVerticesForInstancesByUniqueAttribute(ClassType classType, List<? extends IReferenceableInstance> instancesForClass) throws AtlasException {
    // For each attribute, need to figure out what values to search for and which instance(s)
    // those values correspond to.
    Map<String, AttributeValueMap> map = new HashMap<String, AttributeValueMap>();
    for (AttributeInfo attributeInfo : classType.fieldMapping().fields.values()) {
        if (attributeInfo.isUnique) {
            String propertyKey = getQualifiedFieldName(classType, attributeInfo.name);
            AttributeValueMap mapForAttribute = new AttributeValueMap();
            for (int idx = 0; idx < instancesForClass.size(); idx++) {
                IReferenceableInstance instance = instancesForClass.get(idx);
                Object value = instance.get(attributeInfo.name);
                mapForAttribute.put(value, instance, idx);
            }
            map.put(propertyKey, mapForAttribute);
        }
    }
    AtlasVertex[] result = new AtlasVertex[instancesForClass.size()];
    if (map.isEmpty()) {
        // no unique attributes
        return Arrays.asList(result);
    }
    // construct gremlin query
    AtlasGraphQuery query = graph.query();
    query.has(Constants.ENTITY_TYPE_PROPERTY_KEY, classType.getName());
    query.has(Constants.STATE_PROPERTY_KEY, Id.EntityState.ACTIVE.name());
    List<AtlasGraphQuery> orChildren = new ArrayList<AtlasGraphQuery>();
    // that matches the value in some instance.
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        AtlasGraphQuery orChild = query.createChildQuery();
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Set<Object> values = valueMap.getAttributeValues();
        if (values.size() == 1) {
            orChild.has(propertyName, values.iterator().next());
        } else if (values.size() > 1) {
            orChild.in(propertyName, values);
        }
        orChildren.add(orChild);
    }
    if (orChildren.size() == 1) {
        AtlasGraphQuery child = orChildren.get(0);
        query.addConditionsFrom(child);
    } else if (orChildren.size() > 1) {
        query.or(orChildren);
    }
    Iterable<AtlasVertex> queryResult = query.vertices();
    for (AtlasVertex matchingVertex : queryResult) {
        Collection<IndexedInstance> matches = getInstancesForVertex(map, matchingVertex);
        for (IndexedInstance wrapper : matches) {
            result[wrapper.getIndex()] = matchingVertex;
        }
    }
    return Arrays.asList(result);
}
Also used : AttributeValueMap(org.apache.atlas.util.AttributeValueMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Example 4 with AttributeValueMap

use of org.apache.atlas.util.AttributeValueMap in project incubator-atlas by apache.

the class GraphHelper method getInstancesForVertex.

// finds the instance(s) that correspond to the given vertex
private Collection<IndexedInstance> getInstancesForVertex(Map<String, AttributeValueMap> map, AtlasVertex foundVertex) {
    for (Map.Entry<String, AttributeValueMap> entry : map.entrySet()) {
        String propertyName = entry.getKey();
        AttributeValueMap valueMap = entry.getValue();
        Object vertexValue = foundVertex.getProperty(propertyName, Object.class);
        Collection<IndexedInstance> instances = valueMap.get(vertexValue);
        if (instances != null && instances.size() > 0) {
            // the user requested).
            return instances;
        }
    // try another attribute
    }
    return Collections.emptyList();
}
Also used : AttributeValueMap(org.apache.atlas.util.AttributeValueMap) IndexedInstance(org.apache.atlas.util.IndexedInstance) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashMap(java.util.HashMap) HashBiMap(com.google.common.collect.HashBiMap) AttributeValueMap(org.apache.atlas.util.AttributeValueMap)

Aggregations

BiMap (com.google.common.collect.BiMap)4 HashBiMap (com.google.common.collect.HashBiMap)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 AttributeValueMap (org.apache.atlas.util.AttributeValueMap)4 IndexedInstance (org.apache.atlas.util.IndexedInstance)4 ArrayList (java.util.ArrayList)2 AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)1 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)1 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)1 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)1