Search in sources :

Example 86 with AtlasVertex

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

the class EntityGraphRetriever method getEntityVertex.

private AtlasVertex getEntityVertex(AtlasObjectId objId) throws AtlasBaseException {
    AtlasVertex ret = null;
    if (!AtlasTypeUtil.isValid(objId)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, objId.toString());
    }
    if (AtlasTypeUtil.isAssignedGuid(objId)) {
        ret = AtlasGraphUtilsV1.findByGuid(objId.getGuid());
    } else {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objId.getTypeName());
        Map<String, Object> uniqAttributes = objId.getUniqueAttributes();
        ret = AtlasGraphUtilsV1.getVertexByUniqueAttributes(entityType, uniqAttributes);
    }
    if (ret == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, objId.toString());
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

Example 87 with AtlasVertex

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

the class GraphBackedDiscoveryService method extractResult.

private List<Map<String, String>> extractResult(final Object o) throws DiscoveryException {
    List<Map<String, String>> result = new ArrayList<>();
    if (o instanceof List) {
        List l = (List) o;
        for (Object value : l) {
            Map<String, String> oRow = new HashMap<>();
            if (value instanceof Map) {
                @SuppressWarnings("unchecked") Map<Object, Object> iRow = (Map) value;
                for (Map.Entry e : iRow.entrySet()) {
                    Object k = e.getKey();
                    Object v = e.getValue();
                    oRow.put(k.toString(), v.toString());
                }
            } else if (value instanceof AtlasVertex) {
                AtlasVertex<?, ?> vertex = (AtlasVertex<?, ?>) value;
                for (String key : vertex.getPropertyKeys()) {
                    Object propertyValue = GraphHelper.getProperty(vertex, key);
                    if (propertyValue != null) {
                        oRow.put(key, propertyValue.toString());
                    }
                }
            } else if (value instanceof String) {
                oRow.put("", value.toString());
            } else if (value instanceof AtlasEdge) {
                AtlasEdge edge = (AtlasEdge) value;
                oRow.put("id", edge.getId().toString());
                oRow.put("label", edge.getLabel());
                oRow.put("inVertex", edge.getInVertex().getId().toString());
                oRow.put("outVertex", edge.getOutVertex().getId().toString());
                for (String propertyKey : edge.getPropertyKeys()) {
                    oRow.put(propertyKey, GraphHelper.getProperty(edge, propertyKey).toString());
                }
            } else {
                throw new DiscoveryException(String.format("Cannot process result %s", String.valueOf(value)));
            }
            result.add(oRow);
        }
    } else {
        result.add(new HashMap<String, String>() {

            {
                put("result", o.toString());
            }
        });
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) DiscoveryException(org.apache.atlas.discovery.DiscoveryException)

Example 88 with AtlasVertex

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

the class EntityDiscoveryService method searchUsingBasicQuery.

@Override
public AtlasSearchResult searchUsingBasicQuery(String query, String typeName, String classification, String attrName, String attrValuePrefix, boolean excludeDeletedEntities, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(AtlasQueryType.BASIC);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing basic search query: {} with type: {} and classification: {}", query, typeName, classification);
    }
    final QueryParams params = validateSearchParams(limit, offset);
    Set<String> typeNames = null;
    Set<String> classificationNames = null;
    String attrQualifiedName = null;
    if (StringUtils.isNotEmpty(typeName)) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(UNKNOWN_TYPENAME, typeName);
        }
        typeNames = entityType.getTypeAndAllSubTypes();
        ret.setType(typeName);
    }
    if (StringUtils.isNotEmpty(classification)) {
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classification);
        if (classificationType == null) {
            throw new AtlasBaseException(CLASSIFICATION_NOT_FOUND, classification);
        }
        classificationNames = classificationType.getTypeAndAllSubTypes();
        ret.setClassification(classification);
    }
    boolean isAttributeSearch = StringUtils.isNotEmpty(attrName) || StringUtils.isNotEmpty(attrValuePrefix);
    boolean isGuidPrefixSearch = false;
    if (isAttributeSearch) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        ret.setQueryType(AtlasQueryType.ATTRIBUTE);
        if (entityType != null) {
            AtlasAttribute attribute = null;
            if (StringUtils.isNotEmpty(attrName)) {
                attribute = entityType.getAttribute(attrName);
                if (attribute == null) {
                    throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, typeName);
                }
            } else {
                // if attrName is null|empty iterate defaultAttrNames to get attribute value
                final List<String> defaultAttrNames = new ArrayList<>(Arrays.asList("qualifiedName", "name"));
                Iterator<String> iter = defaultAttrNames.iterator();
                while (iter.hasNext() && attribute == null) {
                    attrName = iter.next();
                    attribute = entityType.getAttribute(attrName);
                }
            }
            if (attribute == null) {
                // for guid prefix search use gremlin and nullify query to avoid using fulltext
                // (guids cannot be searched in fulltext)
                isGuidPrefixSearch = true;
                query = null;
            } else {
                attrQualifiedName = attribute.getQualifiedName();
                String attrQuery = String.format("%s AND (%s *)", attrName, attrValuePrefix.replaceAll("\\.", " "));
                query = StringUtils.isEmpty(query) ? attrQuery : String.format("(%s) AND (%s)", query, attrQuery);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing attribute search attrName: {} and attrValue: {}", attrName, attrValuePrefix);
        }
    }
    // results in a faster and accurate results than using CONTAINS/CONTAINS_PREFIX filter on entityText property
    if (StringUtils.isNotEmpty(query)) {
        final String idxQuery = getQueryForFullTextSearch(query, typeName, classification);
        final int startIdx = params.offset();
        final int resultSize = params.limit();
        int resultIdx = 0;
        for (int indexQueryOffset = 0; ; indexQueryOffset += getMaxResultSetSize()) {
            final Iterator<Result<?, ?>> qryResult = graph.indexQuery(Constants.FULLTEXT_INDEX, idxQuery, indexQueryOffset).vertices();
            if (LOG.isDebugEnabled()) {
                LOG.debug("indexQuery: query=" + idxQuery + "; offset=" + indexQueryOffset);
            }
            if (!qryResult.hasNext()) {
                break;
            }
            while (qryResult.hasNext()) {
                AtlasVertex<?, ?> vertex = qryResult.next().getVertex();
                String vertexTypeName = GraphHelper.getTypeName(vertex);
                // skip non-entity vertices
                if (StringUtils.isEmpty(vertexTypeName) || StringUtils.isEmpty(GraphHelper.getGuid(vertex))) {
                    continue;
                }
                if (typeNames != null && !typeNames.contains(vertexTypeName)) {
                    continue;
                }
                if (classificationNames != null) {
                    List<String> traitNames = GraphHelper.getTraitNames(vertex);
                    if (CollectionUtils.isEmpty(traitNames) || !CollectionUtils.containsAny(classificationNames, traitNames)) {
                        continue;
                    }
                }
                if (isAttributeSearch) {
                    String vertexAttrValue = vertex.getProperty(attrQualifiedName, String.class);
                    if (StringUtils.isNotEmpty(vertexAttrValue) && !vertexAttrValue.startsWith(attrValuePrefix)) {
                        continue;
                    }
                }
                if (skipDeletedEntities(excludeDeletedEntities, vertex)) {
                    continue;
                }
                resultIdx++;
                if (resultIdx <= startIdx) {
                    continue;
                }
                AtlasEntityHeader header = entityRetriever.toAtlasEntityHeader(vertex);
                ret.addEntity(header);
                if (ret.getEntities().size() == resultSize) {
                    break;
                }
            }
            if (ret.getEntities() != null && ret.getEntities().size() == resultSize) {
                break;
            }
        }
    } else {
        final Map<String, Object> bindings = new HashMap<>();
        String basicQuery = "g.V()";
        if (classificationNames != null) {
            bindings.put("traitNames", classificationNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER);
        }
        if (typeNames != null) {
            bindings.put("typeNames", typeNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER);
        }
        if (excludeDeletedEntities) {
            bindings.put("state", Status.ACTIVE.toString());
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_STATE_FILTER);
        }
        if (isGuidPrefixSearch) {
            bindings.put("guid", attrValuePrefix + ".*");
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.GUID_PREFIX_FILTER);
        }
        bindings.put("startIdx", params.offset());
        bindings.put("endIdx", params.offset() + params.limit());
        basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.TO_RANGE_LIST);
        ScriptEngine scriptEngine = graph.getGremlinScriptEngine();
        try {
            Object result = graph.executeGremlinScript(scriptEngine, bindings, basicQuery, false);
            if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
                List queryResult = (List) result;
                Object firstElement = queryResult.get(0);
                if (firstElement instanceof AtlasVertex) {
                    for (Object element : queryResult) {
                        if (element instanceof AtlasVertex) {
                            ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) element));
                        } else {
                            LOG.warn("searchUsingBasicQuery({}): expected an AtlasVertex; found unexpected entry in result {}", basicQuery, element);
                        }
                    }
                }
            }
        } catch (ScriptException e) {
            throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, basicQuery);
        } finally {
            graph.releaseGremlinScriptEngine(scriptEngine);
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) ScriptEngine(javax.script.ScriptEngine) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasFullTextResult(org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Result(org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result) AttributeSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult.AttributeSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) List(java.util.List) ArrayList(java.util.ArrayList) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 89 with AtlasVertex

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

