Friday 8 July 2022

Atlas client: Create a type, where attribute type is a set

In this post, I am going to explain how can we define an attribute that takes list of string.

 

To define an attribute with type list, you should specify

a.   typeName as array<string>

b.   cardinality should be set to SET like below.

 

"attributeDefs": [
    {
      "name": "hobbies",
      "typeName": "array<string>",
      "isOptional": false,
      "cardinality": "SET",
      "valuesMinCount": 1,
      "valuesMaxCount": 2147483647,
      "isUnique": false,
      "isIndexable": true,
      "includeInNotification": false,
      "searchWeight": -1
    }
  ]

 

Create atlas-application.properties file under src/main/resources folder.

 

atlas-application.properties

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

 

Define AttributeWithSetOfStrings class.

 

AttributeWithSetOfStrings.java

package com.sample.app.attributes;

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.AtlasTypesDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;

import com.fasterxml.jackson.core.JsonProcessingException;

public class AttributeWithSetOfStrings {

  private static final String TYPE_NAME = "DemoType3";

  private static void createType(AtlasClientV2 atlasClient) throws AtlasServiceException {
    AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
    attributeDef1.setName("hobbies");
    attributeDef1.setTypeName("array<string>");
    attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SET);
    attributeDef1.setIsIndexable(true);
    attributeDef1.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));

    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("hobbies", Arrays.asList("football", "cricket", "football"));

    // Following are the mandatory attributes
    atlasEntity.setAttribute("qualifiedName", "AttributeWithSetOfStrings_DEMO1");
    atlasEntity.setAttribute("name", "AttributeWithSetOfStrings_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.

 

Open Atlas UI and query for the type ‘DemoType3’.

 

 


 

  Navigate to the entity ‘AttributeWithSetOfStrings_DEMO1’ to see the properties.

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment