Search in sources :

Example 96 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryTest method testFullTextSearch.

/**
     * Full text search requires GraphBackedSearchIndexer, and GraphBackedSearchIndexer can't be enabled in
     * GraphBackedDiscoveryServiceTest because of its test data. So, test for full text search is in
     * GraphBackedMetadataRepositoryTest:(
     */
@Test(dependsOnMethods = "testSubmitEntity")
public void testFullTextSearch() throws Exception {
    //todo fix this
    //Weird: with lucene, the test passes without sleep
    //but with elasticsearch, doesn't work without sleep. why??
    long sleepInterval = 1000;
    TestUtils.dumpGraph(TestUtils.getGraph());
    //person in hr department whose name is john
    Thread.sleep(sleepInterval);
    String response = discoveryService.searchByFullText("john", queryParams);
    Assert.assertNotNull(response);
    JSONArray results = new JSONArray(response);
    Assert.assertEquals(results.length(), 1);
    JSONObject row = (JSONObject) results.get(0);
    Assert.assertEquals(row.get("typeName"), "Person");
    //person in hr department who lives in santa clara
    response = discoveryService.searchByFullText("Jane AND santa AND clara", queryParams);
    Assert.assertNotNull(response);
    results = new JSONArray(response);
    Assert.assertEquals(results.length(), 1);
    row = (JSONObject) results.get(0);
    Assert.assertEquals(row.get("typeName"), "Manager");
    //search for person in hr department whose name starts is john/jahn
    response = discoveryService.searchByFullText("hr AND (john OR jahn)", queryParams);
    Assert.assertNotNull(response);
    results = new JSONArray(response);
    Assert.assertEquals(results.length(), 1);
    row = (JSONObject) results.get(0);
    Assert.assertEquals(row.get("typeName"), "Person");
    //verify limit and offset
    //higher limit should return all results
    results = new JSONArray(discoveryService.searchByFullText("Department", queryParams));
    assertEquals(results.length(), 5);
    //smaller limit should return those many rows
    results = new JSONArray(discoveryService.searchByFullText("Department", new QueryParams(2, 0)));
    assertEquals(results.length(), 2);
    //offset should offset the results
    results = new JSONArray(discoveryService.searchByFullText("Department", new QueryParams(5, 2)));
    assertEquals(results.length(), 3);
    //higher offset shouldn't return any rows
    results = new JSONArray(discoveryService.searchByFullText("Department", new QueryParams(2, 6)));
    assertEquals(results.length(), 0);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) QueryParams(org.apache.atlas.query.QueryParams) Test(org.testng.annotations.Test)

Example 97 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project incubator-atlas by apache.

the class EntityResource method getTraitDefinitionsForEntity.

/**
     * Fetches the trait definitions of all the traits associated to the given entity
     * @param guid globally unique identifier for the entity
     */
@GET
@Path("{guid}/traitDefinitions")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getTraitDefinitionsForEntity(@PathParam("guid") String guid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getTraitDefinitionsForEntity({})", guid);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getTraitDefinitionsForEntity(" + guid + ")");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching all trait definitions for entity={}", guid);
        }
        final List<AtlasClassification> classifications = entitiesStore.getClassifications(guid);
        JSONArray traits = new JSONArray();
        for (AtlasClassification classification : classifications) {
            IStruct trait = restAdapters.getTrait(classification);
            traits.put(new JSONObject(InstanceSerialization.toJson(trait, true)));
        }
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.RESULTS, traits);
        response.put(AtlasClient.COUNT, traits.length());
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get trait definition for entity {}", guid, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get trait definition for entity {}", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get trait definitions for entity {}", guid, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get trait definitions for entity {}", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getTraitDefinitionsForEntity({})", guid);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) IStruct(org.apache.atlas.typesystem.IStruct)

Example 98 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project incubator-atlas by apache.

the class TypesResource method submit.

/**
     * Submits a type definition corresponding to a given type representing a meta model of a
     * domain. Could represent things like Hive Database, Hive Table, etc.
     */
