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);
}
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);
}
}
}
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()");
}
}
}
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);
}
}
}
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");
}
Aggregations