Java Code Examples for org.apache.atlas.model.instance.AtlasEntity#hasAttribute()

The following examples show how to use org.apache.atlas.model.instance.AtlasEntity#hasAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportTransforms.java    From atlas with Apache License 2.0 6 votes vote down vote up
private void applyAttributeSpecific(AtlasEntity entity, Map<String, List<ImportTransformer>> entityTransforms) throws AtlasBaseException {
    for (Map.Entry<String, List<ImportTransformer>> entry : entityTransforms.entrySet()) {
        String                   attributeName  = entry.getKey();
        List<ImportTransformer> attrTransforms = entry.getValue();

        if (!entity.hasAttribute(attributeName)) {
            continue;
        }

        Object attributeValue = entity.getAttribute(attributeName);
        for (ImportTransformer attrTransform : attrTransforms) {
            attributeValue = attrTransform.apply(attributeValue);
        }

        entity.setAttribute(attributeName, attributeValue);
    }
}
 
Example 2
Source File: AtlasEntityGraphDiscoveryV2.java    From atlas with Apache License 2.0 6 votes vote down vote up
void visitEntity(AtlasEntityType entityType, AtlasEntity entity) throws AtlasBaseException {
    List<String> visitedAttributes = new ArrayList<>();

    // visit relationship attributes
    visitRelationships(entityType, entity, visitedAttributes);

    // visit struct attributes
    for (AtlasAttribute attribute : entityType.getAllAttributes().values()) {
        AtlasType attrType = attribute.getAttributeType();
        String    attrName = attribute.getName();
        Object    attrVal  = entity.getAttribute(attrName);

        if (entity.hasAttribute(attrName) && !visitedAttributes.contains(attrName)) {
            visitAttribute(attrType, attrVal);
        }
    }
}
 
Example 3
Source File: AtlasEntityStoreV2.java    From atlas with Apache License 2.0 6 votes vote down vote up
private void compactAttributes(AtlasEntity entity, AtlasEntityType entityType) {
    if (entity != null) {
        for (String attrName : entityType.getRelationshipAttributes().keySet()) {
            if (entity.hasAttribute(attrName)) { // relationship attribute is present in 'attributes'
                Object attrValue = entity.removeAttribute(attrName);

                if (attrValue != null) {
                    // if the attribute doesn't exist in relationshipAttributes, add it
                    Object relationshipAttrValue = entity.getRelationshipAttribute(attrName);

                    if (relationshipAttrValue == null) {
                        entity.setRelationshipAttribute(attrName, attrValue);

                        if (LOG.isDebugEnabled()) {
                            LOG.debug("moved attribute {}.{} from attributes to relationshipAttributes", entityType.getTypeName(), attrName);
                        }
                    } else {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("attribute {}.{} is present in attributes and relationshipAttributes. Removed from attributes", entityType.getTypeName(), attrName);
                        }
                    }
                }
            }
        }
    }
}
 
Example 4
Source File: ImportTransforms.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
public  AtlasEntity apply(AtlasEntity entity) throws AtlasBaseException {
    if(entity != null) {
        Map<String, List<ImportTransformer>> entityTransforms = getTransforms(entity.getTypeName());

        if (MapUtils.isNotEmpty(entityTransforms)) {
            for (Map.Entry<String, List<ImportTransformer>> entry : entityTransforms.entrySet()) {
                String                   attributeName  = entry.getKey();
                List<ImportTransformer> attrTransforms = entry.getValue();

                if (!entity.hasAttribute(attributeName)) {
                    continue;
                }

                Object transformedValue = entity.getAttribute(attributeName);

                for (ImportTransformer attrTransform : attrTransforms) {
                    transformedValue = attrTransform.apply(transformedValue);
                }

                entity.setAttribute(attributeName, transformedValue);
            }
        }
    }

    return entity;
}
 
