Showing posts with label atlas client. Show all posts
Showing posts with label atlas client. Show all posts

Sunday, 30 October 2022

Atlas Client: detach or remove term from an entity

AtlasClientV2#disassociateTermFromEntities method is used to remove a term from an entity or entities.

 

Signature

public void disassociateTermFromEntities(String termGuid, List<AtlasRelatedObjectId> relatedObjectIds) throws AtlasServiceException

 


To disassociate a term from an entity, we need to supply a relationshipGuid. You can get the relationshipGuid from entity response.

 

{
   "referredEntities": {},
   "entity": {
      "typeName": "DemoType6",
      "attributes": {
         "owner": null,
         "replicatedTo": null,
         "userDescription": null,
         "replicatedFrom": null,
         "qualifiedName": "AttributeDefaultValue_DEMO1",
         "displayName": null,
         "name": "AttributeDefaultValue_DEMO1",
         "x": 123,
         "description": null,
         "y": 2
      },
      "relationshipAttributes": {
         "inputToProcesses": [],
         "pipeline": null,
         "schema": [],
         "model": null,
         "meanings": [
            {
               "guid": "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
               "typeName": "AtlasGlossaryTerm",
               "entityStatus": "ACTIVE",
               "displayText": "sensitive",
               "relationshipType": "AtlasGlossarySemanticAssignment",
               "relationshipGuid": "c02d605e-6165-448e-a77c-0df916781fb0",
               "relationshipStatus": "ACTIVE",
               "relationshipAttributes": {
                  "typeName": "AtlasGlossarySemanticAssignment",
                  "attributes": {
                     "expression": null,
                     "createdBy": null,
                     "steward": null,
                     "confidence": null,
                     "description": null,
                     "source": null,
                     "status": null
                  }
               }
            }
         ],
         "outputFromProcesses": []
      },
      "classifications": [
         {
            "typeName": "sensitive_data",
            "attributes": {
               "durationInDays": 8
            },
            "entityGuid": "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
            "entityStatus": "ACTIVE",
            "propagate": true,
            "validityPeriods": [],
            "removePropagationsOnEntityDelete": false
         }
      ],
      "labels": []
   }
}

  ‘relationshipAttributes.meanings’ is an array. In this case we need parse the array ‘relationshipAttributes.meanings’ and fetch the term details that we want to detach and use it while removing.


Let’s remove the term ‘sensitive’ from the above entity (entityId = "e63cb343-675e-4cde-9d49-ceb43fb1aacb").

 

Find the below working application.

 

atlas-application.properties

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

 

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

 

DetachTermFromAnEntity.java

package com.sample.app.glossary;

import java.util.Arrays;
import java.util.Map;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.util.JsonUtil;

/**
 * We need a relationshipGuid to disassociate a term from the entity.
 * 
 * How to get the relationshipGuid? relationshipGuid
 * 
 * @author Krishna
 *
 */
public class DetachTermFromAnEntity {
	private static AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
			new String[] { "admin", "admin" });

	public static void detachTermFromAnEntity(String term, String entityId)
			throws JsonProcessingException, AtlasServiceException {
		AtlasEntityWithExtInfo atlasEntityWithExtInfo = atlasClient.getEntityByGuid(entityId);

		Map<String, Object> relationShipAttributes = atlasEntityWithExtInfo.getEntity().getRelationshipAttributes();
		Object meanings = relationShipAttributes.get("meanings");

		String json = JsonUtil.marshal(meanings);

		ObjectMapper mapper = new ObjectMapper();
		JsonNode jsonNode = mapper.readTree(json);

		if (!jsonNode.isArray()) {
			System.exit(0);
		}

		String relationShipGuid = null;
		String termId = null;
		int noOfElements = jsonNode.size();
		for (int i = 0; i < noOfElements; i++) {
			JsonNode node = jsonNode.get(i);

			if (!"AtlasGlossaryTerm".equals(node.get("typeName").asText())) {
				continue;
			}

			if (!term.equals(node.get("displayText").asText())) {
				continue;
			}

			relationShipGuid = node.get("relationshipGuid").asText();
			termId = node.get("guid").asText();

			break;
		}

		AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
		atlasRelatedObjectId.setGuid(entityId);
		atlasRelatedObjectId.setRelationshipGuid(relationShipGuid);

		atlasClient.disassociateTermFromEntities(termId, Arrays.asList(atlasRelatedObjectId));
	}

	public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {

		String termToRemove = "sensitive";

		String entityId = "e63cb343-675e-4cde-9d49-ceb43fb1aacb";

		detachTermFromAnEntity(termToRemove, entityId);

	}
}

 

After running above application, I can see that the term ‘sensitive’ is removed.

 


 


Previous                                                    Next                                                    Home

Thursday, 20 October 2022

Atlas Client: Assign term to entities

AtlasClientV2#assignTermToEntities method is used to assign term to entities.

 

Signature

public void assignTermToEntities(String termGuid, List<AtlasRelatedObjectId> relatedObjectIds) throws AtlasServiceException

 

