use of org.apache.hyracks.api.dataset.IHyracksDataset in project asterixdb by apache.
the class QueryResultApiServlet method get.
@Override
protected void get(IServletRequest request, IServletResponse response) throws Exception {
// TODO this seems wrong ...
HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_HTML, HttpUtil.Encoding.UTF8);
PrintWriter out = response.writer();
final String strHandle = localPath(request);
final ResultHandle handle = ResultHandle.parse(strHandle);
if (handle == null) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return;
}
IHyracksDataset hds = getHyracksDataset();
ResultReader resultReader = new ResultReader(hds, handle.getJobId(), handle.getResultSetId());
try {
DatasetJobRecord.Status status = resultReader.getStatus();
final HttpResponseStatus httpStatus;
if (status == null) {
httpStatus = HttpResponseStatus.NOT_FOUND;
} else {
switch(status.getState()) {
case SUCCESS:
httpStatus = HttpResponseStatus.OK;
break;
case RUNNING:
case IDLE:
case FAILED:
httpStatus = HttpResponseStatus.NOT_FOUND;
break;
default:
httpStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
break;
}
}
response.setStatus(httpStatus);
if (httpStatus != HttpResponseStatus.OK) {
return;
}
// QQQ The output format is determined by the initial
// query and cannot be modified here, so calling back to
// initResponse() is really an error. We need to find a
// way to send the same OutputFormat value here as was
// originally determined there. Need to save this value on
// some object that we can obtain here.
SessionOutput sessionOutput = RestApiServlet.initResponse(request, response);
ResultUtil.printResults(appCtx, resultReader, sessionOutput, new Stats(), null);
} catch (HyracksDataException e) {
final int errorCode = e.getErrorCode();
if (ErrorCode.NO_RESULTSET == errorCode) {
LOGGER.log(Level.INFO, "No results for: \"" + strHandle + "\"");
response.setStatus(HttpResponseStatus.NOT_FOUND);
return;
}
response.setStatus(HttpResponseStatus.BAD_REQUEST);
out.println(e.getMessage());
LOGGER.log(Level.WARNING, "Error retrieving result for \"" + strHandle + "\"", e);
} catch (Exception e) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
LOGGER.log(Level.WARNING, "Error retrieving result for \"" + strHandle + "\"", e);
}
if (out.checkError()) {
LOGGER.warning("Error flushing output writer for \"" + strHandle + "\"");
}
}
use of org.apache.hyracks.api.dataset.IHyracksDataset in project asterixdb by apache.
the class AbstractMultiNCIntegrationTest method runTest.
protected void runTest(JobSpecification spec) throws Exception {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(spec.toJSON().asText());
}
JobId jobId = hcc.startJob(spec, EnumSet.of(JobFlag.PROFILE_RUNTIME));
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(jobId.toString());
}
int nReaders = 1;
FrameManager resultDisplayFrameMgr = new FrameManager(spec.getFrameSize());
VSizeFrame resultFrame = new VSizeFrame(resultDisplayFrameMgr);
IFrameTupleAccessor frameTupleAccessor = new ResultFrameTupleAccessor();
if (!spec.getResultSetIds().isEmpty()) {
IHyracksDataset hyracksDataset = new HyracksDataset(hcc, spec.getFrameSize(), nReaders);
IHyracksDatasetReader reader = hyracksDataset.createReader(jobId, spec.getResultSetIds().get(0));
ObjectMapper om = new ObjectMapper();
ArrayNode resultRecords = om.createArrayNode();
ByteBufferInputStream bbis = new ByteBufferInputStream();
int readSize = reader.read(resultFrame);
while (readSize > 0) {
try {
frameTupleAccessor.reset(resultFrame.getBuffer());
for (int tIndex = 0; tIndex < frameTupleAccessor.getTupleCount(); tIndex++) {
int start = frameTupleAccessor.getTupleStartOffset(tIndex);
int length = frameTupleAccessor.getTupleEndOffset(tIndex) - start;
bbis.setByteBuffer(resultFrame.getBuffer(), start);
byte[] recordBytes = new byte[length];
bbis.read(recordBytes, 0, length);
resultRecords.add(new String(recordBytes, 0, length));
}
} finally {
try {
bbis.close();
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
readSize = reader.read(resultFrame);
}
}
hcc.waitForCompletion(jobId);
dumpOutputFiles();
}
Aggregations