Example 5
Source File: PreprocessorContext.java    From atlas with Apache License 2.0 4 votes vote down vote up
public void removeRefAttributeAndRegisterToMove(AtlasEntity entity, String attrName, String relationshipType, String refAttrName) {
    Object attrVal = entity.removeAttribute(attrName);

    if (attrVal != null) {
        AtlasRelatedObjectId entityId = null;
        Set<String>          guids    = new HashSet<>();

        collectGuids(attrVal, guids);

        // removed attrVal might have elements removed (e.g. removed column); to handle this case register the entity for partial update
        addToPostUpdate(entity, attrName, attrVal);

        for (String guid : guids) {
            AtlasEntity refEntity = getEntity(guid);

            if (refEntity != null) {
                Object refAttr = null;

                if (refEntity.hasRelationshipAttribute(refAttrName)) {
                    refAttr = refEntity.getRelationshipAttribute(refAttrName);
                } else if (refEntity.hasAttribute(refAttrName)) {
                    refAttr = refEntity.getAttribute(refAttrName);
                } else {
                    if (entityId == null) {
                        entityId = AtlasTypeUtil.toAtlasRelatedObjectId(entity, typeRegistry);
                    }

                    refAttr = entityId;
                }

                if (refAttr != null) {
                    refAttr = setRelationshipType(refAttr, relationshipType);
                }

                if (refAttr != null) {
                    refEntity.setRelationshipAttribute(refAttrName, refAttr);
                }

                addToReferredEntitiesToMove(guid);
            }
        }
    }
}
 
Example 6
Source File: EntityGraphRetriever.java    From atlas with Apache License 2.0 4 votes vote down vote up
private Object mapVertexToRelationshipAttribute(AtlasVertex entityVertex, AtlasEntityType entityType, String attributeName, AtlasEntity entity, AtlasEntityExtInfo entityExtInfo, boolean isMinExtInfo) throws AtlasBaseException {
    Object                ret                  = null;
    String                relationshipTypeName = graphHelper.getRelationshipTypeName(entityVertex, entityType, attributeName);
    AtlasRelationshipType relationshipType     = relationshipTypeName != null ? typeRegistry.getRelationshipTypeByName(relationshipTypeName) : null;

    if (relationshipType == null) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, "relationshipDef is null");
    }

    AtlasAttribute          attribute       = entityType.getRelationshipAttribute(attributeName, relationshipTypeName);
    AtlasRelationshipDef    relationshipDef = relationshipType.getRelationshipDef();
    AtlasRelationshipEndDef endDef1         = relationshipDef.getEndDef1();
    AtlasRelationshipEndDef endDef2         = relationshipDef.getEndDef2();
    AtlasEntityType         endDef1Type     = typeRegistry.getEntityTypeByName(endDef1.getType());
    AtlasEntityType         endDef2Type     = typeRegistry.getEntityTypeByName(endDef2.getType());
    AtlasRelationshipEndDef attributeEndDef = null;

    if (endDef1Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef1.getName(), attributeName)) {
        attributeEndDef = endDef1;
    } else if (endDef2Type.isTypeOrSuperTypeOf(entityType.getTypeName()) && StringUtils.equals(endDef2.getName(), attributeName)) {
        attributeEndDef = endDef2;
    }

    if (attributeEndDef == null) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID, relationshipDef.toString());
    }

    switch (attributeEndDef.getCardinality()) {
        case SINGLE:
            ret = mapRelatedVertexToObjectId(entityVertex, attribute, entityExtInfo, isMinExtInfo);
            break;

        case LIST:
        case SET:
            ret = mapRelationshipArrayAttribute(entityVertex, attribute, entityExtInfo, isMinExtInfo);
            break;
    }

    if (ret != null) {
        entity.setRelationshipAttribute(attributeName, ret);

        if (attributeEndDef.getIsLegacyAttribute() && !entity.hasAttribute(attributeName)) {
            entity.setAttribute(attributeName, toLegacyAttribute(ret));
        }
    }

    return ret;
}
 
