Search in sources :

Example 1 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 2 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 3 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)

Example 4 with EntityMutationResponse

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

the class TestEntitiesREST method testUpdateWithSerializedEntities.

@Test
public void testUpdateWithSerializedEntities() throws Exception {
    //Check with serialization and deserialization of entity attributes for the case
    // where attributes which are de-serialized into a map
    AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
    final AtlasEntity colEntity = TestUtilsV2.createColumnEntity(tableEntity);
    List<AtlasEntity> columns = new ArrayList<AtlasEntity>() {

        {
            add(colEntity);
        }
    };
    tableEntity.setAttribute("columns", getObjIdList(columns));
    AtlasEntity newDBEntity = serDeserEntity(dbEntity);
    AtlasEntity newTableEntity = serDeserEntity(tableEntity);
    AtlasEntitiesWithExtInfo newEntities = new AtlasEntitiesWithExtInfo();
    newEntities.addEntity(newDBEntity);
    newEntities.addEntity(newTableEntity);
    for (AtlasEntity column : columns) {
        newEntities.addReferredEntity(serDeserEntity(column));
    }
    EntityMutationResponse response2 = entityREST.createOrUpdate(newEntities);
    List<AtlasEntityHeader> newGuids = response2.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
    Assert.assertNotNull(newGuids);
    Assert.assertEquals(newGuids.size(), 3);
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Test(org.testng.annotations.Test)

Example 5 with EntityMutationResponse

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

the class EntityResource method partialUpdateEntityAttrByGuid.

/**
     * Supports Partial updates
     * Adds/Updates given entity specified by its GUID
     * Supports updation of only simple primitive attributes like strings, ints, floats, enums, class references and
     * does not support updation of complex types like arrays, maps
     * @param guid entity id
     * @param property property to add
     * @postbody property's value
     * @return response payload as json
     */
private Response partialUpdateEntityAttrByGuid(String guid, String property, HttpServletRequest request) {
    String value = null;
    try {
        Preconditions.checkNotNull(property, "Entity property cannot be null");
        value = Servlets.getRequestPayload(request);
        Preconditions.checkNotNull(value, "Entity value cannot be null");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updating entity {} for property {} = {}", guid, property, value);
        }
        EntityMutationResponse mutationResponse = entitiesStore.updateEntityAttributeByGuid(guid, property, value);
        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 add property {} to entity id {} {} ", property, guid, value, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with GUID={} does not exist {} ", guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException)

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