Search in sources :

Example 1 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class DefaultTypeSystem method createTraitInstance.

@Override
public void createTraitInstance(String guid, String typeName, Map<String, Object> properties) throws ResourceAlreadyExistsException {
    try {
        // not using the constructor with properties argument because it is marked 'InterfaceAudience.Private'
        Struct struct = new Struct(typeName);
        for (Map.Entry<String, Object> propEntry : properties.entrySet()) {
            struct.set(propEntry.getKey(), propEntry.getValue());
        }
        // add Taxonomy Namespace
        struct.set(TaxonomyResourceProvider.NAMESPACE_ATTRIBUTE_NAME, TaxonomyResourceProvider.TAXONOMY_NS);
        metadataService.addTrait(guid, metadataService.createTraitInstance(struct));
    } catch (IllegalArgumentException e) {
        // todo: unfortunately, IllegalArgumentException can be thrown for other reasons
        if (e.getMessage().contains("is already defined for entity")) {
            throw new ResourceAlreadyExistsException(String.format("Tag '%s' already associated with the entity", typeName));
        } else {
            throw e;
        }
    } catch (AtlasException e) {
        throw new CatalogRuntimeException(String.format("Unable to create trait instance '%s' in type system: %s", typeName, e), e);
    }
}
Also used : CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasException(org.apache.atlas.AtlasException) Map(java.util.Map) Struct(org.apache.atlas.typesystem.Struct)

Example 2 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testArrayOfStructs.

@Test
public void testArrayOfStructs() throws Exception {
    // Add array of structs
    TestUtils.dumpGraph(TestUtils.getGraph());
    final Struct partition1 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition1.set(NAME, "part1");
    final Struct partition2 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition2.set(NAME, "part2");
    List<Struct> partitions = new ArrayList<Struct>() {

        {
            add(partition1);
            add(partition2);
        }
    };
    table.set("partitions", partitions);
    String newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    List<Struct> partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // add a new element to array of struct
    final Struct partition3 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition3.set(NAME, "part3");
    partitions.add(partition3);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // remove one of the struct values
    partitions.remove(1);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // Update struct value within array of struct
    partitions.get(0).set(NAME, "part4");
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // add a repeated element to array of struct
    final Struct partition4 = new Struct(TestUtils.PARTITION_STRUCT_TYPE);
    partition4.set(NAME, "part4");
    partitions.add(partition4);
    table.set("partitions", partitions);
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("partitions"));
    partitionsActual = (List<Struct>) tableDefinition.get("partitions");
    assertPartitions(partitionsActual, partitions);
    // Remove all elements. Should set array attribute to null
    partitions.clear();
    newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNull(tableDefinition.get("partitions"));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testStructs.

@Test
public void testStructs() throws Exception {
    Struct serdeInstance = new Struct(TestUtils.SERDE_TYPE);
    serdeInstance.set(NAME, "serde1Name");
    serdeInstance.set("serde", "test");
    serdeInstance.set("description", "testDesc");
    table.set("serde1", serdeInstance);
    String newtableId = updateInstance(table).getUpdateEntities().get(0);
    assertEquals(newtableId, tableId._getId());
    String tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    Referenceable tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNotNull(tableDefinition.get("serde1"));
    Assert.assertTrue(serdeInstance.equalsContents(tableDefinition.get("serde1")));
    // update struct attribute
    serdeInstance.set("serde", "testUpdated");
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table.get(NAME));
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertTrue(serdeInstance.equalsContents(tableDefinition.get("serde1")));
    // set to null
    serdeInstance.setNull("description");
    updateInstance(table);
    tableDefinitionJson = metadataService.getEntityDefinitionJson(tableId._getId());
    tableDefinition = InstanceSerialization.fromJsonReferenceable(tableDefinitionJson, true);
    Assert.assertNull(((Struct) tableDefinition.get("serde1")).get("description"));
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-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 {
    JSONObject jsonResponse = callAPIWithBodyAndParams(API.GET_ALL_TRAIT_DEFINITIONS, null, guid, TRAIT_DEFINITIONS);
    List<JSONObject> traitDefList = extractResults(jsonResponse, AtlasClient.RESULTS, new ExtractOperation<JSONObject, JSONObject>());
    ArrayList<Struct> traitStructList = new ArrayList<>();
    for (JSONObject traitDef : traitDefList) {
        Struct traitStruct = InstanceSerialization.fromJsonStruct(traitDef.toString(), true);
        traitStructList.add(traitStruct);
    }
    return traitStructList;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) ArrayList(java.util.ArrayList) Struct(org.apache.atlas.typesystem.Struct)

Example 5 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class EntityNotificationImpl method getSuperTraits.

private static List<IStruct> getSuperTraits(String typeName, Map<String, Object> values, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> superTypes = new LinkedList<>();
    TraitType traitDef = typeSystem.getDataType(TraitType.class, typeName);
    Set<String> superTypeNames = traitDef.getAllSuperTypeNames();
    for (String superTypeName : superTypeNames) {
        TraitType superTraitDef = typeSystem.getDataType(TraitType.class, superTypeName);
        Map<String, Object> superTypeValues = new HashMap<>();
        FieldMapping fieldMapping = superTraitDef.fieldMapping();
        if (fieldMapping != null) {
            Set<String> superTypeAttributeNames = fieldMapping.fields.keySet();
            for (String superTypeAttributeName : superTypeAttributeNames) {
                if (values.containsKey(superTypeAttributeName)) {
                    superTypeValues.put(superTypeAttributeName, values.get(superTypeAttributeName));
                }
            }
        }
        IStruct superTrait = new Struct(superTypeName, superTypeValues);
        superTypes.add(superTrait);
        superTypes.addAll(getSuperTraits(superTypeName, values, typeSystem));
    }
    return superTypes;
}
Also used : HashMap(java.util.HashMap) TraitType(org.apache.atlas.typesystem.types.TraitType) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Aggregations

Struct (org.apache.atlas.typesystem.Struct)56 Test (org.testng.annotations.Test)35 IStruct (org.apache.atlas.typesystem.IStruct)29 Referenceable (org.apache.atlas.typesystem.Referenceable)25 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)17 TraitType (org.apache.atlas.typesystem.types.TraitType)12 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Id (org.apache.atlas.typesystem.persistence.Id)7 JSONObject (org.codehaus.jettison.json.JSONObject)7 BeforeTest (org.testng.annotations.BeforeTest)6 AfterTest (org.testng.annotations.AfterTest)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ITypedInstance (org.apache.atlas.typesystem.ITypedInstance)4 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)4 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)4 Date (java.util.Date)3