Example 7
Source File: NiFiAtlasClient.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch existing NiFiFlow entity from Atlas.
 * @param rootProcessGroupId The id of a NiFi flow root process group.
 * @param namespace The namespace of a flow.
 * @return A NiFiFlow instance filled with retrieved data from Atlas. Status objects are left blank, e.g. ProcessorStatus.
 * @throws AtlasServiceException Thrown if requesting to Atlas API failed, including when the flow is not found.
 */
public NiFiFlow fetchNiFiFlow(String rootProcessGroupId, String namespace) throws AtlasServiceException {

    final String qualifiedName = AtlasUtils.toQualifiedName(namespace, rootProcessGroupId);
    final AtlasObjectId flowId = new AtlasObjectId(TYPE_NIFI_FLOW, ATTR_QUALIFIED_NAME, qualifiedName);
    final AtlasEntity.AtlasEntityWithExtInfo nifiFlowExt = searchEntityDef(flowId);

    if (nifiFlowExt == null || nifiFlowExt.getEntity() == null) {
        return null;
    }

    final AtlasEntity nifiFlowEntity = nifiFlowExt.getEntity();
    final Map<String, AtlasEntity> nifiFlowReferredEntities = nifiFlowExt.getReferredEntities();
    final Map<String, Object> attributes = nifiFlowEntity.getAttributes();
    final NiFiFlow nifiFlow = new NiFiFlow(rootProcessGroupId);
    nifiFlow.setExEntity(nifiFlowEntity);
    nifiFlow.setFlowName(toStr(attributes.get(ATTR_NAME)));
    nifiFlow.setNamespace(namespace);
    nifiFlow.setUrl(toStr(attributes.get(ATTR_URL)));
    nifiFlow.setDescription(toStr(attributes.get(ATTR_DESCRIPTION)));

    nifiFlow.getQueues().putAll(fetchFlowComponents(TYPE_NIFI_QUEUE, nifiFlowReferredEntities));
    nifiFlow.getRootInputPortEntities().putAll(fetchFlowComponents(TYPE_NIFI_INPUT_PORT, nifiFlowReferredEntities));
    nifiFlow.getRootOutputPortEntities().putAll(fetchFlowComponents(TYPE_NIFI_OUTPUT_PORT, nifiFlowReferredEntities));

    final Map<String, NiFiFlowPath> flowPaths = nifiFlow.getFlowPaths();
    final Map<AtlasObjectId, AtlasEntity> flowPathEntities = fetchFlowComponents(TYPE_NIFI_FLOW_PATH, nifiFlowReferredEntities);

    for (AtlasEntity flowPathEntity : flowPathEntities.values()) {
        final String pathQualifiedName = toStr(flowPathEntity.getAttribute(ATTR_QUALIFIED_NAME));
        final NiFiFlowPath flowPath = new NiFiFlowPath(getComponentIdFromQualifiedName(pathQualifiedName));
        if (flowPathEntity.hasAttribute(ATTR_URL)) {
            final Matcher urlMatcher = FLOW_PATH_URL_PATTERN.matcher(toStr(flowPathEntity.getAttribute(ATTR_URL)));
            if (urlMatcher.matches()) {
                flowPath.setGroupId(urlMatcher.group(1));
            }
        }
        flowPath.setExEntity(flowPathEntity);
        flowPath.setName(toStr(flowPathEntity.getAttribute(ATTR_NAME)));
        flowPath.getInputs().addAll(toQualifiedNameIds(toAtlasObjectIds(flowPathEntity.getAttribute(ATTR_INPUTS))).keySet());
        flowPath.getOutputs().addAll(toQualifiedNameIds(toAtlasObjectIds(flowPathEntity.getAttribute(ATTR_OUTPUTS))).keySet());
        flowPath.startTrackingChanges(nifiFlow);

        flowPaths.put(flowPath.getId(), flowPath);
    }

    nifiFlow.startTrackingChanges();
    return nifiFlow;
}