Search in sources :

Example 1 with ImportInfo

use of org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo in project atlas by apache.

the class AtlasEntityStoreV2 method bulkCreateOrUpdateBusinessAttributes.

@Override
@GraphTransaction
public BulkImportResponse bulkCreateOrUpdateBusinessAttributes(InputStream inputStream, String fileName) throws AtlasBaseException {
    BulkImportResponse ret = new BulkImportResponse();
    if (StringUtils.isBlank(fileName)) {
        throw new AtlasBaseException(AtlasErrorCode.FILE_NAME_NOT_FOUND, fileName);
    }
    List<String[]> fileData = FileUtils.readFileData(fileName, inputStream);
    Map<String, AtlasEntity> attributesToAssociate = getBusinessMetadataDefList(fileData, ret);
    for (AtlasEntity entity : attributesToAssociate.values()) {
        Map<String, Map<String, Object>> businessAttributes = entity.getBusinessAttributes();
        String guid = entity.getGuid();
        try {
            addOrUpdateBusinessAttributes(guid, businessAttributes, true);
            ret.addToSuccessImportInfoList(new ImportInfo(guid, businessAttributes.toString()));
        } catch (Exception e) {
            LOG.error("Error occurred while updating BusinessMetadata Attributes for Entity " + guid);
            ret.addToFailedImportInfoList(new ImportInfo(guid, businessAttributes.toString(), FAILED, e.getMessage()));
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) BulkImportResponse(org.apache.atlas.bulkimport.BulkImportResponse) ImportInfo(org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo) Map(java.util.Map) HashMap(java.util.HashMap) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) IOException(java.io.IOException) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 2 with ImportInfo

use of org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo in project atlas by apache.

the class AtlasEntityStoreV2 method getBusinessMetadataDefList.

private Map<String, AtlasEntity> getBusinessMetadataDefList(List<String[]> fileData, BulkImportResponse bulkImportResponse) throws AtlasBaseException {
    Map<String, AtlasEntity> ret = new HashMap<>();
    Map<String, AtlasVertex> vertexCache = new HashMap<>();
    List<String> failedMsgList = new ArrayList<>();
    for (int lineIndex = 0; lineIndex < fileData.size(); lineIndex++) {
        String[] record = fileData.get(lineIndex);
        int lineIndexToLog = lineIndex + 2;
        boolean missingFields = record.length < FileUtils.UNIQUE_ATTR_NAME_COLUMN_INDEX || StringUtils.isBlank(record[FileUtils.TYPENAME_COLUMN_INDEX]) || StringUtils.isBlank(record[FileUtils.UNIQUE_ATTR_VALUE_COLUMN_INDEX]) || StringUtils.isBlank(record[FileUtils.BM_ATTR_NAME_COLUMN_INDEX]) || StringUtils.isBlank(record[FileUtils.BM_ATTR_VALUE_COLUMN_INDEX]);
        if (missingFields) {
            failedMsgList.add("Line #" + lineIndexToLog + ": missing fields. " + Arrays.toString(record));
            continue;
        }
        String typeName = record[FileUtils.TYPENAME_COLUMN_INDEX];
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            failedMsgList.add("Line #" + lineIndexToLog + ": invalid entity-type '" + typeName + "'");
            continue;
        }
        String uniqueAttrValue = record[FileUtils.UNIQUE_ATTR_VALUE_COLUMN_INDEX];
        String bmAttribute = record[FileUtils.BM_ATTR_NAME_COLUMN_INDEX];
        String bmAttributeValue = record[FileUtils.BM_ATTR_VALUE_COLUMN_INDEX];
        String uniqueAttrName = AtlasTypeUtil.ATTRIBUTE_QUALIFIED_NAME;
        if (record.length > FileUtils.UNIQUE_ATTR_NAME_COLUMN_INDEX && StringUtils.isNotBlank(record[FileUtils.UNIQUE_ATTR_NAME_COLUMN_INDEX])) {
            uniqueAttrName = record[FileUtils.UNIQUE_ATTR_NAME_COLUMN_INDEX];
        }
        AtlasAttribute uniqueAttribute = entityType.getAttribute(uniqueAttrName);
        if (uniqueAttribute == null) {
            failedMsgList.add("Line #" + lineIndexToLog + ": attribute '" + uniqueAttrName + "' not found in entity-type '" + typeName + "'");
            continue;
        }
        if (!uniqueAttribute.getAttributeDef().getIsUnique()) {
            failedMsgList.add("Line #" + lineIndexToLog + ": attribute '" + uniqueAttrName + "' is not an unique attribute in entity-type '" + typeName + "'");
            continue;
        }
        String vertexKey = uniqueAttribute.getVertexPropertyName() + "_" + uniqueAttrValue;
        AtlasVertex vertex = vertexCache.get(vertexKey);
        if (vertex == null) {
            vertex = AtlasGraphUtilsV2.findByTypeAndUniquePropertyName(graph, typeName, uniqueAttribute.getVertexUniquePropertyName(), uniqueAttrValue);
            if (vertex == null) {
                failedMsgList.add("Line #" + lineIndexToLog + ": no " + typeName + " entity found with " + uniqueAttrName + "=" + uniqueAttrValue);
                continue;
            }
            vertexCache.put(vertexKey, vertex);
        }
        AtlasBusinessAttribute businessAttribute = entityType.getBusinesAAttribute(bmAttribute);
        if (businessAttribute == null) {
            failedMsgList.add("Line #" + lineIndexToLog + ": invalid business-metadata '" + bmAttribute + "' for entity type '" + entityType.getTypeName() + "'");
            continue;
        }
        final Object attrValue;
        if (businessAttribute.getAttributeType().getTypeCategory() == TypeCategory.ARRAY) {
            AtlasArrayType arrayType = (AtlasArrayType) businessAttribute.getAttributeType();
            List arrayValue;
            if (arrayType.getElementType() instanceof AtlasEnumType) {
                arrayValue = AtlasGraphUtilsV2.assignEnumValues(bmAttributeValue, (AtlasEnumType) arrayType.getElementType(), failedMsgList, lineIndex + 1);
            } else {
                arrayValue = assignMultipleValues(bmAttributeValue, arrayType.getElementTypeName(), failedMsgList, lineIndex + 1);
            }
            attrValue = arrayValue;
        } else {
            attrValue = bmAttributeValue;
        }
        if (ret.containsKey(vertexKey)) {
            AtlasEntity entity = ret.get(vertexKey);
            entity.setBusinessAttribute(businessAttribute.getDefinedInType().getTypeName(), businessAttribute.getName(), attrValue);
        } else {
            AtlasEntity entity = new AtlasEntity();
            String guid = GraphHelper.getGuid(vertex);
            Map<String, Map<String, Object>> businessAttributes = entityRetriever.getBusinessMetadata(vertex);
            entity.setGuid(guid);
            entity.setTypeName(typeName);
            entity.setAttribute(uniqueAttribute.getName(), uniqueAttrValue);
            if (businessAttributes == null) {
                businessAttributes = new HashMap<>();
            }
            entity.setBusinessAttributes(businessAttributes);
            entity.setBusinessAttribute(businessAttribute.getDefinedInType().getTypeName(), businessAttribute.getName(), attrValue);
            ret.put(vertexKey, entity);
        }
    }
    for (String failedMsg : failedMsgList) {
        LOG.error(failedMsg);
        bulkImportResponse.addToFailedImportInfoList(new ImportInfo(FAILED, failedMsg));
    }
    return ret;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) HashMap(java.util.HashMap) AtlasBusinessAttribute(org.apache.atlas.type.AtlasBusinessMetadataType.AtlasBusinessAttribute) ArrayList(java.util.ArrayList) ImportInfo(org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEnumType(org.apache.atlas.type.AtlasEnumType) List(java.util.List) ArrayList(java.util.ArrayList) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ImportInfo

use of org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo in project atlas by apache.

the class GlossaryService method updateGlossaryTermsRelation.

private void updateGlossaryTermsRelation(List<AtlasGlossaryTerm> glossaryTerms, BulkImportResponse bulkImportResponse) {
    for (AtlasGlossaryTerm glossaryTerm : glossaryTerms) {
        glossaryTermUtils.updateGlossaryTermRelations(glossaryTerm);
        if (glossaryTerm.hasTerms()) {
            String glossaryTermName = glossaryTerm.getName();
            String glossaryName = getGlossaryName(glossaryTerm);
            try {
                updateTerm(glossaryTerm, false);
            } catch (AtlasBaseException e) {
                LOG.error(AtlasErrorCode.FAILED_TO_UPDATE_GLOSSARY_TERM.toString(), glossaryTermName, e);
                bulkImportResponse.addToFailedImportInfoList(new ImportInfo(glossaryName, glossaryTermName, FAILED, e.getMessage()));
            }
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasGlossaryTerm(org.apache.atlas.model.glossary.AtlasGlossaryTerm) ImportInfo(org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo)

Example 4 with ImportInfo

use of org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo in project atlas by apache.

the class GlossaryService method createGlossaryTerms.

private void createGlossaryTerms(List<AtlasGlossaryTerm> glossaryTerms, BulkImportResponse bulkImportResponse) throws AtlasBaseException {
    for (AtlasGlossaryTerm glossaryTerm : glossaryTerms) {
        String glossaryTermName = glossaryTerm.getName();
        String glossaryName = getGlossaryName(glossaryTerm);
        try {
            AtlasGlossaryTerm createdTerm = createTerm(glossaryTerm);
            bulkImportResponse.addToSuccessImportInfoList(new ImportInfo(glossaryName, glossaryTermName, SUCCESS, AtlasJson.toJson(createdTerm.getGlossaryTermHeader())));
        } catch (AtlasBaseException e) {
            LOG.error(AtlasErrorCode.FAILED_TO_CREATE_GLOSSARY_TERM.toString(), glossaryTermName, e);
            bulkImportResponse.addToFailedImportInfoList(new ImportInfo(glossaryName, glossaryTermName, FAILED, e.getMessage()));
        }
    }
    checkForSuccessImports(bulkImportResponse);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasGlossaryTerm(org.apache.atlas.model.glossary.AtlasGlossaryTerm) ImportInfo(org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo)

Example 5 with ImportInfo

use of org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo in project atlas by apache.

the class GlossaryTermUtils method getGlossaryTermDataWithRelations.

protected List<AtlasGlossaryTerm> getGlossaryTermDataWithRelations(List<String[]> fileData, BulkImportResponse bulkImportResponse) throws AtlasBaseException {
    List<AtlasGlossaryTerm> glossaryTerms = new ArrayList<>();
    int rowCount = 1;
    for (String[] record : fileData) {
        List<String> failedTermMsgs = new ArrayList<>();
        if (ArrayUtils.isNotEmpty(record) && StringUtils.isNotBlank(record[INDEX_FOR_GLOSSARY_AT_RECORD])) {
            AtlasGlossaryTerm glossaryTerm = new AtlasGlossaryTerm();
            String glossaryName = record[INDEX_FOR_GLOSSARY_AT_RECORD];
            String glossaryGuid = glossaryNameGuidCache.get().get(glossaryName);
            if (StringUtils.isNotEmpty(glossaryGuid)) {
                glossaryTerm = populateGlossaryTermObject(failedTermMsgs, record, glossaryGuid, true);
                glossaryTerm.setQualifiedName(getGlossaryTermQualifiedName(glossaryTerm.getName(), glossaryName));
                glossaryTerms.add(glossaryTerm);
            }
            if (failedTermMsgs.size() > 0) {
                String failedTermMsg = StringUtils.join(failedTermMsgs, System.lineSeparator());
                String glossaryTermName = glossaryTerm.getName();
                bulkImportResponse.addToFailedImportInfoList(new ImportInfo(glossaryName, glossaryTermName, FAILED, failedTermMsg, rowCount));
            }
        }
        rowCount++;
    }
    return glossaryTerms;
}
Also used : AtlasGlossaryTerm(org.apache.atlas.model.glossary.AtlasGlossaryTerm) ArrayList(java.util.ArrayList) ImportInfo(org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo)

Aggregations

ImportInfo (org.apache.atlas.bulkimport.BulkImportResponse.ImportInfo)7 AtlasGlossaryTerm (org.apache.atlas.model.glossary.AtlasGlossaryTerm)4 ArrayList (java.util.ArrayList)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 IOException (java.io.IOException)1 List (java.util.List)1 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)1 BulkImportResponse (org.apache.atlas.bulkimport.BulkImportResponse)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)1 AtlasBusinessAttribute (org.apache.atlas.type.AtlasBusinessMetadataType.AtlasBusinessAttribute)1 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)1 AtlasEnumType (org.apache.atlas.type.AtlasEnumType)1 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)1