Search in sources :

Example 1 with DiscoveryException

use of org.apache.atlas.discovery.DiscoveryException in project incubator-atlas by apache.

the class GraphBackedDiscoveryService method searchByFullText.

// For titan 0.5.4, refer to http://s3.thinkaurelius.com/docs/titan/0.5.4/index-backends.html for indexed query
// http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query
// .html#query-string-syntax for query syntax
@Override
@GraphTransaction
public String searchByFullText(String query, QueryParams queryParams) throws DiscoveryException {
    String graphQuery = String.format("v.\"%s\":(%s)", Constants.ENTITY_TEXT_PROPERTY_KEY, query);
    LOG.debug("Full text query: {}", graphQuery);
    Iterator<AtlasIndexQuery.Result<?, ?>> results = graph.indexQuery(Constants.FULLTEXT_INDEX, graphQuery).vertices();
    JSONArray response = new JSONArray();
    int index = 0;
    while (results.hasNext() && index < queryParams.offset()) {
        results.next();
        index++;
    }
    while (results.hasNext() && response.length() < queryParams.limit()) {
        AtlasIndexQuery.Result<?, ?> result = results.next();
        AtlasVertex<?, ?> vertex = result.getVertex();
        JSONObject row = new JSONObject();
        String guid = GraphHelper.getGuid(vertex);
        if (guid != null) {
            // Filter non-class entities
            try {
                row.put("guid", guid);
                row.put(AtlasClient.TYPENAME, GraphHelper.getTypeName(vertex));
                row.put(SCORE, result.getScore());
            } catch (JSONException e) {
                LOG.error("Unable to create response", e);
                throw new DiscoveryException("Unable to create response");
            }
            response.put(row);
        }
    }
    return response.toString();
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) AtlasIndexQuery(org.apache.atlas.repository.graphdb.AtlasIndexQuery) DiscoveryException(org.apache.atlas.discovery.DiscoveryException) GremlinQueryResult(org.apache.atlas.query.GremlinQueryResult) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 2 with DiscoveryException

use of org.apache.atlas.discovery.DiscoveryException in project incubator-atlas by apache.

the class DataSetLineageResource method outputsGraph.

/**
 * Returns the outputs graph for a given entity.
 *
 * @param tableName table name
 */
@GET
@Path("table/{tableName}/outputs/graph")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response outputsGraph(@Context HttpServletRequest request, @PathParam("tableName") String tableName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> DataSetLineageResource.outputsGraph({})", tableName);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "DataSetLineageResource.outputsGraph(tableName=" + tableName + ")");
        }
        final String jsonResult = lineageService.getOutputsGraph(tableName);
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put("tableName", tableName);
        response.put(AtlasClient.RESULTS, new JSONObject(jsonResult));
        return Response.ok(response).build();
    } catch (EntityNotFoundException e) {
        LOG.error("table entity not found for {}", tableName);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (DiscoveryException | IllegalArgumentException e) {
        LOG.error("Unable to get lineage outputs graph for table {}", tableName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get lineage outputs graph for table {}", tableName, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get lineage outputs graph for table {}", tableName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) DiscoveryException(org.apache.atlas.discovery.DiscoveryException)

Example 3 with DiscoveryException

use of org.apache.atlas.discovery.DiscoveryException in project incubator-atlas by apache.

the class DataSetLineageResource method inputsGraph.

/**
 * Returns the inputs graph for a given entity.
 *
 * @param tableName table name
 */
@GET
@Path("table/{tableName}/inputs/graph")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response inputsGraph(@Context HttpServletRequest request, @PathParam("tableName") String tableName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> DataSetLineageResource.inputsGraph({})", tableName);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "DataSetLineageResource.inputsGraph(tableName=" + tableName + ")");
        }
        final String jsonResult = lineageService.getInputsGraph(tableName);
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put("tableName", tableName);
        response.put(AtlasClient.RESULTS, new JSONObject(jsonResult));
        return Response.ok(response).build();
    } catch (EntityNotFoundException e) {
        LOG.error("table entity not found for {}", tableName);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (DiscoveryException | IllegalArgumentException e) {
        LOG.error("Unable to get lineage inputs graph for table {}", tableName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get lineage inputs graph for table {}", tableName, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get lineage inputs graph for table {}", tableName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) DiscoveryException(org.apache.atlas.discovery.DiscoveryException)

Example 4 with DiscoveryException

use of org.apache.atlas.discovery.DiscoveryException in project incubator-atlas by apache.

the class MetadataDiscoveryResource method searchUsingGremlinQuery.

/**
 * Search using raw gremlin query format.
 *
 * @param gremlinQuery search query in raw gremlin format.
 * @return JSON representing the type and results.
 */
@GET
@Path("search/gremlin")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
@InterfaceAudience.Private
public Response searchUsingGremlinQuery(@QueryParam("query") String gremlinQuery) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingGremlinQuery({})", gremlinQuery);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingGremlinQuery(" + gremlinQuery + ")");
        }
        gremlinQuery = ParamChecker.notEmpty(gremlinQuery, "gremlinQuery cannot be null or empty");
        final List<Map<String, String>> results = discoveryService.searchByGremlin(gremlinQuery);
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.QUERY, gremlinQuery);
        response.put(AtlasClient.QUERY_TYPE, QUERY_TYPE_GREMLIN);
        JSONArray list = new JSONArray();
        for (Map<String, String> result : results) {
            list.put(new JSONObject(result));
        }
        response.put(AtlasClient.RESULTS, list);
        response.put(AtlasClient.COUNT, list.length());
        return Response.ok(response).build();
    } catch (DiscoveryException | IllegalArgumentException e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingGremlinQuery({})", gremlinQuery);
        }
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) JSONArray(org.codehaus.jettison.json.JSONArray) Map(java.util.Map) DiscoveryException(org.apache.atlas.discovery.DiscoveryException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with DiscoveryException

use of org.apache.atlas.discovery.DiscoveryException 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());
                    }
                }
                oRow.put(GREMLIN_ID_KEY, vertex.getId().toString());
            } else if (value instanceof String) {
                oRow.put("", value.toString());
            } else if (value instanceof AtlasEdge) {
                AtlasEdge edge = (AtlasEdge) value;
                oRow.put(GREMLIN_ID_KEY, edge.getId().toString());
                oRow.put(GREMLIN_LABEL_KEY, edge.getLabel());
                oRow.put(GREMLIN_INVERTEX_KEY, edge.getInVertex().getId().toString());
                oRow.put(GREMLIN_OUTVERTEX_KEY, 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)

Aggregations

DiscoveryException (org.apache.atlas.discovery.DiscoveryException)9 JSONObject (org.codehaus.jettison.json.JSONObject)9 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)7 Consumes (javax.ws.rs.Consumes)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)4 JSONArray (org.codehaus.jettison.json.JSONArray)3 Map (java.util.Map)2 QueryParams (org.apache.atlas.query.QueryParams)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)1 GremlinQueryResult (org.apache.atlas.query.GremlinQueryResult)1 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)1 AtlasIndexQuery (org.apache.atlas.repository.graphdb.AtlasIndexQuery)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1