Example

String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

String objectId = "4eea7a7e-c59b-46ee-b6e9-a998251dada2";
String typeName = "DemoType16";

AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
atlasRelatedObjectId.setGuid(objectId);
atlasRelatedObjectId.setTypeName(typeName);

List<AtlasRelatedObjectId> relatedObjectIds = Arrays.asList(atlasRelatedObjectId);

 


Find the below working application.

 

atlas-application.properties

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

AssignTermToEntities.java

package com.sample.app.glossary;

import java.util.Arrays;
import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

public class AssignTermToEntities {
	public static void main(String[] args) throws AtlasServiceException {
		AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
				new String[] { "admin", "admin" });

		String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

		String objectId = "4eea7a7e-c59b-46ee-b6e9-a998251dada2";
		String typeName = "DemoType16";

		AtlasRelatedObjectId atlasRelatedObjectId = new AtlasRelatedObjectId();
		atlasRelatedObjectId.setGuid(objectId);
		atlasRelatedObjectId.setTypeName(typeName);

		List<AtlasRelatedObjectId> relatedObjectIds = Arrays.asList(atlasRelatedObjectId);
		
		atlasClient.assignTermToEntities(termId, relatedObjectIds);
	}
}

You can observe that the term ‘sensitive’ is attached to the entity.



  

Previous                                                    Next                                                    Home

Wednesday, 19 October 2022

Atlas Client: Get entities assigned with a term

AtlasClientV2#getEntitiesAssignedWithTerm method is used to get the entities attached with given term.

 

Signature

public List<AtlasRelatedObjectId> getEntitiesAssignedWithTerm(String termGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

List<AtlasRelatedObjectId> entitiesAssignedToThisTerm = atlasClient.getEntitiesAssignedWithTerm(termId, null, 10, 0);



Find the below working application.

 

atlas-application.properties

 

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

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

 

GetEntitiesAssignedWithGivenTerm.java

package com.sample.app.glossary;

import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasRelatedObjectId;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class GetEntitiesAssignedWithGivenTerm {

  public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
    AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
        new String[] { "admin", "admin" });

    String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

    List<AtlasRelatedObjectId> entitiesAssignedToThisTerm = atlasClient.getEntitiesAssignedWithTerm(termId, null,
        10, 0);

    System.out.println(JsonUtil.prettyPrintJson(entitiesAssignedToThisTerm));
  }

}

 

Output

[ {
  "guid" : "95731109-eb31-44e9-896e-60652e119312",
  "typeName" : "DemoType7",
  "entityStatus" : "ACTIVE",
  "displayText" : "CreateEntities_DEMO1",
  "relationshipType" : "AtlasGlossarySemanticAssignment",
  "relationshipGuid" : "981b2432-c033-42f5-bf37-07e4c3e41fad",
  "relationshipStatus" : "ACTIVE",
  "relationshipAttributes" : {
    "typeName" : "AtlasGlossarySemanticAssignment",
    "attributes" : {
      "expression" : null,
      "createdBy" : null,
      "steward" : null,
      "confidence" : null,
      "description" : null,
      "source" : null,
      "status" : null
    }
  }
}, {
  "guid" : "e63cb343-675e-4cde-9d49-ceb43fb1aacb",
  "typeName" : "DemoType6",
  "entityStatus" : "ACTIVE",
  "displayText" : "AttributeDefaultValue_DEMO1",
  "relationshipType" : "AtlasGlossarySemanticAssignment",
  "relationshipGuid" : "c02d605e-6165-448e-a77c-0df916781fb0",
  "relationshipStatus" : "ACTIVE",
  "relationshipAttributes" : {
    "typeName" : "AtlasGlossarySemanticAssignment",
    "attributes" : {
      "expression" : null,
      "createdBy" : null,
      "steward" : null,
      "confidence" : null,
      "description" : null,
      "source" : null,
      "status" : null
    }
  }
} ]

 



Previous                                                    Next                                                    Home

Tuesday, 18 October 2022

Atlas Client: Delete glossary category by id

AtlasClientV2#deleteGlossaryCategoryByGuid method is used to delete the glossary category by id.

 

Signature

public void deleteGlossaryCategoryByGuid(String categoryGuid) throws AtlasServiceException 




Example

String categoryId = "80e199fb-f924-4470-adc1-8227b5650f6c";
atlasClient.deleteGlossaryCategoryByGuid(categoryId);

Find the below application.

 

atlas-application.properties

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

DeleteGlossaryCategoryById.java

package com.sample.app.glossary;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;

public class DeleteGlossaryCategoryById {

  public static void main(String[] args) throws AtlasServiceException {
    AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
        new String[] { "admin", "admin" });

    String categoryId = "80e199fb-f924-4470-adc1-8227b5650f6c";

    atlasClient.deleteGlossaryCategoryByGuid(categoryId);
  }

}

After running the application, category1 is deleted.




 

Previous                                                    Next                                                    Home