Search in sources :

Example 1 with AtlasObjectIdType

use of org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType in project atlas by apache.

the class AtlasStructFormatConverter method fromV2ToV1.

protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map<String, Object> attributes, ConverterContext context) throws AtlasBaseException {
    Map<String, Object> ret = null;
    boolean isEntityType = structType instanceof AtlasEntityType;
    if (MapUtils.isNotEmpty(attributes)) {
        ret = new HashMap<>();
        // Only process the requested/set attributes
        for (String attrName : attributes.keySet()) {
            AtlasAttribute attr = structType.getAttribute(attrName);
            if (attr == null) {
                LOG.warn("ignored unknown attribute {}.{}", structType.getTypeName(), attrName);
                continue;
            }
            AtlasType attrType = attr.getAttributeType();
            AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
            Object v2Value = attributes.get(attr.getName());
            if (v2Value != null && isEntityType && attr.isOwnedRef()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("{}: is ownedRef, attrType={}", attr.getQualifiedName(), attrType.getTypeName());
                }
                if (attrType instanceof AtlasArrayType) {
                    AtlasArrayType arrayType = (AtlasArrayType) attrType;
                    AtlasType elemType = arrayType.getElementType();
                    String elemTypeName;
                    if (elemType instanceof AtlasObjectIdType) {
                        elemTypeName = ((AtlasObjectIdType) elemType).getObjectType();
                    } else {
                        elemTypeName = elemType.getTypeName();
                    }
                    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(elemTypeName);
                    ;
                    if (entityType != null) {
                        Collection<?> arrayValue = (Collection<?>) v2Value;
                        List<AtlasEntity> entities = new ArrayList<>(arrayValue.size());
                        for (Object arrayElem : arrayValue) {
                            String entityGuid = getGuid(arrayElem);
                            AtlasEntity entity = StringUtils.isNotEmpty(entityGuid) ? context.getById(entityGuid) : null;
                            if (entity != null) {
                                entities.add(entity);
                            } else {
                                LOG.warn("{}: not replacing objIdList with entityList - entity not found guid={}", attr.getQualifiedName(), entityGuid);
                                entities = null;
                                break;
                            }
                        }
                        if (entities != null) {
                            v2Value = entities;
                            attrType = new AtlasArrayType(entityType);
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("{}: replaced objIdList with entityList", attr.getQualifiedName());
                            }
                        }
                    } else {
                        LOG.warn("{}: not replacing objIdList with entityList - elementType {} is not an entityType", attr.getQualifiedName(), elemTypeName);
                    }
                } else if (attrType instanceof AtlasObjectIdType) {
                    String entityGuid = getGuid(v2Value);
                    AtlasEntity entity = StringUtils.isNotEmpty(entityGuid) ? context.getById(entityGuid) : null;
                    AtlasEntityType entityType = entity != null ? typeRegistry.getEntityTypeByName(entity.getTypeName()) : null;
                    if (entity != null && entityType != null) {
                        v2Value = entity;
                        attrType = entityType;
                        attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("{}: replaced objId with entity guid={}", attr.getQualifiedName(), entityGuid);
                        }
                    } else {
                        LOG.warn("{}: not replacing objId with entity - entity not found guid={}", attr.getQualifiedName(), entityGuid);
                    }
                } else {
                    LOG.warn("{}: not replacing objId with entity - unexpected attribute-type {}", attr.getQualifiedName(), attrType.getTypeName());
                }
            }
            Object v1Value = attrConverter.fromV2ToV1(v2Value, attrType, context);
            ret.put(attr.getName(), v1Value);
        }
    }
    return ret;
}
Also used : AtlasObjectIdType(org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType) ArrayList(java.util.ArrayList) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) Collection(java.util.Collection)

Example 2 with AtlasObjectIdType

use of org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType in project atlas by apache.

the class EntityDiscoveryService method searchWithParameters.

@Override
@GraphTransaction
public AtlasSearchResult searchWithParameters(SearchParameters searchParameters) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(searchParameters);
    final QueryParams params = QueryParams.getNormalizedParams(searchParameters.getLimit(), searchParameters.getOffset());
    searchParameters.setLimit(params.limit());
    searchParameters.setOffset(params.offset());
    SearchContext context = new SearchContext(searchParameters, typeRegistry, graph, indexer.getVertexIndexKeys());
    // For future cancellations
    String searchID = searchTracker.add(context);
    try {
        List<AtlasVertex> resultList = context.getSearchProcessor().execute();
        // By default any attribute that shows up in the search parameter should be sent back in the response
        // If additional values are requested then the entityAttributes will be a superset of the all search attributes
        // and the explicitly requested attribute(s)
        Set<String> resultAttributes = new HashSet<>();
        Set<String> entityAttributes = new HashSet<>();
        if (CollectionUtils.isNotEmpty(searchParameters.getAttributes())) {
            resultAttributes.addAll(searchParameters.getAttributes());
        }
        if (CollectionUtils.isNotEmpty(context.getEntityAttributes())) {
            resultAttributes.addAll(context.getEntityAttributes());
        }
        AtlasEntityType entityType = context.getEntityType();
        if (entityType != null) {
            for (String resultAttribute : resultAttributes) {
                AtlasAttribute attribute = entityType.getAttribute(resultAttribute);
                if (attribute != null) {
                    AtlasType attributeType = attribute.getAttributeType();
                    if (attributeType instanceof AtlasArrayType) {
                        attributeType = ((AtlasArrayType) attributeType).getElementType();
                    }
                    if (attributeType instanceof AtlasEntityType || attributeType instanceof AtlasObjectIdType) {
                        entityAttributes.add(resultAttribute);
                    }
                }
            }
        }
        for (AtlasVertex atlasVertex : resultList) {
            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(atlasVertex, resultAttributes);
            if (searchParameters.getIncludeClassificationAttributes()) {
                entity.setClassifications(entityRetriever.getAllClassifications(atlasVertex));
            }
            ret.addEntity(entity);
            // populate ret.referredEntities
            for (String entityAttribute : entityAttributes) {
                Object attrValue = entity.getAttribute(entityAttribute);
                if (attrValue instanceof AtlasObjectId) {
                    AtlasObjectId objId = (AtlasObjectId) attrValue;
                    if (ret.getReferredEntities() == null) {
                        ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                    }
                    if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                        ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                    }
                } else if (attrValue instanceof Collection) {
                    Collection objIds = (Collection) attrValue;
                    for (Object obj : objIds) {
                        if (obj instanceof AtlasObjectId) {
                            AtlasObjectId objId = (AtlasObjectId) obj;
                            if (ret.getReferredEntities() == null) {
                                ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                            }
                            if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                                ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                            }
                        }
                    }
                }
            }
        }
    } finally {
        searchTracker.remove(searchID);
    }
    return ret;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasObjectIdType(org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 3 with AtlasObjectIdType

use of org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType in project incubator-atlas by apache.

the class EntityDiscoveryService method searchWithParameters.

@Override
@GraphTransaction
public AtlasSearchResult searchWithParameters(SearchParameters searchParameters) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(searchParameters);
    SearchContext context = new SearchContext(searchParameters, typeRegistry, graph, indexer.getVertexIndexKeys());
    // For future cancellations
    String searchID = searchTracker.add(context);
    try {
        List<AtlasVertex> resultList = context.getSearchProcessor().execute();
        // By default any attribute that shows up in the search parameter should be sent back in the response
        // If additional values are requested then the entityAttributes will be a superset of the all search attributes
        // and the explicitly requested attribute(s)
        Set<String> resultAttributes = new HashSet<>();
        Set<String> entityAttributes = new HashSet<>();
        if (CollectionUtils.isNotEmpty(searchParameters.getAttributes())) {
            resultAttributes.addAll(searchParameters.getAttributes());
        }
        if (CollectionUtils.isNotEmpty(context.getEntityAttributes())) {
            resultAttributes.addAll(context.getEntityAttributes());
        }
        for (String resultAttribute : resultAttributes) {
            AtlasAttribute attribute = context.getEntityType().getAttribute(resultAttribute);
            if (attribute != null) {
                AtlasType attributeType = attribute.getAttributeType();
                if (attributeType instanceof AtlasArrayType) {
                    attributeType = ((AtlasArrayType) attributeType).getElementType();
                }
                if (attributeType instanceof AtlasEntityType || attributeType instanceof AtlasObjectIdType) {
                    entityAttributes.add(resultAttribute);
                }
            }
        }
        for (AtlasVertex atlasVertex : resultList) {
            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(atlasVertex, resultAttributes);
            ret.addEntity(entity);
            // populate ret.referredEntities
            for (String entityAttribute : entityAttributes) {
                Object attrValue = entity.getAttribute(entityAttribute);
                if (attrValue instanceof AtlasObjectId) {
                    AtlasObjectId objId = (AtlasObjectId) attrValue;
                    if (ret.getReferredEntities() == null) {
                        ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                    }
                    if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                        ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                    }
                } else if (attrValue instanceof Collection) {
                    Collection objIds = (Collection) attrValue;
                    for (Object obj : objIds) {
                        if (obj instanceof AtlasObjectId) {
                            AtlasObjectId objId = (AtlasObjectId) obj;
                            if (ret.getReferredEntities() == null) {
                                ret.setReferredEntities(new HashMap<String, AtlasEntityHeader>());
                            }
                            if (!ret.getReferredEntities().containsKey(objId.getGuid())) {
                                ret.getReferredEntities().put(objId.getGuid(), entityRetriever.toAtlasEntityHeader(objId.getGuid()));
                            }
                        }
                    }
                }
            }
        }
    } finally {
        searchTracker.remove(searchID);
    }
    return ret;
}
Also used : AtlasObjectIdType(org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasObjectIdType (org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)2 AtlasSearchResult (org.apache.atlas.model.discovery.AtlasSearchResult)2 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)1 QueryParams (org.apache.atlas.query.QueryParams)1 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)1 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)1 AtlasType (org.apache.atlas.type.AtlasType)1