the class EntityDiscoveryService method getIndexQueryResults.

private List<AtlasFullTextResult> getIndexQueryResults(AtlasIndexQuery query, QueryParams params, boolean excludeDeletedEntities) throws AtlasBaseException {
    List<AtlasFullTextResult> ret = new ArrayList<>();
    Iterator<Result> iter = query.vertices();
    while (iter.hasNext() && ret.size() < params.limit()) {
        Result idxQueryResult = iter.next();
        AtlasVertex vertex = idxQueryResult.getVertex();
        if (skipDeletedEntities(excludeDeletedEntities, vertex)) {
            continue;
        }
        String guid = vertex != null ? vertex.getProperty(Constants.GUID_PROPERTY_KEY, String.class) : null;
        if (guid != null) {
            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(vertex);
            Double score = idxQueryResult.getScore();
            ret.add(new AtlasFullTextResult(entity, score));
        }
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) AtlasFullTextResult(org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasFullTextResult(org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Result(org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result) AttributeSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult.AttributeSearchResult)

Example 90 with AtlasVertex

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

the class EntityLineageService method getLineageInfo.

private AtlasLineageInfo getLineageInfo(String guid, LineageDirection direction, int depth) throws AtlasBaseException {
    Map<String, AtlasEntityHeader> entities = new HashMap<>();
    Set<LineageRelation> relations = new HashSet<>();
    String lineageQuery = getLineageQuery(guid, direction, depth);
    List paths = (List) graph.executeGremlinScript(lineageQuery, true);
    if (CollectionUtils.isNotEmpty(paths)) {
        for (Object path : paths) {
            if (path instanceof List) {
                List vertices = (List) path;
                if (CollectionUtils.isNotEmpty(vertices)) {
                    AtlasEntityHeader prev = null;
                    for (Object vertex : vertices) {
                        if (!(vertex instanceof AtlasVertex)) {
                            continue;
                        }
                        AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader((AtlasVertex) vertex);
                        if (!entities.containsKey(entity.getGuid())) {
                            entities.put(entity.getGuid(), entity);
                        }
                        if (prev != null) {
                            if (direction.equals(LineageDirection.INPUT)) {
                                relations.add(new LineageRelation(entity.getGuid(), prev.getGuid()));
                            } else if (direction.equals(LineageDirection.OUTPUT)) {
                                relations.add(new LineageRelation(prev.getGuid(), entity.getGuid()));
                            }
                        }
                        prev = entity;
                    }
                }
            }
        }
    }
    return new AtlasLineageInfo(guid, entities, relations, direction, depth);
}
Also used : AtlasLineageInfo(org.apache.atlas.model.lineage.AtlasLineageInfo) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) HashMap(java.util.HashMap) LineageRelation(org.apache.atlas.model.lineage.AtlasLineageInfo.LineageRelation) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)164 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)53 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)26 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)21 ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)16 Id (org.apache.atlas.typesystem.persistence.Id)14 Map (java.util.Map)13 List (java.util.List)12 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)12 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)12 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)12 AtlasException (org.apache.atlas.AtlasException)11 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)11 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasGraphQuery (org.apache.atlas.repository.graphdb.AtlasGraphQuery)11 AtlasType (org.apache.atlas.type.AtlasType)11 HashSet (java.util.HashSet)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8