Search in sources :

Example 1 with AtlasEntity

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

the class AtlasEntityStoreV1 method preCreateOrUpdate.

private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, EntityGraphMapper entityGraphMapper, boolean isPartialUpdate) throws AtlasBaseException {
    EntityGraphDiscovery graphDiscoverer = new AtlasEntityGraphDiscoveryV1(typeRegistry, entityStream);
    EntityGraphDiscoveryContext discoveryContext = graphDiscoverer.discoverEntities();
    EntityMutationContext context = new EntityMutationContext(discoveryContext);
    for (String guid : discoveryContext.getReferencedGuids()) {
        AtlasVertex vertex = discoveryContext.getResolvedEntityVertex(guid);
        AtlasEntity entity = entityStream.getByGuid(guid);
        if (entity != null) {
            if (vertex != null) {
                // entity would be null if guid is not in the stream but referenced by an entity in the stream
                if (!isPartialUpdate) {
                    graphDiscoverer.validateAndNormalize(entity);
                } else {
                    graphDiscoverer.validateAndNormalizeForUpdate(entity);
                }
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                String guidVertex = AtlasGraphUtilsV1.getIdFromVertex(vertex);
                if (!StringUtils.equals(guidVertex, guid)) {
                    // if entity was found by unique attribute
                    entity.setGuid(guidVertex);
                }
                context.addUpdated(guid, entity, entityType, vertex);
            } else {
                graphDiscoverer.validateAndNormalize(entity);
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                //Create vertices which do not exist in the repository
                if ((entityStream instanceof EntityImportStream) && AtlasTypeUtil.isAssignedGuid(entity.getGuid())) {
                    vertex = entityGraphMapper.createVertexWithGuid(entity, entity.getGuid());
                } else {
                    vertex = entityGraphMapper.createVertex(entity);
                }
                discoveryContext.addResolvedGuid(guid, vertex);
                String generatedGuid = AtlasGraphUtilsV1.getIdFromVertex(vertex);
                entity.setGuid(generatedGuid);
                context.addCreated(guid, entity, entityType, vertex);
            }
            // during import, update the system attributes
            if (entityStream instanceof EntityImportStream) {
                entityGraphMapper.updateSystemAttributes(vertex, entity);
            }
        }
    }
    return context;
}
Also used : EntityGraphDiscovery(org.apache.atlas.repository.store.graph.EntityGraphDiscovery) EntityGraphDiscoveryContext(org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 2 with AtlasEntity

use of org.apache.atlas.model.instance.AtlasEntity 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 3 with AtlasEntity

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

the class AtlasEntityStoreV1 method updateByUniqueAttributes.

@Override
@GraphTransaction
public EntityMutationResponse updateByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes, AtlasEntityWithExtInfo updatedEntityInfo) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> updateByUniqueAttributes({}, {})", entityType.getTypeName(), uniqAttributes);
    }
    if (updatedEntityInfo == null || updatedEntityInfo.getEntity() == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "no entity to update.");
    }
    String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(entityType, uniqAttributes);
    AtlasEntity entity = updatedEntityInfo.getEntity();
    entity.setGuid(guid);
    return createOrUpdate(new AtlasEntityStream(updatedEntityInfo), true);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 4 with AtlasEntity

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

the class ExportServiceTest method verifyExportForEmployeeData.

private void verifyExportForEmployeeData(ZipSource zipSource) throws AtlasBaseException {
    final List<String> expectedEntityTypes = Arrays.asList(new String[] { "Manager", "Employee", "Department" });
    assertNotNull(zipSource.getCreationOrder());
    assertEquals(zipSource.getCreationOrder().size(), 2);
    assertTrue(zipSource.hasNext());
    while (zipSource.hasNext()) {
        AtlasEntity entity = zipSource.next();
        assertNotNull(entity);
        assertEquals(AtlasEntity.Status.ACTIVE, entity.getStatus());
        assertTrue(expectedEntityTypes.contains(entity.getTypeName()));
    }
    verifyTypeDefs(zipSource);
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity)

Example 5 with AtlasEntity

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

the class ExportServiceTest method verifyExportForHrData.

private void verifyExportForHrData(ZipSource zipSource) throws IOException, AtlasBaseException {
    assertNotNull(zipSource.getCreationOrder());
    assertTrue(zipSource.getCreationOrder().size() == 1);
    assertTrue(zipSource.hasNext());
    AtlasEntity entity = zipSource.next();
    assertNotNull(entity);
    assertTrue(entity.getTypeName().equals("Department"));
    assertEquals(entity.getStatus(), AtlasEntity.Status.ACTIVE);
    verifyTypeDefs(zipSource);
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity)

Aggregations

AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)124 Test (org.testng.annotations.Test)58 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)43 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)36 HashMap (java.util.HashMap)25 BeforeTest (org.testng.annotations.BeforeTest)25 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)24 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)24 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)21 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)17 ArrayList (java.util.ArrayList)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)14 Map (java.util.Map)12 List (java.util.List)11 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)11 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)9 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)7 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)7 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)7