Thursday 30 June 2022

Atlas client: Create new type definition

In this post, I am going to explain how to create a new entity type definition in Atlas using the bulk api ‘createAtlasTypeDefs’.

 

Step 1: Get an instance of AtlasClientV2.

AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" }, new String[] { "admin", "admin" });

 

Step 2: Define an instance of AtlasEntityDef.

AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
atlasEntityDef.setName("tablet");
atlasEntityDef.setCreatedBy("Krishna");
atlasEntityDef.setUpdatedBy("krishna");
atlasEntityDef.setDescription("Represent a laptoo specification");

 

Set super types to the entity that we want to create.

Set<String> superTypes = new HashSet<>();
superTypes.add("DataSet");
atlasEntityDef.setSuperTypes(superTypes);

 

Define and attach attribute definitions.

AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
attributeDef1.setName("screen_size");
attributeDef1.setTypeName("string");
attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
attributeDef1.setIsIndexable(true);
attributeDef1.setIsUnique(false);

AtlasAttributeDef attributeDef2 = new AtlasAttributeDef();
attributeDef2.setName("operating_system");
attributeDef2.setTypeName("string");
attributeDef2.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
attributeDef2.setIsIndexable(true);
attributeDef2.setIsUnique(false);

atlasEntityDef.setAttributeDefs(Arrays.asList(attributeDef1, attributeDef2));

Step 3: Define an instance of AtlasTypesDef and attach the entity definition to it.

AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
atlasTypesDef.getEntityDefs().add(atlasEntityDef);

Step 4: Define new type using ‘createAtlasTypeDefs’ method.

AtlasTypesDef atlasTypesDefResponse = atlasClient.createAtlasTypeDefs(atlasTypesDef);

Find the below working application.

 

Step 1: Create atlas-application.properties file in src/main/resources folder.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

Step 2: Define JsonUtil and CreateNewType classses.

 

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

CreateNewType.java

package com.sample.app.types;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.typedef.AtlasEntityDef;
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 CreateNewType {
	public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
		AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
				new String[] { "admin", "admin" });

		AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
		attributeDef1.setName("screen_size");
		attributeDef1.setTypeName("string");
		attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
		attributeDef1.setIsIndexable(true);
		attributeDef1.setIsUnique(false);

		AtlasAttributeDef attributeDef2 = new AtlasAttributeDef();
		attributeDef2.setName("operating_system");
		attributeDef2.setTypeName("string");
		attributeDef2.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
		attributeDef2.setIsIndexable(true);
		attributeDef2.setIsUnique(false);

		AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
		atlasEntityDef.setName("tablet");
		atlasEntityDef.setCreatedBy("Krishna");
		atlasEntityDef.setUpdatedBy("krishna");
		atlasEntityDef.setDescription("Represent a laptoo specification");
		atlasEntityDef.setAttributeDefs(Arrays.asList(attributeDef1, attributeDef2));

		Set<String> superTypes = new HashSet<>();
		superTypes.add("DataSet");
		atlasEntityDef.setSuperTypes(superTypes);

		AtlasTypesDef atlasTypesDef = new AtlasTypesDef();

		atlasTypesDef.getEntityDefs().add(atlasEntityDef);

		AtlasTypesDef atlasTypesDefResponse = atlasClient.createAtlasTypeDefs(atlasTypesDef);

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

	}
}

Output

{
  "enumDefs" : [ ],
  "structDefs" : [ ],
  "classificationDefs" : [ ],
  "entityDefs" : [ {
    "category" : "ENTITY",
    "guid" : "e7201b2c-a87d-49ef-aac0-c228b92f9763",
    "createdBy" : "admin",
    "updatedBy" : "admin",
    "createTime" : 1644561370044,
    "updateTime" : 1644561370044,
    "version" : 1,
    "name" : "tablet",
    "description" : "Represent a laptoo specification",
    "typeVersion" : "1.0",
    "attributeDefs" : [ {
      "name" : "screen_size",
      "typeName" : "string",
      "isOptional" : false,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 1,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : true,
      "includeInNotification" : false,
      "searchWeight" : -1
    }, {
      "name" : "operating_system",
      "typeName" : "string",
      "isOptional" : false,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 1,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : true,
      "includeInNotification" : false,
      "searchWeight" : -1
    } ],
    "superTypes" : [ "DataSet" ],
    "subTypes" : [ ],
    "relationshipAttributeDefs" : [ {
      "name" : "inputToProcesses",
      "typeName" : "array<Process>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "dataset_process_inputs",
      "isLegacyAttribute" : false
    }, {
      "name" : "pipeline",
      "typeName" : "spark_ml_pipeline",
      "isOptional" : true,
      "cardinality" : "SINGLE",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "spark_ml_pipeline_dataset",
      "isLegacyAttribute" : false
    }, {
      "name" : "schema",
      "typeName" : "array<avro_schema>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "avro_schema_associatedEntities",
      "isLegacyAttribute" : false
    }, {
      "name" : "model",
      "typeName" : "spark_ml_model",
      "isOptional" : true,
      "cardinality" : "SINGLE",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "spark_ml_model_dataset",
      "isLegacyAttribute" : false
    }, {
      "name" : "meanings",
      "typeName" : "array<AtlasGlossaryTerm>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "AtlasGlossarySemanticAssignment",
      "isLegacyAttribute" : false
    }, {
      "name" : "outputFromProcesses",
      "typeName" : "array<Process>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "process_dataset_outputs",
      "isLegacyAttribute" : false
    } ],
    "businessAttributeDefs" : { }
  } ],
  "relationshipDefs" : [ ],
  "businessMetadataDefs" : [ ]
}

You can confirm the same by login to Atlas UI.




Previous                                                    Next                                                    Home

No comments:

Post a Comment