Monday, 11 July 2022

Atlas Client: Set default value to an attribute

By setting the ‘defaultValue’ property to an attribute definition, you can explicitly set the default value to an attribute. This setting makes sense for optional attributes. If user do not set any value to the attribute, default value is used.

 

Example

"attributeDefs": [
    {
      "name": "x",
      "typeName": "int",
      "isOptional": true,
      "cardinality": "SINGLE",
      "valuesMinCount": 0,
      "valuesMaxCount": 1,
      "isUnique": false,
      "isIndexable": true,
      "includeInNotification": false,
      "defaultValue": "1",
      "searchWeight": -1
    },
    {
      "name": "y",
      "typeName": "int",
      "isOptional": true,
      "cardinality": "SINGLE",
      "valuesMinCount": 0,
      "valuesMaxCount": 1,
      "isUnique": false,
      "isIndexable": true,
      "includeInNotification": false,
      "defaultValue": "2",
      "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 AttributeDefaultValue class.

 

AttributeDefaultValue.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 AttributeDefaultValue {

  private static final String TYPE_NAME = "DemoType6";

  private static void createType(AtlasClientV2 atlasClient) throws AtlasServiceException {
    AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
    attributeDef1.setName("x");
    attributeDef1.setTypeName("int");
    attributeDef1.setIsIndexable(true);
    attributeDef1.setIsUnique(false);
    attributeDef1.setDefaultValue("1");
    attributeDef1.setIsOptional(true);

    AtlasAttributeDef attributeDef2 = new AtlasAttributeDef();
    attributeDef2.setName("y");
    attributeDef2.setTypeName("int");
    attributeDef2.setIsIndexable(true);
    attributeDef2.setIsUnique(false);
    attributeDef2.setDefaultValue("2");
    attributeDef2.setIsOptional(true);

    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, 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("x", 123);

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

  }
}

As you see the above snippet, I explicitly set the value of x to 123, but didn’t set the value for variable y (default value is used for y). You can confirm the same from Atlas UI.



 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment