Search in sources :

Example 11 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class EntityResource method partialUpdateEntityByGuid.

private Response partialUpdateEntityByGuid(String guid, HttpServletRequest request) {
    String entityJson = null;
    try {
        guid = ParamChecker.notEmpty(guid, "Guid property cannot be null");
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("partially updating entity for guid {} : {} ", guid, entityJson);
        }
        Referenceable updatedEntity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !updateId.isAssigned()) {
            updatedEntity.replaceWithNewId(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());
        }
        JSONObject response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with GUID={} does not exist {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to update entity by GUID {} {}", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.typesystem.persistence.Id)

Example 12 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class QuickStartV2 method createInstance.

private AtlasEntity createInstance(AtlasEntity entity, String[] traitNames) throws Exception {
    AtlasEntity ret = null;
    EntityMutationResponse response = atlasClientV2.createEntity(new AtlasEntityWithExtInfo(entity));
    List<AtlasEntityHeader> entities = response.getEntitiesByOperation(EntityOperation.CREATE);
    if (CollectionUtils.isNotEmpty(entities)) {
        AtlasEntityWithExtInfo getByGuidResponse = atlasClientV2.getEntityByGuid(entities.get(0).getGuid());
        ret = getByGuidResponse.getEntity();
        System.out.println("Created entity of type [" + ret.getTypeName() + "], guid: " + ret.getGuid());
    }
    return ret;
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader)

Example 13 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method bulkImport.

@Override
@GraphTransaction
public EntityMutationResponse bulkImport(EntityImportStream entityStream, AtlasImportResult importResult) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> bulkImport()");
    }
    if (entityStream == null || !entityStream.hasNext()) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "no entities to create/update.");
    }
    EntityMutationResponse ret = new EntityMutationResponse();
    ret.setGuidAssignments(new HashMap<String, String>());
    Set<String> processedGuids = new HashSet<>();
    int progressReportedAtCount = 0;
    while (entityStream.hasNext()) {
        AtlasEntityWithExtInfo entityWithExtInfo = entityStream.getNextEntityWithExtInfo();
        AtlasEntity entity = entityWithExtInfo != null ? entityWithExtInfo.getEntity() : null;
        if (entity == null || processedGuids.contains(entity.getGuid())) {
            continue;
        }
        AtlasEntityStreamForImport oneEntityStream = new AtlasEntityStreamForImport(entityWithExtInfo, entityStream);
        EntityMutationResponse resp = createOrUpdate(oneEntityStream, false, true);
        updateImportMetrics("entity:%s:created", resp.getCreatedEntities(), processedGuids, importResult);
        updateImportMetrics("entity:%s:updated", resp.getUpdatedEntities(), processedGuids, importResult);
        updateImportMetrics("entity:%s:deleted", resp.getDeletedEntities(), processedGuids, importResult);
        if ((processedGuids.size() - progressReportedAtCount) > 1000) {
            progressReportedAtCount = processedGuids.size();
            LOG.info("bulkImport(): in progress.. number of entities imported: {}", progressReportedAtCount);
        }
        if (resp.getGuidAssignments() != null) {
            ret.getGuidAssignments().putAll(resp.getGuidAssignments());
        }
        entityStream.onImportComplete(entity.getGuid());
    }
    importResult.getProcessedEntities().addAll(processedGuids);
    LOG.info("bulkImport(): done. Number of entities imported: {}", processedGuids.size());
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) HashSet(java.util.HashSet) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 14 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method createOrUpdate.

private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean isPartialUpdate, boolean replaceClassifications) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> createOrUpdate()");
    }
    if (entityStream == null || !entityStream.hasNext()) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "no entities to create/update.");
    }
    // Create/Update entities
    EntityMutationContext context = preCreateOrUpdate(entityStream, entityGraphMapper, isPartialUpdate);
    EntityMutationResponse ret = entityGraphMapper.mapAttributesAndClassifications(context, isPartialUpdate, replaceClassifications);
    ret.setGuidAssignments(context.getGuidAssignments());
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== createOrUpdate()");
    }
    // Notify the change listeners
    entityChangeNotifier.onEntitiesMutated(ret, entityStream instanceof EntityImportStream);
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse)

Example 15 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method deleteByUniqueAttributes.

@Override
@GraphTransaction
public EntityMutationResponse deleteByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes) throws AtlasBaseException {
    if (MapUtils.isEmpty(uniqAttributes)) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, uniqAttributes.toString());
    }
    final AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(entityType, uniqAttributes);
    Collection<AtlasVertex> deletionCandidates = new ArrayList<>();
    if (vertex != null) {
        deletionCandidates.add(vertex);
    } else {
        if (LOG.isDebugEnabled()) {
            // Entity does not exist - treat as non-error, since the caller
            // wanted to delete the entity and it's already gone.
            LOG.debug("Deletion request ignored for non-existent entity with uniqueAttributes " + uniqAttributes);
        }
    }
    EntityMutationResponse ret = deleteVertices(deletionCandidates);
    // Notify the change listeners
    entityChangeNotifier.onEntitiesMutated(ret, false);
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)60 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)44 Test (org.testng.annotations.Test)40 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)34 BeforeTest (org.testng.annotations.BeforeTest)22 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)21 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)17 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)17 ArrayList (java.util.ArrayList)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)13 HashMap (java.util.HashMap)12 List (java.util.List)8 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)7 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)7 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)7 ImmutableList (com.google.common.collect.ImmutableList)6 AtlasException (org.apache.atlas.AtlasException)6 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)6 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)6 Map (java.util.Map)5