Search in sources :

Example 1 with ExcelSink

use of edu.uci.ics.texera.dataflow.sink.excel.ExcelSink in project textdb by TextDB.

the class DownloadFileResource method downloadExcelFile.

@GET
@Path("/result")
public Response downloadExcelFile(@QueryParam("resultID") String resultID) throws JsonParseException, JsonMappingException, IOException {
    java.nio.file.Path resultFile = QueryPlanResource.resultDirectory.resolve(resultID + ".json");
    if (Files.notExists(resultFile)) {
        System.out.println(resultFile + " file does not found");
        return Response.status(Status.NOT_FOUND).build();
    }
    ArrayList<Tuple> result = new ObjectMapper().readValue(Files.readAllBytes(resultFile), TypeFactory.defaultInstance().constructCollectionLikeType(ArrayList.class, Tuple.class));
    if (result.size() == 0) {
        System.out.println(resultFile + " file is empty");
        return Response.status(Status.NOT_FOUND).build();
    }
    TupleSourceOperator tupleSource = new TupleSourceOperator(result, result.get(0).getSchema());
    ExcelSink excelSink = new ExcelSinkPredicate().newOperator();
    excelSink.setInputOperator(tupleSource);
    excelSink.open();
    excelSink.collectAllTuples();
    excelSink.close();
    StreamingOutput fileStream = new StreamingOutput() {

        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            byte[] data = Files.readAllBytes(excelSink.getFilePath());
            output.write(data);
            output.flush();
        }
    };
    return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition", "attachment; filename=result.xlsx").build();
}
Also used : OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ExcelSinkPredicate(edu.uci.ics.texera.dataflow.sink.excel.ExcelSinkPredicate) StreamingOutput(javax.ws.rs.core.StreamingOutput) TupleSourceOperator(edu.uci.ics.texera.dataflow.source.tuple.TupleSourceOperator) ExcelSink(edu.uci.ics.texera.dataflow.sink.excel.ExcelSink) Tuple(edu.uci.ics.texera.api.tuple.Tuple) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tuple (edu.uci.ics.texera.api.tuple.Tuple)1 ExcelSink (edu.uci.ics.texera.dataflow.sink.excel.ExcelSink)1 ExcelSinkPredicate (edu.uci.ics.texera.dataflow.sink.excel.ExcelSinkPredicate)1 TupleSourceOperator (edu.uci.ics.texera.dataflow.source.tuple.TupleSourceOperator)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1