Tuesday 23 August 2022

Atlas Client: facetedSearch: Get more control on performing basic search

Signature

public AtlasSearchResult facetedSearch(SearchParameters searchParameters) throws AtlasServiceException

  ‘SearchParameters’ class is very rich enough to support almost all the kinds of searches on entities.

 

public class SearchParameters implements Serializable {
    private static final long serialVersionUID = 1L;

    private String  query;
    private String  typeName;
    private String  classification;
    private String  termName;
    private String  sortBy;
    private boolean excludeDeletedEntities;
    private boolean includeClassificationAttributes;
    private boolean includeSubTypes                 = true;
    private boolean includeSubClassifications       = true;
    private int     limit;
    private int     offset;
    private String  marker;

    private FilterCriteria entityFilters;
    private FilterCriteria tagFilters;
    private Set<String>    attributes;
    private SortOrder      sortOrder;

    .....

     public static class FilterCriteria {
        // Can be presented as a group of conditions or a single condition
        public enum Condition { AND, OR }

        // Single condition
        private String   attributeName;
        private Operator operator;
        private String   attributeValue;

        // Complex conditions
        private Condition            condition;
        private List<FilterCriteria> criterion;
        .....
    }

    public enum Operator {
        LT(new String[]{"<", "lt"}),
        GT(new String[]{">", "gt"}),
        LTE(new String[]{"<=", "lte"}),
        GTE(new String[]{">=", "gte"}),
        EQ(new String[]{"=", "eq"}),
        NEQ(new String[]{"!=", "neq"}),
        IN(new String[]{"in", "IN"}),
        LIKE(new String[]{"like", "LIKE"}),
        STARTS_WITH(new String[]{"startsWith", "STARTSWITH", "begins_with", "BEGINS_WITH"}),
        ENDS_WITH(new String[]{"endsWith", "ENDSWITH", "ends_with", "ENDS_WITH"}),
        CONTAINS(new String[]{"contains", "CONTAINS"}),
        NOT_CONTAINS(new String[]{"not_contains", "NOT_CONTAINS"}),
        CONTAINS_ANY(new String[]{"containsAny", "CONTAINSANY", "contains_any", "CONTAINS_ANY"}),
        CONTAINS_ALL(new String[]{"containsAll", "CONTAINSALL", "contains_all", "CONTAINS_ALL"}),
        IS_NULL(new String[]{"isNull", "ISNULL", "is_null", "IS_NULL"}),
        NOT_NULL(new String[]{"notNull", "NOTNULL", "not_null", "NOT_NULL"}),
        TIME_RANGE(new String[]{"timerange", "TIMERANGE","time_range","TIME_RANGE"}),
        NOT_EMPTY(new String[]{"notEmpty", "NOTEMPTY", "not_empty", "NOT_EMPTY"})
        ;
        ......
        ......
    }
}

 

Example 1: Get all the first 10 entities in a ‘DataSet’ type

SearchParameters searchParameters = new SearchParameters();
searchParameters.setTypeName("DataSet");
searchParameters.setOffset(0);
searchParameters.setLimit(10);
AtlasSearchResult atlasSearchResult = atlasClient.facetedSearch(searchParameters);

 

Example 2: Get all the first 10 entities in a ‘DataSet’ type and sort by name.

searchParameters = new SearchParameters();
searchParameters.setTypeName("DataSet");
searchParameters.setOffset(0);
searchParameters.setLimit(10);
searchParameters.setSortBy("name");
searchParameters.setSortOrder(SortOrder.ASCENDING);
atlasSearchResult = atlasClient.facetedSearch(searchParameters);

 

Example 3: Get all the entities attached with sensitive_data classification.

searchParameters = new SearchParameters();
searchParameters.setClassification("sensitive_data");
searchParameters.setOffset(0);
searchParameters.setLimit(10);
atlasSearchResult = atlasClient.facetedSearch(searchParameters);

 

Example 4: Get all the first 10 entities in a ‘DataSet’ type that contain name 'sale'

 

searchParameters = new SearchParameters();
searchParameters.setTypeName("DataSet");
searchParameters.setOffset(0);
searchParameters.setLimit(10);
searchParameters.setSortBy("name");
searchParameters.setSortOrder(SortOrder.ASCENDING);

FilterCriteria entityFilters = new FilterCriteria();
entityFilters.setAttributeName("name");
entityFilters.setOperator(Operator.CONTAINS);
entityFilters.setAttributeValue("sale");
searchParameters.setEntityFilters(entityFilters);
atlasSearchResult = atlasClient.facetedSearch(searchParameters);

Example 5: Get all the first 10 entities in a ‘DataSet’ type that attached to 'sensitive_data' classification and the classification attribute 'DurationInDays' is set to 10

searchParameters = new SearchParameters();
searchParameters.setClassification("sensitive_data");
searchParameters.setOffset(0);
searchParameters.setLimit(10);
searchParameters.setSortBy("name");
searchParameters.setSortOrder(SortOrder.ASCENDING);

FilterCriteria tagFilters = new FilterCriteria();
tagFilters.setAttributeName("durationInDays");
tagFilters.setOperator(Operator.EQ);
tagFilters.setAttributeValue("10");
searchParameters.setTagFilters(tagFilters);
atlasSearchResult = atlasClient.facetedSearch(searchParameters);

Example 6: Search by text: Get all the entities that contain string 'sales'

searchParameters = new SearchParameters();
searchParameters.setQuery("sales");
atlasSearchResult = atlasClient.facetedSearch(searchParameters);

Find the below working application.

 

Create atlas-application.properties file under src/main/resources folder.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

Define FacetedSearchDemo class.

 

FacetedSearchDemo.java

package com.sample.app.search;

import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.SortOrder;
import org.apache.atlas.model.discovery.AtlasSearchResult;
import org.apache.atlas.model.discovery.SearchParameters;
import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria;
import org.apache.atlas.model.discovery.SearchParameters.Operator;
import org.apache.atlas.model.instance.AtlasEntityHeader;

public class FacetedSearchDemo {

    private static void printAtlasSearchResult(AtlasSearchResult atlasSearchResult, String message) {
        System.out.println("\n" + message);
        System.out.println("________________________________________");
        List<AtlasEntityHeader> entityHeaders = atlasSearchResult.getEntities();

        for (AtlasEntityHeader atlasEntityHeader : entityHeaders) {
            System.out.println("\t" + atlasEntityHeader.getAttribute("name"));
        }
    }

    public static void main(String[] args) throws AtlasServiceException {
        AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
                new String[] { "admin", "admin" });

        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setTypeName("DataSet");
        searchParameters.setOffset(0);
        searchParameters.setLimit(10);
        AtlasSearchResult atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult, "Get all the first 10 in a ‘DataSet’ type");

        searchParameters = new SearchParameters();
        searchParameters.setTypeName("DataSet");
        searchParameters.setOffset(0);
        searchParameters.setLimit(10);
        searchParameters.setSortBy("name");
        searchParameters.setSortOrder(SortOrder.ASCENDING);
        atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult, "Get all the first 10 in a ‘DataSet’ type and sort by name");

        searchParameters = new SearchParameters();
        searchParameters.setClassification("sensitive_data");
        searchParameters.setOffset(0);
        searchParameters.setLimit(10);
        atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult, "Get all the entities attached with sensitive_data classification");

        searchParameters = new SearchParameters();
        searchParameters.setTypeName("DataSet");
        searchParameters.setOffset(0);
        searchParameters.setLimit(10);
        searchParameters.setSortBy("name");
        searchParameters.setSortOrder(SortOrder.ASCENDING);

        FilterCriteria entityFilters = new FilterCriteria();
        entityFilters.setAttributeName("name");
        entityFilters.setOperator(Operator.CONTAINS);
        entityFilters.setAttributeValue("sale");
        searchParameters.setEntityFilters(entityFilters);
        atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult, "Get all the first 10 in a ‘DataSet’ type that contain name 'sale'");

        // Search by classification attribute
        searchParameters = new SearchParameters();
        searchParameters.setClassification("sensitive_data");
        searchParameters.setOffset(0);
        searchParameters.setLimit(10);
        searchParameters.setSortBy("name");
        searchParameters.setSortOrder(SortOrder.ASCENDING);

        FilterCriteria tagFilters = new FilterCriteria();
        tagFilters.setAttributeName("durationInDays");
        tagFilters.setOperator(Operator.EQ);
        tagFilters.setAttributeValue("10");
        searchParameters.setTagFilters(tagFilters);
        atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult,
                "Get all the first 10 in a ‘DataSet’ type that attached to 'sensitive_data' classification and the classificaiton attribute 'DurationInDays' is set to 10");
        
        // Free text search
        searchParameters = new SearchParameters();
        searchParameters.setQuery("sales");
        atlasSearchResult = atlasClient.facetedSearch(searchParameters);
        printAtlasSearchResult(atlasSearchResult,
                "get all the entities that contain string 'sales'");
        
        
    }

}

Output

Get all the first 10 in a ‘DataSet’ type
________________________________________
    sales_from_germany.txt
    global_sales.txt
    sales_from_Canada.txt
    profit_in_Germany.txt
    profit_in_Canada.txt
    products
    CreateEntity_DEMO1
    CreateEntity_DEMO1
    DeleteEntityById_DEMO1
    DeleteEntityById_DEMO1

Get all the first 10 in a ‘DataSet’ type and sort by name
________________________________________
    AttributeDefaultValue_DEMO1
    AttributeTypeMap_DEMO1
    AttributeWithListOfStrings_DEMO1
    AttributeWithSetOfStrings_DEMO1
    CreateEntities_DEMO1
    CreateEntities_DEMO2
    CreateEntity_DEMO1
    CreateEntity_DEMO1
    CreateEntity_DEMO1
    DeleteEntitiesByGuids_DEMO1

Get all the entities attached with sensitive_data classification
________________________________________
    products
    AttributeWithListOfStrings_DEMO1
    AttributeWithSetOfStrings_DEMO1
    AttributeTypeMap_DEMO1

Get all the first 10 in a ‘DataSet’ type that contain name 'sale'
________________________________________
    global_sales.txt
    sales
    sales_from_Canada.txt
    sales_from_germany.txt

Get all the first 10 in a ‘DataSet’ type that attached to 'sensitive_data' classification and the classificaiton attribute 'DurationInDays' is set to 10
________________________________________
    products
    AttributeWithSetOfStrings_DEMO1
    AttributeTypeMap_DEMO1

get all the entities that contain string 'sales'
________________________________________
    sales
    global_sales.txt
    mergeSalesDailyFromGermanyCanada


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment