Search in sources :

Example 76 with Referenceable

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

the class HookNotificationTest method testNewMessageSerDe.

@Test
public void testNewMessageSerDe() throws Exception {
    Referenceable entity1 = new Referenceable("sometype");
    Referenceable entity2 = new Referenceable("newtype");
    entity1.set("attr", "value");
    entity1.set("complex", new Referenceable("othertype"));
    String user = "user";
    EntityCreateRequest request = new EntityCreateRequest(user, entity1, entity2);
    String notificationJson = AtlasType.toV1Json(request);
    HookNotification actualNotification = deserializer.deserialize(notificationJson);
    assertEquals(actualNotification.getType(), HookNotificationType.ENTITY_CREATE);
    assertEquals(actualNotification.getUser(), user);
    assertTrue(actualNotification instanceof EntityCreateRequest);
    EntityCreateRequest createRequest = (EntityCreateRequest) actualNotification;
    assertEquals(createRequest.getEntities().size(), 2);
    Referenceable actualEntity1 = createRequest.getEntities().get(0);
    assertEquals(actualEntity1.getTypeName(), "sometype");
    assertEquals(((Referenceable) actualEntity1.get("complex")).getTypeName(), "othertype");
    assertEquals(createRequest.getEntities().get(1).getTypeName(), "newtype");
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) EntityCreateRequest(org.apache.atlas.v1.model.notification.HookNotificationV1.EntityCreateRequest) Test(org.testng.annotations.Test)

Example 77 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable 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 78 with Referenceable

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

the class MetadataDiscoveryResource method searchUsingQueryDSL.

/**
 * Search using query DSL format.
 *
 * @param dslQuery search query in DSL format.
 * @param limit number of rows to be returned in the result, used for pagination. maxlimit > limit > 0. -1 maps to atlas.search.defaultlimit property value
 * @param offset offset to the results returned, used for pagination. offset >= 0. -1 maps to offset 0
 * Limit and offset in API are used in conjunction with limit and offset in DSL query
 * Final limit = min(API limit, max(query limit - API offset, 0))
 * Final offset = API offset + query offset
 *
 * @return JSON representing the type and results.
 */
@GET
@Path("search/dsl")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response searchUsingQueryDSL(@QueryParam("query") String dslQuery, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("limit") int limit, @DefaultValue(LIMIT_OFFSET_DEFAULT) @QueryParam("offset") int offset) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingQueryDSL({}, {}, {})", dslQuery, limit, offset);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingQueryDSL(" + dslQuery + ", " + limit + ", " + offset + ")");
        }
        dslQuery = ParamChecker.notEmpty(dslQuery, "dslQuery cannot be null");
        QueryParams queryParams = validateQueryParams(limit, offset);
        AtlasSearchResult result = atlasDiscoveryService.searchUsingDslQuery(dslQuery, queryParams.limit(), queryParams.offset());
        DSLSearchResult dslResult = new DSLSearchResult();
        dslResult.setQueryType(QUERY_TYPE_DSL);
        dslResult.setRequestId(Servlets.getRequestId());
        dslResult.setDataType(result.getType());
        dslResult.setQuery(result.getQueryText());
        dslResult.setCount(0);
        if (CollectionUtils.isNotEmpty(result.getEntities())) {
            for (AtlasEntityHeader entityHeader : result.getEntities()) {
                Referenceable entity = getEntity(entityHeader.getGuid());
                dslResult.addResult(entity);
            }
            if (dslResult.getResults() != null) {
                dslResult.setCount(dslResult.getResults().size());
            }
        } else if (result.getAttributes() != null && CollectionUtils.isNotEmpty(result.getAttributes().getName())) {
            List<String> attrNames = result.getAttributes().getName();
            for (List<Object> attrValues : result.getAttributes().getValues()) {
                if (attrValues == null) {
                    continue;
                }
                Referenceable entity = new Referenceable();
                for (int i = 0; i < attrNames.size(); i++) {
                    String attrName = attrNames.get(i);
                    Object attrValue = attrValues.size() > i ? attrValues.get(i) : null;
                    entity.set(attrName, attrValue);
                }
                dslResult.addResult(entity);
            }
            if (dslResult.getResults() != null) {
                dslResult.setCount(dslResult.getResults().size());
            }
        }
        String response = AtlasJson.toV1SearchJson(dslResult);
        return Response.ok(response).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingQueryDSL({}, {}, {})", dslQuery, limit, offset);
        }
    }
}
Also used : DSLSearchResult(org.apache.atlas.v1.model.discovery.DSLSearchResult) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) QueryParams(org.apache.atlas.query.QueryParams) List(java.util.List) AtlasSearchResult(org.apache.atlas.model.discovery.AtlasSearchResult) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 79 with Referenceable

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

the class MetadataDiscoveryResource method getEntity.

private Referenceable getEntity(String guid) throws AtlasBaseException {
    AtlasEntityWithExtInfo entity = entitiesStore.getById(guid);
    Referenceable referenceable = restAdapters.getReferenceable(entity);
    return referenceable;
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) Referenceable(org.apache.atlas.v1.model.instance.Referenceable)

Example 80 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable 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)143 Test (org.testng.annotations.Test)64 Id (org.apache.atlas.v1.model.instance.Id)37 Struct (org.apache.atlas.v1.model.instance.Struct)23 ArrayList (java.util.ArrayList)20 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)15 HashMap (java.util.HashMap)12 List (java.util.List)10 AtlasException (org.apache.atlas.AtlasException)10 HookNotification (org.apache.atlas.model.notification.HookNotification)10 Map (java.util.Map)9 EntityUpdateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityUpdateRequest)9 AtlasServiceException (org.apache.atlas.AtlasServiceException)8 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)8 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)7 EntityCreateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityCreateRequest)7 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)6 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)6 EntityNotificationTest (org.apache.atlas.notification.entity.EntityNotificationTest)5