@POST
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response submit(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TypesResource.submit()");
    }
    AtlasPerfTracer perf = null;
    if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
        perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "TypesResource.submit()");
    }
    JSONArray typesResponse = new JSONArray();
    try {
        final String typeDefinition = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating type with definition {} ", typeDefinition);
        }
        AtlasTypesDef createTypesDef = TypeConverterUtil.toAtlasTypesDef(typeDefinition, typeRegistry);
        AtlasTypesDef createdTypesDef = typesREST.createAtlasTypeDefs(createTypesDef);
        List<String> typeNames = TypeConverterUtil.getTypeNames(createdTypesDef);
        for (int i = 0; i < typeNames.size(); i++) {
            final String name = typeNames.get(i);
            typesResponse.put(new JSONObject() {

                {
                    put(AtlasClient.NAME, name);
                }
            });
        }
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.TYPES, typesResponse);
        return Response.status(ClientResponse.Status.CREATED).entity(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Type creation failed", e);
        throw new WebApplicationException(Servlets.getErrorResponse(e));
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to persist types", e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist types", e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist types", e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== TypesResource.submit()");
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 99 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project incubator-atlas by apache.

the class MetadataDiscoveryResource method searchUsingGremlinQuery.

/**
     * Search using raw gremlin query format.
     *
     * @param gremlinQuery search query in raw gremlin format.
     * @return JSON representing the type and results.
     */
@GET
@Path("search/gremlin")
@Consumes(Servlets.JSON_MEDIA_TYPE)
@Produces(Servlets.JSON_MEDIA_TYPE)
@InterfaceAudience.Private
public Response searchUsingGremlinQuery(@QueryParam("query") String gremlinQuery) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> MetadataDiscoveryResource.searchUsingGremlinQuery({})", gremlinQuery);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "MetadataDiscoveryResource.searchUsingGremlinQuery(" + gremlinQuery + ")");
        }
        gremlinQuery = ParamChecker.notEmpty(gremlinQuery, "gremlinQuery cannot be null or empty");
        final List<Map<String, String>> results = discoveryService.searchByGremlin(gremlinQuery);
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.QUERY, gremlinQuery);
        response.put(AtlasClient.QUERY_TYPE, QUERY_TYPE_GREMLIN);
        JSONArray list = new JSONArray();
        for (Map<String, String> result : results) {
            list.put(new JSONObject(result));
        }
        response.put(AtlasClient.RESULTS, list);
        response.put(AtlasClient.COUNT, list.length());
        return Response.ok(response).build();
    } catch (DiscoveryException | IllegalArgumentException e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== MetadataDiscoveryResource.searchUsingGremlinQuery({})", gremlinQuery);
        }
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) JSONArray(org.codehaus.jettison.json.JSONArray) Map(java.util.Map) DiscoveryException(org.apache.atlas.discovery.DiscoveryException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 100 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project incubator-atlas by apache.

the class SqoopHookIT method assertEntityIsRegistered.

private String assertEntityIsRegistered(final String query) throws Exception {
    waitFor(MAX_WAIT_TIME, new Predicate() {

        @Override
        public boolean evaluate() throws Exception {
            JSONArray results = atlasClient.search(query, 10, 0);
            return results.length() > 0;
        }
    });
    JSONArray results = atlasClient.search(query, 10, 0);
    JSONObject row = results.getJSONObject(0).getJSONObject("t");
    return row.getString("id");
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray)

Aggregations

JSONArray (org.codehaus.jettison.json.JSONArray)302 JSONObject (org.codehaus.jettison.json.JSONObject)248 Test (org.junit.Test)111 WebResource (com.sun.jersey.api.client.WebResource)65 ClientResponse (com.sun.jersey.api.client.ClientResponse)64 JSONException (org.codehaus.jettison.json.JSONException)49 ArrayList (java.util.ArrayList)37 Test (org.testng.annotations.Test)30 Vertex (com.tinkerpop.blueprints.Vertex)20 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)20 HashMap (java.util.HashMap)18 Map (java.util.Map)17 Produces (javax.ws.rs.Produces)17 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)14 GET (javax.ws.rs.GET)13 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)13 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)12 IOException (java.io.IOException)11 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)9 Path (javax.ws.rs.Path)9