Wednesday 27 July 2022

Atlas Client: Create new classification

In this post, I am going to explain how to define a classification along with the attributes.

 

Using 'AtlasClientV2#createAtlasTypeDefs()' method, you can create a classification.

 

Example

AtlasClassificationDef atlasClassificationDef = new AtlasClassificationDef();
atlasClassificationDef.setName("sensitive_data");
atlasClassificationDef.setDescription("Entities with sensitive data can classified as sensitive");
atlasClassificationDef.setCreatedBy("Krishna");
atlasClassificationDef.setUpdatedBy("Krishna");

AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
attributeDef1.setName("durationInDays");
attributeDef1.setTypeName("int");
attributeDef1.setDefaultValue("12");

atlasClassificationDef.getAttributeDefs().add(attributeDef1);

AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
atlasTypesDef.getClassificationDefs().add(atlasClassificationDef);

AtlasTypesDef atlasTypesDefResponse = atlasClient.createAtlasTypeDefs(atlasTypesDef);

 

Above snippet create a classification "sensitive_data" and attach an attribute "durationInDays" to it.

 

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

Define JsonUtil, CreateNewClassification classes.

 

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

CreateNewClassification.java

package com.sample.app.classifications;

import java.util.ArrayList;
import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;

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

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

		AtlasClassificationDef atlasClassificationDef = new AtlasClassificationDef();
		atlasClassificationDef.setName("sensitive_data");
		atlasClassificationDef.setDescription("Entities with sensitive data can classified as sensitive");
		atlasClassificationDef.setCreatedBy("Krishna");
		atlasClassificationDef.setUpdatedBy("Krishna");

		AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
		attributeDef1.setName("durationInDays");
		attributeDef1.setTypeName("int");
		attributeDef1.setDefaultValue("12");

		atlasClassificationDef.getAttributeDefs().add(attributeDef1);

		AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
		atlasTypesDef.getClassificationDefs().add(atlasClassificationDef);

		AtlasTypesDef atlasTypesDefResponse = atlasClient.createAtlasTypeDefs(atlasTypesDef);

		String json = JsonUtil.prettyPrintJson(atlasTypesDefResponse);
		System.out.println(json);

	}
}

Output

{
  "enumDefs" : [ ],
  "structDefs" : [ ],
  "classificationDefs" : [ {
    "category" : "CLASSIFICATION",
    "guid" : "d71e4578-3e55-4a86-ae04-c9c971ee0420",
    "createdBy" : "admin",
    "updatedBy" : "admin",
    "createTime" : 1644834992198,
    "updateTime" : 1644834992198,
    "version" : 1,
    "name" : "sensitive_data",
    "description" : "Entities with sensitive data can classified as sensitive",
    "typeVersion" : "1.0",
    "attributeDefs" : [ {
      "name" : "durationInDays",
      "typeName" : "int",
      "isOptional" : false,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 1,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "defaultValue" : "12",
      "searchWeight" : -1
    } ],
    "superTypes" : [ ],
    "entityTypes" : [ ],
    "subTypes" : [ ]
  } ],
  "entityDefs" : [ ],
  "relationshipDefs" : [ ],
  "businessMetadataDefs" : [ ]
}

You can confirm the same by login to Atlas UI application.



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment