Wednesday 6 July 2022

Atlas client: Create an entity

In this post, I am going to explain how to create an instance of a type.

 

For the demo purpose, I am going to define a type ‘DemoType1’ with some properties to it. Once the type is defined successfully, I am going to create an instance of the type ‘DemoType1’.

 

Follow below steps to define an instance of entity DemoType1.

 

Step 1: Define an instance of AtlasEntity and populate the attributes that you are interested.

AtlasEntity atlasEntity = new AtlasEntity();

atlasEntity.setTypeName(TYPE_NAME);

atlasEntity.setAttribute("a", 10);
atlasEntity.setAttribute("b", 20);

// Following are the mandatory attributes
atlasEntity.setAttribute("qualifiedName", "CreateEntity_DEMO1");
atlasEntity.setAttribute("name", "CreateEntity_DEMO1");

Step 2: Define an instance of AtlasEntityWithExtInfo, pass it to an argument to ‘createEntity’ method to define an entity.

AtlasEntity.AtlasEntityWithExtInfo atlasEntityWithExtInfo = new AtlasEntity.AtlasEntityWithExtInfo();
atlasEntityWithExtInfo.setEntity(atlasEntity);
atlasClient.createEntity(atlasEntityWithExtInfo);

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 ‘CreateEntity’ class.

 

CreateEntity.java


package com.sample.app.entity;

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.instance.AtlasEntity;
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;

public class CreateEntity {

    private static final String TYPE_NAME = "DemoType1";

    private static void createType(AtlasClientV2 atlasClient) throws AtlasServiceException {
        AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
        attributeDef1.setName("a");
        attributeDef1.setTypeName("int");
        attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
        attributeDef1.setIsIndexable(true);
        attributeDef1.setIsUnique(false);

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

        AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
        atlasEntityDef.setName(TYPE_NAME);
        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);

        atlasClient.createAtlasTypeDefs(atlasTypesDef);
    }

    private static void createEntity(AtlasClientV2 atlasClient) throws AtlasServiceException {

        AtlasEntity atlasEntity = new AtlasEntity();

        atlasEntity.setTypeName(TYPE_NAME);

        atlasEntity.setAttribute("a", 10);
        atlasEntity.setAttribute("b", 20);
        
        // Following are the mandatory attributes
        atlasEntity.setAttribute("qualifiedName", "CreateEntity_DEMO1");
        atlasEntity.setAttribute("name", "CreateEntity_DEMO1");

        AtlasEntity.AtlasEntityWithExtInfo atlasEntityWithExtInfo = new AtlasEntity.AtlasEntityWithExtInfo();
        atlasEntityWithExtInfo.setEntity(atlasEntity);

        atlasClient.createEntity(atlasEntityWithExtInfo);
    }

    public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {

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

        createType(atlasClient);
        createEntity(atlasClient);

    }
}

Run above application. Login to (http://localhost:21000/) Atlas portal and query for all the entities of type DemoType1, you will see an entity with name ‘CreateEntity_DEMO1’ is created in Atlas.




Click on the entity hyper link to see all the properties of the entity.





Previous                                                    Next                                                    Home

No comments:

Post a Comment