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);
}
}
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"));
}
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"));
}
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;
}
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;
}
Aggregations