use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.
the class AtlasClassificationFormatConverter method fromV1ToV2.
@Override
public AtlasClassification fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
AtlasClassification ret = null;
if (v1Obj != null) {
AtlasClassificationType classificationType = (AtlasClassificationType) type;
if (v1Obj instanceof Map) {
final Map v1Map = (Map) v1Obj;
final Map v1Attribs = (Map) v1Map.get(ATTRIBUTES_PROPERTY_KEY);
if (MapUtils.isNotEmpty(v1Attribs)) {
ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, v1Attribs, ctx));
} else {
ret = new AtlasClassification(type.getTypeName());
}
} else if (v1Obj instanceof Struct) {
Struct struct = (Struct) v1Obj;
ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, struct.getValues(), ctx));
} else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or Struct", v1Obj.getClass().getCanonicalName());
}
}
return ret;
}
use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.
the class EntityNotificationIT method testAddTrait.
public void testAddTrait() throws Exception {
String superSuperTraitName = "SuperTrait" + randomString();
String superTraitName = "SuperTrait" + randomString();
traitName = "Trait" + randomString();
createTrait(superSuperTraitName);
createTrait(superTraitName, superSuperTraitName);
createTrait(traitName, superTraitName);
Struct traitInstance = new Struct(traitName);
String traitInstanceJSON = AtlasType.toV1Json(traitInstance);
LOG.debug("Trait instance = {}", traitInstanceJSON);
final String guid = tableId._getId();
atlasClientV1.addTrait(guid, traitInstance);
EntityNotificationV1 entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
Referenceable entity = entityNotification.getEntity();
assertTrue(entity.getTraitNames().contains(traitName));
List<Struct> allTraits = entityNotification.getAllTraits();
List<String> allTraitNames = new LinkedList<>();
for (Struct struct : allTraits) {
allTraitNames.add(struct.getTypeName());
}
assertTrue(allTraitNames.contains(traitName));
assertTrue(allTraitNames.contains(superTraitName));
assertTrue(allTraitNames.contains(superSuperTraitName));
String anotherTraitName = "Trait" + randomString();
createTrait(anotherTraitName, superTraitName);
traitInstance = new Struct(anotherTraitName);
traitInstanceJSON = AtlasType.toV1Json(traitInstance);
LOG.debug("Trait instance = {}", traitInstanceJSON);
atlasClientV1.addTrait(guid, traitInstance);
entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
allTraits = entityNotification.getAllTraits();
allTraitNames = new LinkedList<>();
for (Struct struct : allTraits) {
allTraitNames.add(struct.getTypeName());
}
assertTrue(allTraitNames.contains(traitName));
assertTrue(allTraitNames.contains(anotherTraitName));
// verify that the super type shows up twice in all traits
assertEquals(2, Collections.frequency(allTraitNames, superTraitName));
}
use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.
the class AtlasClient method listTraitDefinitions.
/**
* Get all trait definitions for an entity
* @param guid GUID of the entity
* @return List<String> trait definitions of the traits associated to the entity
* @throws AtlasServiceException
*/
public List<Struct> listTraitDefinitions(final String guid) throws AtlasServiceException {
ObjectNode jsonResponse = callAPIWithBodyAndParams(API_V1.GET_ALL_TRAIT_DEFINITIONS, null, guid, TRAIT_DEFINITIONS);
List<ObjectNode> traitDefList = extractResults(jsonResponse, AtlasClient.RESULTS, new ExtractOperation<ObjectNode, ObjectNode>());
ArrayList<Struct> traitStructList = new ArrayList<>();
for (ObjectNode traitDef : traitDefList) {
Struct traitStruct = AtlasType.fromV1Json(traitDef.toString(), Struct.class);
traitStructList.add(traitStruct);
}
return traitStructList;
}
use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.
the class EntityNotificationV1 method getAllTraits.
// ----- helper methods ----------------------------------------------------
private static List<Struct> getAllTraits(Referenceable entityDefinition, AtlasTypeRegistry typeRegistry) {
List<Struct> ret = new LinkedList<>();
for (String traitName : entityDefinition.getTraitNames()) {
Struct trait = entityDefinition.getTrait(traitName);
AtlasClassificationType traitType = typeRegistry.getClassificationTypeByName(traitName);
Set<String> superTypeNames = traitType != null ? traitType.getAllSuperTypes() : null;
ret.add(trait);
if (CollectionUtils.isNotEmpty(superTypeNames)) {
for (String superTypeName : superTypeNames) {
Struct superTypeTrait = new Struct(superTypeName);
if (MapUtils.isNotEmpty(trait.getValues())) {
AtlasClassificationType superType = typeRegistry.getClassificationTypeByName(superTypeName);
if (superType != null && MapUtils.isNotEmpty(superType.getAllAttributes())) {
Map<String, Object> superTypeTraitAttributes = new HashMap<>();
for (Map.Entry<String, Object> attrEntry : trait.getValues().entrySet()) {
String attrName = attrEntry.getKey();
if (superType.getAllAttributes().containsKey(attrName)) {
superTypeTraitAttributes.put(attrName, attrEntry.getValue());
}
}
superTypeTrait.setValues(superTypeTraitAttributes);
}
}
ret.add(superTypeTrait);
}
}
}
return ret;
}
use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.
the class EntityJerseyResourceIT method testAddTrait.
@Test
public void testAddTrait() throws Exception {
String dbName = "db" + randomString();
String tableName = "table" + randomString();
Referenceable hiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
Id dbId = createInstance(hiveDBInstance);
Referenceable hiveTableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
Id id = createInstance(hiveTableInstance);
final String guid = id._getId();
try {
Assert.assertNotNull(UUID.fromString(guid));
} catch (IllegalArgumentException e) {
Assert.fail("Response is not a guid, " + guid);
}
String traitName = "PII_Trait" + randomString();
TraitTypeDefinition piiTrait = TypesUtil.createTraitTypeDef(traitName, null, Collections.<String>emptySet());
String traitDefinitionAsJSON = AtlasType.toV1Json(piiTrait);
LOG.debug("traitDefinitionAsJSON = {}", traitDefinitionAsJSON);
TypesDef typesDef = new TypesDef(Collections.emptyList(), Collections.emptyList(), Collections.singletonList(piiTrait), Collections.emptyList());
createType(typesDef);
Struct traitInstance = new Struct(traitName);
atlasClientV1.addTrait(guid, traitInstance);
assertEntityAudit(guid, EntityAuditEvent.EntityAuditAction.TAG_ADD);
}
Aggregations