Search in sources :

Example 61 with AtlasPerfTracer

use of org.apache.atlas.utils.AtlasPerfTracer in project atlas by apache.

the class EntityResource method submit.

/**
 * Submits the entity definitions (instances).
 * The body contains the JSONArray of entity json. The service takes care of de-duping the entities based on any
 * unique attribute for the give type.
 */
@POST
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response submit(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.submit()");
    }
    String entityJson = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.submit()");
        }
        entityJson = Servlets.getRequestPayload(request);
        // Handle backward compatibility - if entities is not JSONArray, convert to JSONArray
        String[] jsonStrings;
        try {
            ArrayNode jsonEntities = AtlasJson.parseToV1ArrayNode(entityJson);
            jsonStrings = new String[jsonEntities.size()];
            for (int i = 0; i < jsonEntities.size(); i++) {
                jsonStrings[i] = AtlasJson.toV1Json(jsonEntities.get(i));
            }
        } catch (IOException e) {
            jsonStrings = new String[1];
            jsonStrings[0] = entityJson;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("submitting entities: count={}; entities-json={}", jsonStrings.length, entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(jsonStrings);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        final List<String> guids = restAdapters.getGuids(mutationResponse.getCreatedEntities());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created entities {}", guids);
        }
        final CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        String response = getResponse(result);
        URI locationURI = getLocationURI(guids);
        return Response.created(locationURI).entity(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw toWebApplicationException(e);
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.submit()");
        }
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) URI(java.net.URI) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 62 with AtlasPerfTracer

use of org.apache.atlas.utils.AtlasPerfTracer in project atlas by apache.

the class EntityResource method updateByUniqueAttribute.

/**
 * Adds/Updates given entity identified by its unique attribute( entityType, attributeName and value)
 * Updates support only partial update of an entity - Adds/updates any new values specified
 * Updates do not support removal of attribute values
 *
 * @param entityType the entity type
 * @param attribute the unique attribute used to identify the entity
 * @param value the unique attributes value
 * @param request The updated entity json
 * @return response payload as json
 * The body contains the JSONArray of entity json. The service takes care of de-duping the entities based on any
 * unique attribute for the give type.
 */
@POST
@Path("qualifiedName")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateByUniqueAttribute(@QueryParam("type") String entityType, @QueryParam("property") String attribute, @QueryParam("value") String value, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    String entityJson = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateByUniqueAttribute(" + entityType + ", " + attribute + ", " + value + ")");
        }
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Partially updating entity by unique attribute {} {} {} {} ", entityType, attribute, value, entityJson);
        }
        Referenceable updatedEntity = AtlasType.fromV1Json(entityJson, Referenceable.class);
        entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
        attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
        value = ParamChecker.notEmpty(value, "attribute value cannot be null");
        Map<String, Object> attributes = new HashMap<>();
        attributes.put(attribute, value);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !AtlasTypeUtil.isAssignedGuid(updateId.getId())) {
            String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(getEntityType(entityType), attributes);
            updatedEntity.setId(new Id(guid, 0, updatedEntity.getTypeName()));
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntity(updatedEntity);
        EntityMutationResponse mutationResponse = entitiesStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), true);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        String response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.v1.model.instance.Id)

Example 63 with AtlasPerfTracer

use of org.apache.atlas.utils.AtlasPerfTracer in project atlas by apache.

the class EntityResource method getAuditEvents.

/**
 * Returns the entity audit events for a given entity id. The events are returned in the decreasing order of timestamp.
 * @param guid entity id
 * @param startKey used for pagination. Startkey is inclusive, the returned results contain the event with the given startkey.
 *                  First time getAuditEvents() is called for an entity, startKey should be null,
 *                  with count = (number of events required + 1). Next time getAuditEvents() is called for the same entity,
 *                  startKey should be equal to the entityKey of the last event returned in the previous call.
 * @param count number of events required
 * @return
 */
@GET
@Path("{guid}/audit")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getAuditEvents(@PathParam("guid") String guid, @QueryParam("startKey") String startKey, @QueryParam("count") @DefaultValue("100") short count) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getAuditEvents({}, {}, {})", guid, startKey, count);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getAuditEvents(" + guid + ", " + startKey + ", " + count + ")");
        }
        List events = entityAuditRepository.listEvents(guid, startKey, count);
        List<EntityAuditEvent> v1Events = new ArrayList<>();
        for (Object event : events) {
            if (event instanceof EntityAuditEvent) {
                v1Events.add((EntityAuditEvent) event);
            } else if (event instanceof EntityAuditEventV2) {
                v1Events.add(instanceConverter.toV1AuditEvent((EntityAuditEventV2) event));
            } else {
                LOG.warn("unknown entity-audit event type {}. Ignored", event != null ? event.getClass().getCanonicalName() : "null");
            }
        }
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.EVENTS, v1Events);
        return Response.ok(AtlasJson.toV1Json(response)).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get audit events for entity guid={} startKey={}", guid, startKey, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get audit events for entity guid={} startKey={}", guid, startKey, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get audit events for entity guid={} startKey={}", guid, startKey, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getAuditEvents({}, {}, {})", guid, startKey, count);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) ArrayList(java.util.ArrayList) EntityAuditEvent(org.apache.atlas.EntityAuditEvent) EntityAuditEventV2(org.apache.atlas.model.audit.EntityAuditEventV2) List(java.util.List) ArrayList(java.util.ArrayList)

