Wednesday 24 August 2022

Atlas Client: Get total count of results that matches to given query

When you ran a atlas search query, Atlas send ‘approximateCount’ as part of the response. ‘approximateCount’ return approximate results that match to given query.

 

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

 

ApproximateCountOfDocs.java

package com.sample.app.search;

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

public class ApproximateCountOfDocs {
	
	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(1);
		
		// Comment this if you want to consider sub types also
		searchParameters.setIncludeSubTypes(false);
		AtlasSearchResult atlasSearchResult = atlasClient.facetedSearch(searchParameters);
		
		System.out.println("Approximate count : " + atlasSearchResult.getApproximateCount());
		
	}

}

 

You can even count number of documents using dsl search.

 

ApproximateCountOfEntitiesByAdvnacedSearch.java
package com.sample.app.search;

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.AtlasSearchResult.AttributeSearchResult;

import com.fasterxml.jackson.core.JsonProcessingException;

public class ApproximateCountOfEntitiesByAdvnacedSearch {

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

		AtlasSearchResult atlasResult = atlasClient.dslSearch("DataSet select count()");

		AttributeSearchResult attributeSearchResult = atlasResult.getAttributes();

		List<List<Object>> values = attributeSearchResult.getValues();

		System.out.println("Approximate count : " + values.get(0).get(0));

	}

}

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment