Search in sources :

Example 6 with Struct

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;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) Map(java.util.Map) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 7 with Struct

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));
}
Also used : EntityNotificationV1(org.apache.atlas.v1.model.notification.EntityNotificationV1) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 8 with Struct

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 9 with Struct

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;
}
Also used : HashMap(java.util.HashMap) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 10 with Struct

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);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Id(org.apache.atlas.v1.model.instance.Id) Struct(org.apache.atlas.v1.model.instance.Struct) Test(org.testng.annotations.Test)

Aggregations

Struct (org.apache.atlas.v1.model.instance.Struct)36 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)22 Test (org.testng.annotations.Test)17 HashMap (java.util.HashMap)9 Map (java.util.Map)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)9 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)7 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)6 Id (org.apache.atlas.v1.model.instance.Id)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)3 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasServiceException (org.apache.atlas.AtlasServiceException)2 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 EntityChangeListenerV2 (org.apache.atlas.listener.EntityChangeListenerV2)2