use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasEntityGraphDiscoveryV1 method visitStruct.
void visitStruct(AtlasStructType structType, Object val) throws AtlasBaseException {
if (structType == null || val == null) {
return;
}
AtlasStruct struct;
if (val instanceof AtlasStruct) {
struct = (AtlasStruct) val;
} else if (val instanceof Map) {
Map attributes = AtlasTypeUtil.toStructAttributes((Map) val);
struct = new AtlasStruct(structType.getTypeName(), attributes);
} else {
throw new AtlasBaseException(AtlasErrorCode.INVALID_STRUCT_VALUE, val.toString());
}
for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
AtlasType attrType = attribute.getAttributeType();
Object attrVal = struct.getAttribute(attribute.getName());
visitAttribute(attrType, attrVal);
}
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasStructFormatConverter method fromV1ToV2.
@Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
AtlasStruct ret = null;
if (v1Obj != null) {
AtlasStructType structType = (AtlasStructType) 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 AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs, converterContext));
} else {
ret = new AtlasStruct(type.getTypeName());
}
} else if (v1Obj instanceof IStruct) {
IStruct struct = (IStruct) v1Obj;
Map<String, Object> v1Attribs = null;
try {
v1Attribs = struct.getValuesMap();
} catch (AtlasException excp) {
LOG.error("IStruct.getValuesMap() failed", excp);
}
ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs, converterContext));
} else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", v1Obj.getClass().getCanonicalName());
}
}
return ret;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasStructFormatConverter method fromV2ToV1.
@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext converterContext) throws AtlasBaseException {
Struct ret = null;
if (v2Obj != null) {
AtlasStructType structType = (AtlasStructType) type;
if (v2Obj instanceof Map) {
final Map v2Map = (Map) v2Obj;
final Map v2Attribs;
if (v2Map.containsKey(ATTRIBUTES_PROPERTY_KEY)) {
v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY);
} else {
v2Attribs = v2Map;
}
if (MapUtils.isNotEmpty(v2Attribs)) {
ret = new Struct(type.getTypeName(), fromV2ToV1(structType, v2Attribs, converterContext));
} else {
ret = new Struct(type.getTypeName());
}
} else if (v2Obj instanceof AtlasStruct) {
AtlasStruct struct = (AtlasStruct) v2Obj;
ret = new Struct(type.getTypeName(), fromV2ToV1(structType, struct.getAttributes(), converterContext));
} else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasStruct", v2Obj.getClass().getCanonicalName());
}
}
return ret;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class EntityGraphRetriever method mapVertexToStruct.
private AtlasStruct mapVertexToStruct(AtlasVertex entityVertex, String edgeLabel, AtlasEdge edge, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
AtlasStruct ret = null;
if (edge == null) {
edge = graphHelper.getEdgeForLabel(entityVertex, edgeLabel);
}
if (GraphHelper.elementExists(edge)) {
final AtlasVertex referenceVertex = edge.getInVertex();
ret = new AtlasStruct(GraphHelper.getTypeName(referenceVertex));
mapAttributes(referenceVertex, ret, entityExtInfo);
}
return ret;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasEntityStoreV1Test method testArrayOfStructs.
@Test(dependsOnMethods = "testCreate")
public void testArrayOfStructs() throws Exception {
//Modify array of structs
AtlasEntity tableEntity = new AtlasEntity(tblEntity.getEntity());
AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntitiesWithExtInfo(tableEntity);
List<AtlasStruct> partitions = new ArrayList<AtlasStruct>() {
{
add(new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part1"));
add(new AtlasStruct(TestUtilsV2.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part2"));
}
};
tableEntity.setAttribute("partitions", partitions);
init();
EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
AtlasEntityHeader updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//add a new element to array of struct
partitions.add(new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part3"));
init();
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//remove one of the struct values
init();
partitions.remove(1);
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//Update struct value within array of struct
init();
partitions.get(0).setAttribute(TestUtilsV2.NAME, "part4");
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//add a repeated element to array of struct
partitions.add(new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "part4"));
init();
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
// Remove all elements. Should set array attribute to null
partitions.clear();
init();
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
}
Aggregations