Friday 30 September 2022

Atlas Client: Get related terms of this term

AtlasClientV2#getRelatedTerms method return the related terms of this term.

 

Signature

public Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> getRelatedTerms(String termGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> relatedTerms = atlasClient.getRelatedTerms(termId, 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);
	}
}

GetRelatedTerms.java

package com.sample.app.glossary;

import java.util.Map;
import java.util.Set;

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

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

public class GetRelatedTerms {

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

    String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

    Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> relatedTerms = atlasClient.getRelatedTerms(termId, null, 10, 0);

    if (relatedTerms == null) {
      System.out.println("Related terms not exists");
    } else {
      System.out.println("Related terms : \n" + JsonUtil.prettyPrintJson(relatedTerms));
    }

  }

}

Output

Related terms : 
{
  "seeAlso" : [ {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "d807b7c7-f243-4541-94c5-8555bd689cfa",
    "displayText" : "term4"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "a6b631ef-3b5b-4260-a069-5368ab39ad78",
    "displayText" : "term2"
  } ]
}

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment