use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.
the class AtlasClient method updateEntity.
/**
* Supports Partial updates
* Updates properties set in the definition for the entity corresponding to guid
* @param entityType Type of the entity being updated
* @param uniqueAttributeName Attribute Name that uniquely identifies the entity
* @param uniqueAttributeValue Attribute Value that uniquely identifies the entity
* @param entity entity definition
*/
public EntityResult updateEntity(final String entityType, final String uniqueAttributeName, final String uniqueAttributeValue, Referenceable entity) throws AtlasServiceException {
final API api = API.UPDATE_ENTITY_PARTIAL;
String entityJson = InstanceSerialization.toJson(entity, true);
LOG.debug("Updating entity type: {}, attributeName: {}, attributeValue: {}, entity: {}", entityType, uniqueAttributeName, uniqueAttributeValue, entityJson);
JSONObject response = callAPIWithRetries(api, entityJson, new ResourceCreator() {
@Override
public WebResource createResource() {
WebResource resource = getResource(api, QUALIFIED_NAME);
resource = resource.queryParam(TYPE, entityType);
resource = resource.queryParam(ATTRIBUTE_NAME, uniqueAttributeName);
resource = resource.queryParam(ATTRIBUTE_VALUE, uniqueAttributeValue);
return resource;
}
});
EntityResult result = extractEntityResult(response);
LOG.debug("Update entity returned result: {}", result);
return result;
}
use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.
the class EntityJerseyResourceIT method addProperty.
private void addProperty(String guid, String property, String value) throws AtlasServiceException {
EntityResult entityResult = atlasClientV1.updateEntityAttribute(guid, property, value);
assertEquals(entityResult.getUpdateEntities().size(), 1);
assertEquals(entityResult.getUpdateEntities().get(0), guid);
}
use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.
the class EntityJerseyResourceIT method testPartialUpdate.
@Test
public void testPartialUpdate() 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 tableId = createInstance(hiveTableInstance);
final String guid = tableId._getId();
try {
Assert.assertNotNull(UUID.fromString(guid));
} catch (IllegalArgumentException e) {
Assert.fail("Response is not a guid, " + guid);
}
String colName = "col1" + randomString();
final List<Referenceable> columns = new ArrayList<>();
Map<String, Object> values = new HashMap<>();
values.put(NAME, colName);
values.put("comment", "col1 comment");
values.put(QUALIFIED_NAME, "default.table.col1@" + colName);
values.put("comment", "col1 comment");
values.put("type", "string");
values.put("owner", "user1");
values.put("position", 0);
values.put("description", "col1");
//table is a required reference, can't be null
values.put("table", tableId);
Referenceable ref = new Referenceable(BaseResourceIT.COLUMN_TYPE_BUILTIN, values);
columns.add(ref);
Referenceable tableUpdated = new Referenceable(BaseResourceIT.HIVE_TABLE_TYPE_BUILTIN, new HashMap<String, Object>() {
{
put("columns", columns);
}
});
LOG.debug("Updating entity= {}", tableUpdated);
EntityResult entityResult = atlasClientV1.updateEntity(guid, tableUpdated);
assertEquals(entityResult.getUpdateEntities().size(), 1);
assertEquals(entityResult.getUpdateEntities().get(0), guid);
Referenceable entity = atlasClientV1.getEntity(guid);
List<Referenceable> refs = (List<Referenceable>) entity.get("columns");
Assert.assertTrue(refs.get(0).equalsContents(columns.get(0)));
//Update by unique attribute
values.put("type", "int");
ref = new Referenceable(BaseResourceIT.COLUMN_TYPE_BUILTIN, values);
columns.set(0, ref);
tableUpdated = new Referenceable(BaseResourceIT.HIVE_TABLE_TYPE_BUILTIN, new HashMap<String, Object>() {
{
put("columns", columns);
}
});
LOG.debug("Updating entity= {}", tableUpdated);
entityResult = atlasClientV1.updateEntity(BaseResourceIT.HIVE_TABLE_TYPE_BUILTIN, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, (String) hiveTableInstance.get(QUALIFIED_NAME), tableUpdated);
assertEquals(entityResult.getUpdateEntities().size(), 2);
assertEquals(entityResult.getUpdateEntities().get(1), guid);
entity = atlasClientV1.getEntity(guid);
refs = (List<Referenceable>) entity.get("columns");
Assert.assertTrue(refs.get(0).getValuesMap().equals(values));
Assert.assertEquals(refs.get(0).get("type"), "int");
}
use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.
the class DataSetLineageServiceTest method testLineageWithDelete.
@Test(enabled = false)
public void testLineageWithDelete() throws Exception {
String tableName = "table" + random();
createTable(tableName, 3, true);
String tableId = getEntityId(HIVE_TABLE_TYPE, "name", tableName);
JSONObject results = getSchema(tableName);
assertEquals(results.getJSONArray("rows").length(), 3);
results = getInputsGraph(tableName);
Struct resultInstance = InstanceSerialization.fromJsonStruct(results.toString(), true);
Map<String, Struct> vertices = (Map) resultInstance.get("vertices");
assertEquals(vertices.size(), 2);
Struct vertex = vertices.get(tableId);
assertEquals(((Struct) vertex.get("vertexId")).get("state"), Id.EntityState.ACTIVE.name());
results = getOutputsGraph(tableName);
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 2);
results = new JSONObject(lineageService.getSchemaForEntity(tableId));
assertEquals(results.getJSONArray("rows").length(), 3);
results = new JSONObject(lineageService.getInputsGraphForEntity(tableId));
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 2);
results = new JSONObject(lineageService.getOutputsGraphForEntity(tableId));
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 2);
//Delete the entity. Lineage for entity returns the same results as before.
//Lineage for table name throws EntityNotFoundException
EntityResult deleteResult = repository.deleteEntities(Arrays.asList(tableId));
assertTrue(deleteResult.getDeletedEntities().contains(tableId));
results = new JSONObject(lineageService.getSchemaForEntity(tableId));
assertEquals(results.getJSONArray("rows").length(), 3);
results = new JSONObject(lineageService.getInputsGraphForEntity(tableId));
resultInstance = InstanceSerialization.fromJsonStruct(results.toString(), true);
vertices = (Map) resultInstance.get("vertices");
assertEquals(vertices.size(), 2);
vertex = vertices.get(tableId);
assertEquals(((Struct) vertex.get("vertexId")).get("state"), Id.EntityState.DELETED.name());
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 2);
results = new JSONObject(lineageService.getOutputsGraphForEntity(tableId));
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 2);
try {
getSchema(tableName);
fail("Expected EntityNotFoundException");
} catch (EntityNotFoundException e) {
//expected
}
try {
getInputsGraph(tableName);
fail("Expected EntityNotFoundException");
} catch (EntityNotFoundException e) {
//expected
}
try {
getOutputsGraph(tableName);
fail("Expected EntityNotFoundException");
} catch (EntityNotFoundException e) {
//expected
}
//Create table again should show new lineage
createTable(tableName, 2, false);
results = getSchema(tableName);
assertEquals(results.getJSONArray("rows").length(), 2);
results = getOutputsGraph(tableName);
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 0);
results = getInputsGraph(tableName);
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 0);
tableId = getEntityId(HIVE_TABLE_TYPE, "name", tableName);
results = new JSONObject(lineageService.getSchemaForEntity(tableId));
assertEquals(results.getJSONArray("rows").length(), 2);
results = new JSONObject(lineageService.getInputsGraphForEntity(tableId));
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 0);
results = new JSONObject(lineageService.getOutputsGraphForEntity(tableId));
assertEquals(results.getJSONObject("values").getJSONObject("vertices").length(), 0);
}
use of org.apache.atlas.model.legacy.EntityResult in project incubator-atlas by apache.
the class DefaultMetadataServiceTest method testDeleteEntities.
@Test
public void testDeleteEntities() throws Exception {
// Create a table entity, with 3 composite column entities
Referenceable dbEntity = createDBEntity();
String dbGuid = TestUtils.createInstance(metadataService, dbEntity);
Referenceable table1Entity = createTableEntity(dbGuid);
Referenceable col1 = createColumnEntity();
Referenceable col2 = createColumnEntity();
Referenceable col3 = createColumnEntity();
table1Entity.set(COLUMNS_ATTR_NAME, ImmutableList.of(col1, col2, col3));
TestUtils.createInstance(metadataService, table1Entity);
// Retrieve the table entities from the repository,
// to get their guids and the composite column guids.
String entityJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table1Entity.get(NAME));
Assert.assertNotNull(entityJson);
table1Entity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
List<IReferenceableInstance> table1Columns = (List<IReferenceableInstance>) table1Entity.get(COLUMNS_ATTR_NAME);
// Register an EntityChangeListener to verify the notification mechanism
// is working for deleteEntities().
EntitiesChangeListener listener = new EntitiesChangeListener();
metadataService.registerListener(listener);
//Delete one column
String columnId = table1Columns.get(0).getId()._getId();
EntityResult entityResult = deleteEntities(columnId);
//column is deleted and table is updated
assertEquals(entityResult.getDeletedEntities().get(0), columnId);
assertEquals(entityResult.getUpdateEntities().get(0), table1Entity.getId()._getId());
//verify listener was called for updates and deletes
assertEquals(entityResult.getDeletedEntities(), listener.getDeletedEntities());
assertEquals(entityResult.getUpdateEntities(), listener.getUpdatedEntities());
// Delete the table entities. The deletion should cascade
// to their composite columns.
entityResult = deleteEntities(table1Entity.getId()._getId());
// Verify that deleteEntities() response has guids for tables and their composite columns.
Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Entity.getId()._getId()));
Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Columns.get(1).getId()._getId()));
Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Columns.get(2).getId()._getId()));
// Verify that tables and their composite columns have been deleted from the repository.
assertEntityDeleted(TABLE_TYPE, NAME, table1Entity.get(NAME));
assertEntityDeleted(COLUMN_TYPE, NAME, col2.get(NAME));
assertEntityDeleted(COLUMN_TYPE, NAME, col3.get(NAME));
// Verify that the listener was notified about the deleted entities.
List<String> deletedEntitiesFromListener = listener.getDeletedEntities();
Assert.assertNotNull(deletedEntitiesFromListener);
assertEquals(deletedEntitiesFromListener.size(), entityResult.getDeletedEntities().size());
Assert.assertTrue(deletedEntitiesFromListener.containsAll(entityResult.getDeletedEntities()));
}
Aggregations