Sunday, 10 July 2022

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

By specifying the typeName as ‘map<string,string>’, we can set an attribute to take map of values.

"attributeDefs": [
    {
      "name": "myMap",
      "typeName": "map<string,string>",
      "isOptional": false,
      "cardinality": "SINGLE",
      "valuesMinCount": 1,
      "valuesMaxCount": 1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1
    }
  ]

 

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 AttributeTypeMap class.

 

AttributeTypeMap.java

 

package com.sample.app.attributes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
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 AttributeTypeMap {

  private static final String TYPE_NAME = "DemoType4";

  private static void createType(AtlasClientV2 atlasClient) throws AtlasServiceException {
    AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
    attributeDef1.setName("myMap");
    attributeDef1.setTypeName("map<string,string>");
    attributeDef1.setIsIndexable(false);
    attributeDef1.setIsUnique(false);

    AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
    atlasEntityDef.setName(TYPE_NAME);
    atlasEntityDef.setCreatedBy("Krishna");
    atlasEntityDef.setUpdatedBy("krishna");
    atlasEntityDef.setDescription("Represent a demo 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);

    Map<String, Object> map = new HashMap<>();
    map.put("name", "Krishna");
    map.put("age", "23");

    Map<String, String> address = new HashMap<>();
    address.put("city", "Bangalore");
    address.put("state", "Karnatka");

    map.put("address", address);

    atlasEntity.setAttribute("myMap", map);

    // Following are the mandatory attributes
    atlasEntity.setAttribute("qualifiedName", "AttributeTypeMap_DEMO1");
    atlasEntity.setAttribute("name", "AttributeTypeMap_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 ‘DemoType4’.

Click on the link ‘AttributeTypeMap_DEMO1’ to view the entity attributes.

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment