Search in sources :

Example 26 with Id

use of org.apache.atlas.v1.model.instance.Id in project atlas by apache.

the class MetadataDiscoveryJerseyResourceIT method createInstance.

private Id createInstance() throws Exception {
    Referenceable entityInstance = new Referenceable("dsl_test_type", "Classification");
    entityInstance.set("name", randomString());
    entityInstance.set("description", randomString());
    Struct traitInstance = (Struct) entityInstance.getTrait("Classification");
    tagName = randomString();
    traitInstance.set("tag", tagName);
    List<String> traits = entityInstance.getTraitNames();
    assertEquals(traits.size(), 1);
    return createInstance(entityInstance);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 27 with Id

use of org.apache.atlas.v1.model.instance.Id in project atlas by apache.

the class DataSetLineageJerseyResourceIT method database.

Id database(String name, String description, String owner, String locationUri, String... traitNames) throws Exception {
    Referenceable referenceable = new Referenceable(DATABASE_TYPE, traitNames);
    referenceable.set(NAME, name);
    referenceable.set(QUALIFIED_NAME, name);
    referenceable.set(CLUSTER_NAME, locationUri + name);
    referenceable.set("description", description);
    referenceable.set("owner", owner);
    referenceable.set("locationUri", locationUri);
    referenceable.set("createTime", System.currentTimeMillis());
    return createInstance(referenceable);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable)

Example 28 with Id

use of org.apache.atlas.v1.model.instance.Id in project atlas by apache.

the class HookNotificationDeserializerTest method generateLargeEntityWithTrait.

private Referenceable generateLargeEntityWithTrait() {
    Referenceable ret = EntityNotificationTest.getEntity("id", new Struct("MyTrait", Collections.<String, Object>emptyMap()));
    // add 100 attributes, each with value of size 10k
    // Json Size=1,027,984; GZipped Size=16,387 ==> will compress, but not split
    // use the same value for all attributes - to aid better compression
    String attrValue = RandomStringUtils.randomAlphanumeric(10 * 1024);
    for (int i = 0; i < 100; i++) {
        ret.set("attr_" + i, attrValue);
    }
    return ret;
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 29 with Id

use of org.apache.atlas.v1.model.instance.Id in project atlas by apache.

the class EntityResource method updateByUniqueAttribute.

/**
 * Adds/Updates given entity identified by its unique attribute( entityType, attributeName and value)
 * Updates support only partial update of an entity - Adds/updates any new values specified
 * Updates do not support removal of attribute values
 *
 * @param entityType the entity type
 * @param attribute the unique attribute used to identify the entity
 * @param value the unique attributes value
 * @param request The updated entity json
 * @return response payload as json
 * The body contains the JSONArray of entity json. The service takes care of de-duping the entities based on any
 * unique attribute for the give type.
 */
@POST
@Path("qualifiedName")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateByUniqueAttribute(@QueryParam("type") String entityType, @QueryParam("property") String attribute, @QueryParam("value") String value, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    String entityJson = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateByUniqueAttribute(" + entityType + ", " + attribute + ", " + value + ")");
        }
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Partially updating entity by unique attribute {} {} {} {} ", entityType, attribute, value, entityJson);
        }
        Referenceable updatedEntity = AtlasType.fromV1Json(entityJson, Referenceable.class);
        entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
        attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
        value = ParamChecker.notEmpty(value, "attribute value cannot be null");
        Map<String, Object> attributes = new HashMap<>();
        attributes.put(attribute, value);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !AtlasTypeUtil.isAssignedGuid(updateId.getId())) {
            String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(getEntityType(entityType), attributes);
            updatedEntity.setId(new Id(guid, 0, updatedEntity.getTypeName()));
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntity(updatedEntity);
        EntityMutationResponse mutationResponse = entitiesStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), true);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        String response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.v1.model.instance.Id)

Example 30 with Id

use of org.apache.atlas.v1.model.instance.Id in project atlas by apache.

the class EntityJerseyResourceIT method testEntityDeduping.

@Test
public void testEntityDeduping() throws Exception {
    final String dbName = "db" + randomString();
    Referenceable HiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbIdReference = createInstance(HiveDBInstance);
    final String dbId = dbIdReference._getId();
    assertEntityAudit(dbId, EntityAuditEvent.EntityAuditAction.ENTITY_CREATE);
// Disabling DSL tests until v2 DSL implementation is ready
// JSONArray results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
// assertEquals(results.length(), 1);
// 
// //create entity again shouldn't create another instance with same unique attribute value
// List<String> entityResults = atlasClientV1.createEntity(HiveDBInstance);
// assertEquals(entityResults.size(), 0);
// 
// results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
// assertEquals(results.length(), 1);
// 
// //Test the same across references
// Referenceable table = new Referenceable(HIVE_TABLE_TYPE_BUILTIN);
// final String tableName = randomString();
// Referenceable tableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbIdReference);
// atlasClientV1.createEntity(tableInstance);
// results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
// assertEquals(results.length(), 1);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Id(org.apache.atlas.v1.model.instance.Id) Test(org.testng.annotations.Test)

Aggregations

Referenceable (org.apache.atlas.v1.model.instance.Referenceable)62 Test (org.testng.annotations.Test)39 Id (org.apache.atlas.v1.model.instance.Id)38 Struct (org.apache.atlas.v1.model.instance.Struct)15 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)11 List (java.util.List)7 AtlasException (org.apache.atlas.AtlasException)6 AtlasServiceException (org.apache.atlas.AtlasServiceException)6 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)5 HashMap (java.util.HashMap)4 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)4 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)4 Feed (org.apache.falcon.entity.v0.feed.Feed)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Consumes (javax.ws.rs.Consumes)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3