Community Articles
Find and share helpful community-sourced technical articles.
Labels (1)
Not applicable

Atlas API : Create Trait Type

Traits are Atlas types that act like decorators to provide a mechanism for classification. Traits can be applied to any Atlas entities (instances of classes). An entity can have any number of traits.

For example, a SECURITY_CLEARANCE trait could be defined with an attribute ‘security_level’. An Atlas entity (e.g. an instance of Person class) could be tagged with the SECURITY_CLEARANCE trait by providing a value for the trait’s ‘security_level’ attribute.

In order to use a trait, you first need to define the trait type. You can do that by posting to the Types resource of the Atlas REST APIs.

Types Resource

A type is the description of any representable item. In this case we are creating a trait type but the same resource is used to represent enums, structs and classes as well.

/types

POST

Submits a types definition containing a type definition representing a trait.

Request Body

Types Definition application/json

Response Body

Types Response application/json

Types Definition

Properties

enumTypes : Array of Type Definition for ENUM types

structTypes : Array of Type Definition for STRUCT types

traitTypes : Array of Type Definition for TRAIT types

classTypes : Array of Type Definition for CLASS types

Type Definition

Properties

superTypes : Array of super type names.

hierarchicalMetaTypeName : String : The trait class name.

typeName : String : The type name.

attributeDefinitions : Array of Attribute Definition.

Attribute Definition

Properties

name : String : The attribute name.

dataTypeName : String : The owning data type name.

multiplicity : String : OPTIONAL, REQUIRED, COLLECTION, SET

isComposite : Boolean : True if the attribute’s lifecycle is dependent on the enclosing type and is not just a reference.

isUnique : Boolean : True if the attribute must be unique.

isIndexable : Boolean : True if the attribute can be indexed.

reverseAttributeName : String : If this is a reference attribute, then the name of the attribute on the Class that this refers to.

Types Response

Properties

requestId : String : The unique id for the request.

types : Array of Type Name Definition.

Type Name Definition

Properties

name : String : The type name.

Example

The following example creates an Atlas trait type called EXPIRES_ON that has a single attribute called ‘expiry_date’.

POST

http://c6401.ambari.apache.org:21000/api/atlas/types

Body

{  
  "enumTypes": [],  
  "structTypes": [],  
  "traitTypes": [  
    {  
      "superTypes":[], 
      "hierarchicalMetaTypeName":
      "org.apache.atlas.typesystem.types.TraitType",   
      "typeName": "EXPIRES_ON", 
      "attributeDefinitions": [  
        { 
          "name": "expiry_date", 
          "dataTypeName": "date", 
          "multiplicity": "required", 
          "isComposite": false, 
          "isUnique": false, 
          "isIndexable": true, 
          "reverseAttributeName": null  
        }  
      ]  
    } 
  ],  
  "classTypes": []
}

Response

{  
  requestId:"qtp1796488937-14 - 1a194816-1c6f-451a-b706-6dc2f53139e5"  
  types:{  
    name:"EXPIRES_ON"
  }
}
3,244 Views
Comments
Expert Contributor

Hello,

Can you tell me which version of HDP and Atlas that you tested this with? I tried today with HDP 2.4, which comes with Atlas 0.5.0.2.4, and I'm getting an error regarding "Unable to deserialize json"

I'm using the following curl command to test:

curl -iv -d @./atlas_payload.json -H "Content-Type: application/json" -X POST http://sandbox.hortonworks.com:21000/api/atlas/types

Thanks!

Expert Contributor

I figured this out. I had left out dataTypeName as part of the attributeDefinitions.

Super Guru

@bhagan I am getting same error. did you flatten the json?

Expert Contributor
@Sunile Manjee

Yes, I did flatten the json. Here is what I used (all one line):

{"enumTypes":[],"structTypes":[],"traitTypes": [{"superTypes":[],"hierarchicalMetaTypeName":"org.apache.atlas.typesystem.types.TraitType","typeName":"EXPIRES_ON","attributeDefinitions":[{"name":"expiry_date","dataTypeName":"string","multiplicity":"required","isComposite":false,"isUnique":false,"isIndexable":true,"reverseAttributeName": null}]}],"classTypes":[]}

But for me, I had left out an attribute.