Tuesday, 27 September 2022

Atlas Client: Get terms in a category

AtlasClientV2#getCategoryTerms method is used to get terms in a category.

 

Signature

public List<AtlasRelatedTermHeader> getCategoryTerms(String categoryGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

List<AtlasRelatedTermHeader> termsInACategory = atlasClient.getCategoryTerms(categoryId, null, 10, 0);

 

Find the below working application.

 

atlas-application.properties
atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

 

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

TermsInACategory.java

package com.sample.app.glossary;

import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class TermsInACategory {

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

    String categoryId = "80e199fb-f924-4470-adc1-8227b5650f6c";

    List<AtlasRelatedTermHeader> termsInACategory = atlasClient.getCategoryTerms(categoryId, null, 10, 0);

    if (termsInACategory == null) {
      System.out.println("no terms associated with this category");
    } else {
      System.out.println("terms : \n" + JsonUtil.prettyPrintJson(termsInACategory));
    }

  }

}

Output

terms : 
[ {
  "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
  "relationGuid" : "c1a5772f-290e-4696-a80c-5cc5a69b9bdd",
  "displayText" : "term1"
}, {
  "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
  "relationGuid" : "aa82b016-39bb-40ab-83fc-aa7f14c17e5a",
  "displayText" : "term2"
} ]




 

Previous                                                    Next                                                    Home

No comments:

Post a Comment