Friday, 2 September 2022

Atlas Client : Faceted search on entity attributes

By supplying entity filters, we can perform search on entity attirbutes.

 

Example

SearchParameters searchParameters = new SearchParameters();
searchParameters.setTypeName("DataSet");
searchParameters.setOffset(0);
searchParameters.setLimit(10);

SearchParameters.FilterCriteria attributeFilter = new SearchParameters.FilterCriteria();
attributeFilter.setAttributeName("name");
attributeFilter.setAttributeValue("Attr");
attributeFilter.setOperator(org.apache.atlas.model.discovery.SearchParameters.Operator.STARTS_WITH);

searchParameters.setEntityFilters(attributeFilter);

AtlasSearchResult atlasSearchResult = atlasClient.facetedSearch(searchParameters);

 

Above snippet return at most 10 entities that has the name starts with Attr.

 

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

 

FacetedSearchOnAttributes.java

package com.sample.app.search;

import java.util.Arrays;
import java.util.List;

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

public class FacetedSearchOnAttributes {

  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);

    SearchParameters.FilterCriteria attributeFilter = new SearchParameters.FilterCriteria();
    attributeFilter.setAttributeName("name");
    attributeFilter.setAttributeValue("Attr");
    attributeFilter.setOperator(org.apache.atlas.model.discovery.SearchParameters.Operator.STARTS_WITH);

    searchParameters.setEntityFilters(attributeFilter);

    AtlasSearchResult atlasSearchResult = atlasClient.facetedSearch(searchParameters);

    printAtlasSearchResult(atlasSearchResult, "Get all the first 10 in a ‘DataSet’ type");
  }
}

Sample Output

Get all the first 10 in a ‘DataSet’ type
________________________________________
  AttributeWithSetOfStrings_DEMO1
  AttributeTypeMap_DEMO1
  AttributeWithListOfStrings_DEMO1
  AttributeDefaultValue_DEMO1


Previous                                                    Next                                                    Home

No comments:

Post a Comment