Example 64 with AtlasPerfTracer

use of org.apache.atlas.utils.AtlasPerfTracer in project atlas by apache.

the class EntityResource method getTraitDefinitionForEntity.

/**
 * Fetches the trait definition for an entity given its guid and trait name
 *
 * @param guid globally unique identifier for the entity
 * @param traitName name of the trait
 */
@GET
@Path("{guid}/traitDefinitions/{traitName}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getTraitDefinitionForEntity(@PathParam("guid") String guid, @PathParam("traitName") String traitName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getTraitDefinitionForEntity({}, {})", guid, traitName);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getTraitDefinitionForEntity(" + guid + ", " + traitName + ")");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching trait definition for entity {} and trait name {}", guid, traitName);
        }
        final AtlasClassification classification = entitiesStore.getClassification(guid, traitName);
        Struct traitDefinition = restAdapters.getTrait(classification);
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.RESULTS, traitDefinition);
        return Response.ok(AtlasJson.toV1Json(response)).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getTraitDefinitionForEntity({}, {})", guid, traitName);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 65 with AtlasPerfTracer

use of org.apache.atlas.utils.AtlasPerfTracer in project atlas by apache.

the class MetadataDiscoveryResource method searchUsingFullText.

/**
 * Search using full text search.
 *
 * @param query search query.
 * @param limit number of rows to be returned in the result, used for pagination. maxlimit > limit > 0. -1 maps to atlas.search.defaultlimit property value
 * @param offset offset to the results returned, used for pagination. offset >= 0. -1 maps to offset 0
 * @return JSON representing the type and results.
 */
@GET
@Path("search/fulltext")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response searchUsingFullText(@QueryParam("query") String query, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("limit") int limit, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("offset") int offset) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingFullText({}, {}, {})", query, limit, offset);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingFullText(" + query + ", " + limit + ", " + offset + ")");
        }
        query = ParamChecker.notEmpty(query, "query cannot be null or empty");
        QueryParams queryParams = validateQueryParams(limit, offset);
        AtlasSearchResult result = atlasDiscoveryService.searchUsingFullTextQuery(query, false, queryParams.limit(), queryParams.offset());
        FullTextSearchResult fullTextResult = new FullTextSearchResult();
        fullTextResult.setQueryType(QUERY_TYPE_FULLTEXT);
        fullTextResult.setRequestId(Servlets.getRequestId());
        fullTextResult.setDataType(result.getType());
        fullTextResult.setQuery(result.getQueryText());
        fullTextResult.setCount(0);
        if (CollectionUtils.isNotEmpty(result.getFullTextResult())) {
            for (AtlasSearchResult.AtlasFullTextResult entity : result.getFullTextResult()) {
                fullTextResult.addResult(entity);
            }
            fullTextResult.setCount(fullTextResult.getResults().size());
        }
        String response = AtlasJson.toV1SearchJson(fullTextResult);
        return Response.ok(response).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for query {}", query, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingFullText({}, {}, {})", query, limit, offset);
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) FullTextSearchResult(org.apache.atlas.v1.model.discovery.FullTextSearchResult) QueryParams(org.apache.atlas.query.QueryParams) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)108 Produces (javax.ws.rs.Produces)76 Path (javax.ws.rs.Path)67 Consumes (javax.ws.rs.Consumes)45 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)45 GET (javax.ws.rs.GET)43 HashMap (java.util.HashMap)27 JSONObject (org.codehaus.jettison.json.JSONObject)24 WebApplicationException (javax.ws.rs.WebApplicationException)22 ArrayList (java.util.ArrayList)14 DELETE (javax.ws.rs.DELETE)11 POST (javax.ws.rs.POST)11 PUT (javax.ws.rs.PUT)11 AtlasException (org.apache.atlas.AtlasException)10 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)10 JSONArray (org.codehaus.jettison.json.JSONArray)9 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)8 DiscoveryException (org.apache.atlas.discovery.DiscoveryException)7 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)7 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)7