Monday 5 September 2022

Apache Atlas: Create glossary term

AtlasClientV2 provides 'createGlossaryTerm' method to create a glossary term.

 

Signature

public AtlasGlossaryTerm createGlossaryTerm(AtlasGlossaryTerm glossaryTerm) throws AtlasServiceException

public List<AtlasGlossaryTerm> createGlossaryTerms(List<AtlasGlossaryTerm> glossaryTerms) throws AtlasServiceException

 

Example

AtlasGlossaryTerm glossaryTerm = new AtlasGlossaryTerm();
glossaryTerm.setName("term1");
glossaryTerm.setShortDescription("term1...");
glossaryTerm.setLongDescription("term1...........");

AtlasGlossaryHeader atlasGlossaryHeader = new AtlasGlossaryHeader();
atlasGlossaryHeader.setGlossaryGuid(persistedGlossary.getGuid());
glossaryTerm.setAnchor(atlasGlossaryHeader);

 

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

 

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

CreateTerm.java

package com.sample.app.glossary;

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

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

public class CreateTerm {

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

    AtlasGlossary atlasGlossary = new AtlasGlossary();
    atlasGlossary.setName("myGlosary3");
    atlasGlossary.setShortDescription("Demo Glossary");
    atlasGlossary.setLongDescription("Demo GLossary.......");

    AtlasGlossary persistedGlossary = atlasClient.createGlossary(atlasGlossary);
    System.out.println("Glossary : \n" + JsonUtil.prettyPrintJson(persistedGlossary));

    AtlasGlossaryTerm glossaryTerm = new AtlasGlossaryTerm();
    glossaryTerm.setName("term1");
    glossaryTerm.setShortDescription("term1...");
    glossaryTerm.setLongDescription("term1...........");

    AtlasGlossaryHeader atlasGlossaryHeader = new AtlasGlossaryHeader();
    atlasGlossaryHeader.setGlossaryGuid(persistedGlossary.getGuid());
    glossaryTerm.setAnchor(atlasGlossaryHeader);

    AtlasGlossaryTerm persistedGLossaryTerm = atlasClient.createGlossaryTerm(glossaryTerm);
    System.out.println("\n\npersistedGLossaryTerm : \n" + JsonUtil.prettyPrintJson(persistedGLossaryTerm));

  }
}

 

Output

Glossary : 
{
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3",
  "shortDescription" : "Demo Glossary",
  "longDescription" : "Demo GLossary......."
}


persistedGLossaryTerm : 
{
  "guid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
  "qualifiedName" : "term1@myGlosary3",
  "name" : "term1",
  "shortDescription" : "term1...",
  "longDescription" : "term1...........",
  "anchor" : {
    "glossaryGuid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80"
  },
  "glossaryTermHeader" : {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "qualifiedName" : "term1@myGlosary3"
  }
}

 

You can confirm the same from Atlas UI.

 


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment