Search in sources :

Example 1 with AtlasClassificationType

use of org.apache.atlas.type.AtlasClassificationType in project atlas by apache.

the class AtlasEntityStoreV1 method validateAndNormalize.

private void validateAndNormalize(AtlasClassification classification) throws AtlasBaseException {
    AtlasClassificationType type = typeRegistry.getClassificationTypeByName(classification.getTypeName());
    if (type == null) {
        throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_FOUND, classification.getTypeName());
    }
    List<String> messages = new ArrayList<>();
    type.validateValue(classification, classification.getTypeName(), messages);
    if (!messages.isEmpty()) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, messages);
    }
    type.getNormalizedValue(classification);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType)

Example 2 with AtlasClassificationType

use of org.apache.atlas.type.AtlasClassificationType in project atlas by apache.

the class EntityGraphMapper method addTagPropagation.

private List<AtlasVertex> addTagPropagation(AtlasVertex classificationVertex, List<AtlasVertex> propagatedEntityVertices) {
    List<AtlasVertex> ret = null;
    if (CollectionUtils.isNotEmpty(propagatedEntityVertices) && classificationVertex != null) {
        String classificationName = getTypeName(classificationVertex);
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationName);
        for (AtlasVertex propagatedEntityVertex : propagatedEntityVertices) {
            AtlasEdge existingEdge = getPropagatedClassificationEdge(propagatedEntityVertex, classificationVertex);
            if (existingEdge != null) {
                continue;
            }
            String entityTypeName = getTypeName(propagatedEntityVertex);
            AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
            if (classificationType.canApplyToEntityType(entityType)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(" --> Adding propagated classification: [{}] to {} ({}) using edge label: [{}]", classificationName, getTypeName(propagatedEntityVertex), GraphHelper.getGuid(propagatedEntityVertex), CLASSIFICATION_LABEL);
                }
                if (ret == null) {
                    ret = new ArrayList<>();
                }
                ret.add(propagatedEntityVertex);
                graphHelper.addClassificationEdge(propagatedEntityVertex, classificationVertex, true);
                addToPropagatedTraitNames(propagatedEntityVertex, classificationName);
            }
        }
    }
    return ret;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 3 with AtlasClassificationType

use of org.apache.atlas.type.AtlasClassificationType in project atlas by apache.

the class EntityGraphRetriever method addTagPropagation.

private void addTagPropagation(AtlasVertex fromVertex, AtlasVertex toVertex, AtlasEdge edge) throws AtlasBaseException {
    final List<AtlasVertex> classificationVertices = getPropagationEnabledClassificationVertices(fromVertex);
    final List<AtlasVertex> impactedEntityVertices = CollectionUtils.isNotEmpty(classificationVertices) ? graphHelper.getIncludedImpactedVerticesWithReferences(toVertex, getRelationshipGuid(edge)) : null;
    if (CollectionUtils.isNotEmpty(impactedEntityVertices)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Propagate {} tags: from {} entity to {} entities", classificationVertices.size(), getTypeName(fromVertex), impactedEntityVertices.size());
        }
        for (AtlasVertex classificationVertex : classificationVertices) {
            String classificationName = getTypeName(classificationVertex);
            AtlasVertex associatedEntityVertex = getAssociatedEntityVertex(classificationVertex);
            AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationName);
            for (AtlasVertex impactedEntityVertex : impactedEntityVertices) {
                if (getClassificationEdge(impactedEntityVertex, classificationVertex) != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Classification edge already exists from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), classificationName);
                    }
                    continue;
                } else if (getPropagatedClassificationEdge(impactedEntityVertex, classificationVertex) != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Propagated classification edge already exists from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), CLASSIFICATION_LABEL);
                    }
                    continue;
                }
                String entityTypeName = getTypeName(impactedEntityVertex);
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
                if (!classificationType.canApplyToEntityType(entityType)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Not creating propagated classification edge from [{}] --> [{}][{}], classification is not applicable for entity type", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex));
                    }
                    continue;
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug(" --> Creating propagated classification edge from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), CLASSIFICATION_LABEL);
                }
                AtlasEdge existingEdge = getPropagatedClassificationEdge(impactedEntityVertex, classificationVertex);
                if (existingEdge != null) {
                    continue;
                }
                graphHelper.addClassificationEdge(impactedEntityVertex, classificationVertex, true);
                addToPropagatedTraitNames(impactedEntityVertex, classificationName);
            }
        }
    }
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 4 with AtlasClassificationType

use of org.apache.atlas.type.AtlasClassificationType in project atlas by apache.

the class EntityDiscoveryService method searchUsingBasicQuery.

@Override
@GraphTransaction
public AtlasSearchResult searchUsingBasicQuery(String query, String typeName, String classification, String attrName, String attrValuePrefix, boolean excludeDeletedEntities, int limit, int offset) throws AtlasBaseException {
    AtlasSearchResult ret = new AtlasSearchResult(AtlasQueryType.BASIC);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing basic search query: {} with type: {} and classification: {}", query, typeName, classification);
    }
    final QueryParams params = QueryParams.getNormalizedParams(limit, offset);
    Set<String> typeNames = null;
    Set<String> classificationNames = null;
    String attrQualifiedName = null;
    if (StringUtils.isNotEmpty(typeName)) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(UNKNOWN_TYPENAME, typeName);
        }
        typeNames = entityType.getTypeAndAllSubTypes();
        ret.setType(typeName);
    }
    if (StringUtils.isNotEmpty(classification)) {
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classification);
        if (classificationType == null) {
            throw new AtlasBaseException(CLASSIFICATION_NOT_FOUND, classification);
        }
        classificationNames = classificationType.getTypeAndAllSubTypes();
        ret.setClassification(classification);
    }
    boolean isAttributeSearch = StringUtils.isNotEmpty(attrName) || StringUtils.isNotEmpty(attrValuePrefix);
    boolean isGuidPrefixSearch = false;
    if (isAttributeSearch) {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        ret.setQueryType(AtlasQueryType.ATTRIBUTE);
        if (entityType != null) {
            AtlasAttribute attribute = null;
            if (StringUtils.isNotEmpty(attrName)) {
                attribute = entityType.getAttribute(attrName);
                if (attribute == null) {
                    throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, typeName);
                }
            } else {
                // if attrName is null|empty iterate defaultAttrNames to get attribute value
                final List<String> defaultAttrNames = new ArrayList<>(Arrays.asList("qualifiedName", "name"));
                Iterator<String> iter = defaultAttrNames.iterator();
                while (iter.hasNext() && attribute == null) {
                    attrName = iter.next();
                    attribute = entityType.getAttribute(attrName);
                }
            }
            if (attribute == null) {
                // for guid prefix search use gremlin and nullify query to avoid using fulltext
                // (guids cannot be searched in fulltext)
                isGuidPrefixSearch = true;
                query = null;
            } else {
                attrQualifiedName = attribute.getQualifiedName();
                String attrQuery = String.format("%s AND (%s *)", attrName, attrValuePrefix.replaceAll("\\.", " "));
                query = StringUtils.isEmpty(query) ? attrQuery : String.format("(%s) AND (%s)", query, attrQuery);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing attribute search attrName: {} and attrValue: {}", attrName, attrValuePrefix);
        }
    }
    // results in a faster and accurate results than using CONTAINS/CONTAINS_PREFIX filter on entityText property
    if (StringUtils.isNotEmpty(query)) {
        final String idxQuery = getQueryForFullTextSearch(query, typeName, classification);
        final int startIdx = params.offset();
        final int resultSize = params.limit();
        int resultIdx = 0;
        for (int indexQueryOffset = 0; ; indexQueryOffset += getMaxResultSetSize()) {
            final Iterator<Result<?, ?>> qryResult = graph.indexQuery(Constants.FULLTEXT_INDEX, idxQuery, indexQueryOffset).vertices();
            if (LOG.isDebugEnabled()) {
                LOG.debug("indexQuery: query=" + idxQuery + "; offset=" + indexQueryOffset);
            }
            if (!qryResult.hasNext()) {
                break;
            }
            while (qryResult.hasNext()) {
                AtlasVertex<?, ?> vertex = qryResult.next().getVertex();
                String vertexTypeName = GraphHelper.getTypeName(vertex);
                // skip non-entity vertices
                if (StringUtils.isEmpty(vertexTypeName) || StringUtils.isEmpty(GraphHelper.getGuid(vertex))) {
                    continue;
                }
                if (typeNames != null && !typeNames.contains(vertexTypeName)) {
                    continue;
                }
                if (classificationNames != null) {
                    List<String> traitNames = GraphHelper.getTraitNames(vertex);
                    if (CollectionUtils.isEmpty(traitNames) || !CollectionUtils.containsAny(classificationNames, traitNames)) {
                        continue;
                    }
                }
                if (isAttributeSearch) {
                    String vertexAttrValue = vertex.getProperty(attrQualifiedName, String.class);
                    if (StringUtils.isNotEmpty(vertexAttrValue) && !vertexAttrValue.startsWith(attrValuePrefix)) {
                        continue;
                    }
                }
                if (skipDeletedEntities(excludeDeletedEntities, vertex)) {
                    continue;
                }
                resultIdx++;
                if (resultIdx <= startIdx) {
                    continue;
                }
                AtlasEntityHeader header = entityRetriever.toAtlasEntityHeader(vertex);
                ret.addEntity(header);
                if (ret.getEntities().size() == resultSize) {
                    break;
                }
            }
            if (ret.getEntities() != null && ret.getEntities().size() == resultSize) {
                break;
            }
        }
    } else {
        final Map<String, Object> bindings = new HashMap<>();
        String basicQuery = "g.V()";
        if (classificationNames != null) {
            bindings.put("traitNames", classificationNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER);
        }
        if (typeNames != null) {
            bindings.put("typeNames", typeNames);
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER);
        }
        if (excludeDeletedEntities) {
            bindings.put("state", ACTIVE.toString());
            basicQuery += gremlinQueryProvider.getQuery(BASIC_SEARCH_STATE_FILTER);
        }
        if (isGuidPrefixSearch) {
            bindings.put("guid", attrValuePrefix + ".*");
            basicQuery += gremlinQueryProvider.getQuery(AtlasGremlinQuery.GUID_PREFIX_FILTER);
        }
        bindings.put("startIdx", params.offset());
        bindings.put("endIdx", params.offset() + params.limit());
        basicQuery += gremlinQueryProvider.getQuery(TO_RANGE_LIST);
        ScriptEngine scriptEngine = graph.getGremlinScriptEngine();
        try {
            Object result = graph.executeGremlinScript(scriptEngine, bindings, basicQuery, false);
            if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
                List queryResult = (List) result;
                Object firstElement = queryResult.get(0);
                if (firstElement instanceof AtlasVertex) {
                    for (Object element : queryResult) {
                        if (element instanceof AtlasVertex) {
                            ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) element));
                        } else {
                            LOG.warn("searchUsingBasicQuery({}): expected an AtlasVertex; found unexpected entry in result {}", basicQuery, element);
                        }
                    }
                }
            }
        } catch (ScriptException e) {
            throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, basicQuery);
        } finally {
            graph.releaseGremlinScriptEngine(scriptEngine);
        }
    }
    return ret;
}
Also used : AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) ScriptEngine(javax.script.ScriptEngine) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) AtlasFullTextResult(org.apache.atlas.model.discovery.AtlasSearchResult.AtlasFullTextResult) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Result(org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result) AttributeSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult.AttributeSearchResult) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) 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 5 with AtlasClassificationType

use of org.apache.atlas.type.AtlasClassificationType in project atlas by apache.

the class ExportService method addClassificationType.

private void addClassificationType(String typeName, ExportContext context) {
    if (!context.classificationTypes.contains(typeName)) {
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(typeName);
        addClassificationType(classificationType, context);
    }
}
Also used : AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType)

Aggregations

AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)35 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)12 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)8 AtlasTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry)8 Test (org.testng.annotations.Test)8 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)7 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)6 Struct (org.apache.atlas.v1.model.instance.Struct)6 Map (java.util.Map)5 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)5 AtlasStructType (org.apache.atlas.type.AtlasStructType)3 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ConverterContext (org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext)2 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)2 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)2 AtlasEnumType (org.apache.atlas.type.AtlasEnumType)2 AtlasMapType (org.apache.atlas.type.AtlasMapType)2