use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.
the class GraphHelper method findVertex.
/**
* Args of the format prop1, key1, prop2, key2...
* Searches for a AtlasVertex with prop1=key1 && prop2=key2
* @param args
* @return AtlasVertex with the given property keys
* @throws EntityNotFoundException
*/
public AtlasVertex findVertex(Object... args) throws EntityNotFoundException {
AtlasGraphQuery query = graph.query();
for (int i = 0; i < args.length; i += 2) {
query = query.has((String) args[i], args[i + 1]);
}
Iterator<AtlasVertex> results = query.vertices().iterator();
// returning one since entityType, qualifiedName should be unique
AtlasVertex vertex = results.hasNext() ? results.next() : null;
if (vertex == null) {
String conditionStr = getConditionString(args);
if (LOG.isDebugEnabled()) {
LOG.debug("Could not find a vertex with {}", conditionStr);
}
throw new EntityNotFoundException("Could not find an entity in the repository with " + conditionStr);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Found a vertex {} with {}", string(vertex), getConditionString(args));
}
}
return vertex;
}
use of org.apache.atlas.repository.graphdb.AtlasGraphQuery 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);
}
use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.
the class GraphQueryTest method testCombinationOfAndsAndOrs.
@Test
public void testCombinationOfAndsAndOrs() throws AtlasException {
Titan1Graph graph = getTitan1Graph();
AtlasVertex<Titan1Vertex, Titan1Edge> v1 = createVertex(graph);
v1.setProperty("name", "Fred");
v1.setProperty("size15", "15");
v1.setProperty("typeName", "Person");
AtlasVertex<Titan1Vertex, Titan1Edge> v2 = createVertex(graph);
v2.setProperty("name", "George");
v2.setProperty("size15", "16");
v2.setProperty("typeName", "Person");
AtlasVertex<Titan1Vertex, Titan1Edge> v3 = createVertex(graph);
v3.setProperty("name", "Jane");
v3.setProperty("size15", "17");
v3.setProperty("typeName", "Person");
AtlasVertex<Titan1Vertex, Titan1Edge> v4 = createVertex(graph);
v4.setProperty("name", "Bob");
v4.setProperty("size15", "18");
v4.setProperty("typeName", "Person");
AtlasVertex<Titan1Vertex, Titan1Edge> v5 = createVertex(graph);
v5.setProperty("name", "Julia");
v5.setProperty("size15", "19");
v5.setProperty("typeName", "Manager");
AtlasGraphQuery q = getGraphQuery();
q.has("typeName", "Person");
//initially match
AtlasGraphQuery inner1a = q.createChildQuery();
AtlasGraphQuery inner1b = q.createChildQuery();
inner1a.has("name", "Fred");
inner1b.has("name", "Jane");
q.or(toList(inner1a, inner1b));
AtlasGraphQuery inner2a = q.createChildQuery();
AtlasGraphQuery inner2b = q.createChildQuery();
AtlasGraphQuery inner2c = q.createChildQuery();
inner2a.has("size15", "18");
inner2b.has("size15", "15");
inner2c.has("size15", "16");
q.or(toList(inner2a, inner2b, inner2c));
assertQueryMatches(q, v1);
graph.commit();
//let the index update
pause();
assertQueryMatches(q, v1);
}
use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.
the class GraphQueryTest method testSimpleOrQuery.
@Test
public void testSimpleOrQuery() throws AtlasException {
Titan1Graph graph = getTitan1Graph();
AtlasVertex<Titan1Vertex, Titan1Edge> v1 = createVertex(graph);
v1.setProperty("name", "Fred");
v1.setProperty("size15", "15");
AtlasVertex<Titan1Vertex, Titan1Edge> v2 = createVertex(graph);
v2.setProperty("name", "Fred");
AtlasVertex<Titan1Vertex, Titan1Edge> v3 = createVertex(graph);
v3.setProperty("size15", "15");
graph.commit();
AtlasVertex<Titan1Vertex, Titan1Edge> v4 = createVertex(graph);
v4.setProperty("name", "Fred");
v4.setProperty("size15", "15");
AtlasVertex<Titan1Vertex, Titan1Edge> v5 = createVertex(graph);
v5.setProperty("name", "George");
v5.setProperty("size15", "16");
AtlasGraphQuery q = graph.query();
AtlasGraphQuery inner1 = q.createChildQuery().has("name", "Fred");
AtlasGraphQuery inner2 = q.createChildQuery().has("size15", "15");
q.or(toList(inner1, inner2));
assertQueryMatches(q, v1, v2, v3, v4);
graph.commit();
//pause to let the indexer get updated (this fails frequently without a pause)
pause();
assertQueryMatches(q, v1, v2, v3, v4);
}
use of org.apache.atlas.repository.graphdb.AtlasGraphQuery in project incubator-atlas by apache.
the class AtlasGraphUtilsV1 method findByTypeAndPropertyName.
public static AtlasVertex findByTypeAndPropertyName(String typeName, String propertyName, Object attrVal) {
AtlasGraphQuery query = AtlasGraphProvider.getGraphInstance().query().has(Constants.ENTITY_TYPE_PROPERTY_KEY, typeName).has(Constants.STATE_PROPERTY_KEY, AtlasEntity.Status.ACTIVE.name()).has(propertyName, attrVal);
Iterator<AtlasVertex> results = query.vertices().iterator();
AtlasVertex vertex = results.hasNext() ? results.next() : null;
return vertex;
}
Aggregations