Search in sources :

Example 6 with AtlasGraphQuery

use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.

the class GraphBackedMetadataRepository method getEntityList.

@Override
@GraphTransaction
public List<String> getEntityList(String entityType) throws RepositoryException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving entity list for type={}", entityType);
    }
    AtlasGraphQuery query = getGraph().query().has(Constants.ENTITY_TYPE_PROPERTY_KEY, entityType);
    Iterator<AtlasVertex> results = query.vertices().iterator();
    if (!results.hasNext()) {
        return Collections.emptyList();
    }
    ArrayList<String> entityList = new ArrayList<>();
    while (results.hasNext()) {
        AtlasVertex vertex = results.next();
        entityList.add(GraphHelper.getGuid(vertex));
    }
    return entityList;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 7 with AtlasGraphQuery

use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.

the class GraphHelper method getVerticesForPropertyValues.

/**
     * Finds the Vertices that correspond to the given property values.  Property
     * values that are not found in the graph will not be in the map.
     *
     *  @return propertyValue to AtlasVertex map with the result.
     */
public Map<String, AtlasVertex> getVerticesForPropertyValues(String property, List<String> values) {
    if (values.isEmpty()) {
        return Collections.emptyMap();
    }
    Collection<String> nonNullValues = new HashSet<>(values.size());
    for (String value : values) {
        if (value != null) {
            nonNullValues.add(value);
        }
    }
    //create graph query that finds vertices with the guids
    AtlasGraphQuery query = graph.query();
    query.in(property, nonNullValues);
    Iterable<AtlasVertex> results = query.vertices();
    Map<String, AtlasVertex> result = new HashMap<>(values.size());
    //each vertex should go in the result list.
    for (AtlasVertex vertex : results) {
        if (vertex.exists()) {
            String propertyValue = vertex.getProperty(property, String.class);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found a vertex {} with {} =  {}", string(vertex), property, propertyValue);
            }
            result.put(propertyValue, vertex);
        }
    }
    return result;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap) AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) HashSet(java.util.HashSet)

Example 8 with AtlasGraphQuery

use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.

the class GraphQueryTest method testQueryDoesNotMatchRemovedVertices.

@Test
public <V, E> void testQueryDoesNotMatchRemovedVertices() throws AtlasException {
    AtlasGraph<V, E> graph = getGraph();
    AtlasVertex<V, E> v1 = createVertex(graph);
    v1.setProperty("name", "Fred");
    v1.setProperty("size15", "15");
    AtlasVertex<V, E> v2 = createVertex(graph);
    v2.setProperty("name", "Fred");
    AtlasVertex<V, E> v3 = createVertex(graph);
    v3.setProperty("size15", "15");
    AtlasVertex<V, E> v4 = createVertex(graph);
    v4.setProperty("name", "Fred");
    v4.setProperty("size15", "15");
    graph.commit();
    graph.removeVertex(v1);
    AtlasGraphQuery q = getGraphQuery();
    q.has("name", "Fred");
    q.has("size15", "15");
    assertQueryMatches(q, v4);
    graph.commit();
    assertQueryMatches(q, v4);
}
Also used : AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) Test(org.testng.annotations.Test)

Example 9 with AtlasGraphQuery

use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.

the class GraphQueryTest method testQueryMatchesAddedVertices.

@Test
public <V, E> void testQueryMatchesAddedVertices() throws AtlasException {
    AtlasGraph<V, E> graph = getGraph();
    AtlasVertex<V, E> v1 = createVertex(graph);
    v1.setProperty("name", "Fred");
    v1.setProperty("size15", "15");
    AtlasVertex<V, E> v2 = createVertex(graph);
    v2.setProperty("name", "Fred");
    AtlasVertex<V, E> v3 = createVertex(graph);
    v3.setProperty("size15", "15");
    graph.commit();
    AtlasVertex<V, E> v4 = createVertex(graph);
    v4.setProperty("name", "Fred");
    v4.setProperty("size15", "15");
    AtlasGraphQuery q = getGraphQuery();
    q.has("name", "Fred");
    q.has("size15", "15");
    assertQueryMatches(q, v1, v4);
    graph.commit();
    assertQueryMatches(q, v1, v4);
}
Also used : AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) Test(org.testng.annotations.Test)

Example 10 with AtlasGraphQuery

use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.

the class GraphQueryTest method testQueryDoesNotMatchUncommittedAddedAndRemovedVertices.

@Test
public <V, E> void testQueryDoesNotMatchUncommittedAddedAndRemovedVertices() throws AtlasException {
    AtlasGraph<V, E> graph = getGraph();
    AtlasVertex<V, E> v1 = createVertex(graph);
    v1.setProperty("name", "Fred");
    v1.setProperty("size15", "15");
    AtlasVertex<V, E> v2 = createVertex(graph);
    v2.setProperty("name", "Fred");
    AtlasVertex<V, E> v3 = createVertex(graph);
    v3.setProperty("size15", "15");
    AtlasVertex<V, E> v4 = createVertex(graph);
    v4.setProperty("name", "Fred");
    v4.setProperty("size15", "15");
    AtlasGraphQuery q = getGraphQuery();
    q.has("name", "Fred");
    q.has("size15", "15");
    assertQueryMatches(q, v1, v4);
    graph.removeVertex(v1);
    assertQueryMatches(q, v4);
    graph.commit();
    assertQueryMatches(q, v4);
}
Also used : AtlasGraphQuery(org.apache.atlas.repository.graphdb.AtlasGraphQuery) Test(org.testng.annotations.Test)

Aggregations

AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)29 Test (org.testng.annotations.Test)18 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)11 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)3 HashMap (java.util.HashMap)2 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)1 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)1 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)1 AttributeValueMap (org.apache.atlas.util.AttributeValueMap)1 IndexedInstance (org.apache.atlas.util.IndexedInstance)1