Thursday, 16 June 2022

Apache Atlas: example to add relationship between two entities

In this post, I am going to explain how to add relationship between two entities.

 

Different types of relationship category

a.   Association: This is a relation with no containment

b.   Composition: This is a relation with containment. In this case, children can’t exist without container. Modification of one object alters the data on other object. For example, column doesn’t make any sense independently. It should be part of table. So while creating, we should create table and column together.

c.    Aggregation: This is a relation with containment. Here the lifecycle of container and its children is totally independent. Modification of one object does not alter the other object. For example, one car can have multiple wheels, and a car can’t move without a wheel, but the wheel can be independently used with scooter, or any other vehicle.

 

In this post, I am going to explain simple ASSOCIATION relationship between employee and address. If you want to know about AGGREGATION and COMPOSITION, you can refer my previous examples.

 

I am going to define two entity types and one relation type.

a.   Employee

b.   Address

c.    Employee-To-Address

 

employee.json

{
  "entityDefs": [
    {
      "category": "ENTITY",
      "name": "Employee",
      "description": "Used to model an employee",
      "superTypes": [
        "DataSet"
      ],
      "subTypes": [],
      "relationshipAttributeDefs": [],
      "businessAttributeDefs": {},
      "attributeDefs": [
        {
          "name": "first_name",
          "typeName": "string",
          "isOptional": true,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        },
        {
          "name": "last_name",
          "typeName": "string",
          "isOptional": true,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        },
        {
          "name": "id",
          "typeName": "long",
          "isOptional": true,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        }
      ]
    }
  ]
}

 

address.json

{
  "entityDefs": [
    {
      "category": "ENTITY",
      "name": "Address",
      "description": "Used to model an employee",
      "superTypes": [
        "DataSet"
      ],
      "subTypes": [],
      "relationshipAttributeDefs": [],
      "businessAttributeDefs": {},
      "attributeDefs": [
        {
          "name": "city",
          "typeName": "string",
          "isOptional": false,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        },
        {
          "name": "addressline",
          "typeName": "string",
          "isOptional": false,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        },
        {
          "name": "country",
          "typeName": "long",
          "isOptional": false,
          "cardinality": "SINGLE",
          "valuesMinCount": 0,
          "valuesMaxCount": 1,
          "isUnique": false,
          "isIndexable": false,
          "includeInNotification": false,
          "searchWeight": -1
        }
      ]
    }
  ]
}

 

employeeToAddress.json

{
  "relationshipDefs": [
    {
      "category": "RELATIONSHIP",
      "name": "Employee_To_Address",
      "description": "Specifies the relationship between employee and address",
      "attributeDefs": [],
      "relationshipCategory": "ASSOCIATION",
      "endDef1": {
        "type": "Employee",
        "name": "address_info",
        "isContainer": false,
        "cardinality": "SINGLE"
      },
      "endDef2": {
        "type": "Address",
        "name": "employee_details",
        "isContainer": false,
        "cardinality": "SINGLE"
      }
    }
  ]
}

 

Execute below commands to define types.

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/types/typedefs -d @employee.json

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/types/typedefs -d @address.json

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/types/typedefs -d @employeeToAddress.json

 

Define employee instance or entity

 

employeeInstance.json

{
  "entity": {
    "typeName": "Employee",
    "attributes": {
      "name": "Krishna Majety",
      "createTime": 1643363556,
      "qualifiedName": "krishna_majety123",
      "displayName": "Krishna Majety",
      "first_name": "krishna",
      "last_name": "Majety",
      "id": 1234
    }
  }
}

 

addressInstance.json

{
  "entity": {
    "typeName": "Address",
    "attributes": {
      "name": "Krishna Address",
      "createTime": 1643363556,
      "qualifiedName": "krishna_majety123_address",
      "city": "Bangalore",
      "country": 91,
      "addressline": "Chowdeswari street, Basavanagar"
    }
  }
}

Execute below command to define employee and address instances.

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/entity -d @employeeInstance.json

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/entity -d @addressInstance.json

$curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/entity -d @employeeInstance.json
{"mutatedEntities":{"CREATE":[{"typeName":"Employee","attributes":{"qualifiedName":"krishna_majety123","name":"Krishna Majety"},"guid":"0af59543-335f-4d71-ab2d-1263ae6c3dc1","status":"ACTIVE","displayText":"Krishna Majety","classificationNames":[],"classifications":[],"meaningNames":[],"meanings":[],"isIncomplete":false,"labels":[]}]},"guidAssignments":{"-43426249823799":"0af59543-335f-4d71-ab2d-1263ae6c3dc1"}}
$
$curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/entity -d @addressInstance.json
{"mutatedEntities":{"CREATE":[{"typeName":"Address","attributes":{"qualifiedName":"krishna_majety123_address","name":"Krishna Address"},"guid":"776deaa1-4db9-4c92-83cf-a631b5594113","status":"ACTIVE","displayText":"Krishna Address","classificationNames":[],"classifications":[],"meaningNames":[],"meanings":[],"isIncomplete":false,"labels":[]}]},"guidAssignments":{"-43426249823805":"776deaa1-4db9-4c92-83cf-a631b5594113"}}

From the output, I can confirm,

a.   Employee instance has the id "0af59543-335f-4d71-ab2d-1263ae6c3dc1"

b.   Address instance has the id "776deaa1-4db9-4c92-83cf-a631b5594113"

 

 

Let’s define the relationship between employee and address.

 

employeeToAddressInstance.json

{
  "typeName": "Employee_To_Address",
  "end1": {
    "guid": "0af59543-335f-4d71-ab2d-1263ae6c3dc1",
    "typeName": "Employee"
  },
  "end2": {
    "guid": "776deaa1-4db9-4c92-83cf-a631b5594113",
    "typeName": "Address"
  },
  "label": "rel-between-emp-address"
}

Execute below command to establish the relationship between Employee and Address.

curl -X POST -u admin:admin -H 'accept: application/json'  -H 'cache-control: no-cache'  -H 'content-type: application/json'  http://localhost:21000/api/atlas/v2/relationship -d @employeeToAddressInstance.json

Let’s go to Atlas UI and confirm the same. Search all entities of type Employee.




Click on ‘Krishna Majety’ and navigate to Relationship tab.

 

 


You can see the relationship ‘address_info’ is established. Click on hyper link ‘Krishna Adress’, it will take you to Address instance.




Here you can see the relationship to employee instance.

 

You can download the examples from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment