Search in sources :

Example 1 with GremlinQuery

use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.

the class GraphBackedDiscoveryService method parseAndTranslateDsl.

private GremlinQuery parseAndTranslateDsl(String dslQuery, QueryParams queryParams) throws DiscoveryException {
    CompiledQueryCacheKey entry = new CompiledQueryCacheKey(dslQuery, queryParams);
    GremlinQuery gremlinQuery = QueryProcessor.compiledQueryCache().get(entry);
    if (gremlinQuery == null) {
        Expressions.Expression validatedExpression = parseQuery(dslQuery, queryParams);
        //If the final limit is 0, don't launch the query, return with 0 rows
        if (validatedExpression instanceof Expressions.LimitExpression && ((Integer) ((Expressions.LimitExpression) validatedExpression).limit().rawValue()) == 0) {
            gremlinQuery = new NoopGremlinQuery(validatedExpression.dataType());
        } else {
            gremlinQuery = new GremlinTranslator(validatedExpression, graphPersistenceStrategy).translate();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Query = {}", validatedExpression);
                LOG.debug("Expression Tree = {}", validatedExpression.treeString());
                LOG.debug("Gremlin Query = {}", gremlinQuery.queryStr());
            }
        }
        QueryProcessor.compiledQueryCache().put(entry, gremlinQuery);
    }
    return gremlinQuery;
}
Also used : GremlinTranslator(org.apache.atlas.query.GremlinTranslator) NoopGremlinQuery(org.apache.atlas.util.NoopGremlinQuery) CompiledQueryCacheKey(org.apache.atlas.util.CompiledQueryCacheKey) Expressions(org.apache.atlas.query.Expressions) GremlinQuery(org.apache.atlas.query.GremlinQuery) NoopGremlinQuery(org.apache.atlas.util.NoopGremlinQuery)

Example 2 with GremlinQuery

use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.

the class EntityDiscoveryService method toGremlinQuery.

private GremlinQuery toGremlinQuery(String query, int limit, int offset) throws AtlasBaseException {
    QueryParams params = validateSearchParams(limit, offset);
    Either<NoSuccess, Expression> either = QueryParser.apply(query, params);
    if (either.isLeft()) {
        throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, query);
    }
    Expression expression = either.right().get();
    Expression validExpression = QueryProcessor.validate(expression);
    GremlinQuery gremlinQuery = new GremlinTranslator(validExpression, graphPersistenceStrategy).translate();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Translated Gremlin Query: {}", gremlinQuery.queryStr());
    }
    return gremlinQuery;
}
Also used : GremlinTranslator(org.apache.atlas.query.GremlinTranslator) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AliasExpression(org.apache.atlas.query.Expressions.AliasExpression) Expression(org.apache.atlas.query.Expressions.Expression) SelectExpression(org.apache.atlas.query.Expressions.SelectExpression) NoSuccess(scala.util.parsing.combinator.Parsers.NoSuccess) QueryParams(org.apache.atlas.query.QueryParams) GremlinQuery(org.apache.atlas.query.GremlinQuery) AtlasGremlinQuery(org.apache.atlas.util.AtlasGremlinQueryProvider.AtlasGremlinQuery)

Example 3 with GremlinQuery

use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.

the class EntityDiscoveryService method searchUsingDslQuery.

@Override
public AtlasSearchResult searchUsingDslQuery(String dslQuery, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
    GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing DSL query: {}", dslQuery);
    }
    Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), 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("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
                }
            }
        } else if (firstElement instanceof Map && (((Map) firstElement).containsKey("theInstance") || ((Map) firstElement).containsKey("theTrait"))) {
            for (Object element : queryResult) {
                if (element instanceof Map) {
                    Map map = (Map) element;
                    if (map.containsKey("theInstance")) {
                        Object value = map.get("theInstance");
                        if (value instanceof List && CollectionUtils.isNotEmpty((List) value)) {
                            Object entry = ((List) value).get(0);
                            if (entry instanceof AtlasVertex) {
                                ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) entry));
                            }
                        }
                    }
                } else {
                    LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
                }
            }
        } else if (gremlinQuery.hasSelectList()) {
            ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
        }
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) List(java.util.List) ArrayList(java.util.ArrayList) GremlinQuery(org.apache.atlas.query.GremlinQuery) AtlasGremlinQuery(org.apache.atlas.util.AtlasGremlinQueryProvider.AtlasGremlinQuery) Map(java.util.Map) HashMap(java.util.HashMap) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult)

Aggregations

GremlinQuery (org.apache.atlas.query.GremlinQuery)3 GremlinTranslator (org.apache.atlas.query.GremlinTranslator)2 AtlasGremlinQuery (org.apache.atlas.util.AtlasGremlinQueryProvider.AtlasGremlinQuery)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)1 AtlasSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult)1 Expressions (org.apache.atlas.query.Expressions)1 AliasExpression (org.apache.atlas.query.Expressions.AliasExpression)1 Expression (org.apache.atlas.query.Expressions.Expression)1 SelectExpression (org.apache.atlas.query.Expressions.SelectExpression)1 QueryParams (org.apache.atlas.query.QueryParams)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1 CompiledQueryCacheKey (org.apache.atlas.util.CompiledQueryCacheKey)1 NoopGremlinQuery (org.apache.atlas.util.NoopGremlinQuery)1 NoSuccess (scala.util.parsing.combinator.Parsers.NoSuccess)1