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.
No comments:
Post a Comment