Search in sources :

Example 1 with OutputFormat

use of org.apache.asterix.translator.SessionConfig.OutputFormat in project asterixdb by apache.

the class RestApiServlet method initResponse.

/**
     * Initialize the Content-Type of the response, and construct a
     * SessionConfig with the appropriate output writer and output-format
     * based on the Accept: header and other servlet parameters.
     */
static SessionOutput initResponse(IServletRequest request, IServletResponse response) throws IOException {
    HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_PLAIN, HttpUtil.Encoding.UTF8);
    // CLEAN_JSON output is the default; most generally useful for a
    // programmatic HTTP API
    OutputFormat format = OutputFormat.CLEAN_JSON;
    // First check the "output" servlet parameter.
    String output = request.getParameter("output");
    String accept = request.getHeader("Accept", "");
    if (output != null) {
        if ("CSV".equals(output)) {
            format = OutputFormat.CSV;
        } else if ("ADM".equals(output)) {
            format = OutputFormat.ADM;
        }
    } else {
        // Second check the Accept: HTTP header.
        if (accept.contains("application/x-adm")) {
            format = OutputFormat.ADM;
        } else if (accept.contains("text/csv")) {
            format = OutputFormat.CSV;
        }
    }
    if (format == OutputFormat.CLEAN_JSON && ("true".equals(request.getParameter("lossless")) || accept.contains("lossless=true"))) {
        format = OutputFormat.LOSSLESS_JSON;
    }
    SessionOutput.ResultAppender appendHandle = (app, handle) -> app.append("{ \"").append("handle").append("\":" + " \"").append(handle).append("\" }");
    SessionConfig sessionConfig = new SessionConfig(format);
    // If it's JSON or ADM, check for the "wrapper-array" flag. Default is
    // "true" for JSON and "false" for ADM. (Not applicable for CSV.)
    boolean wrapperArray = format == OutputFormat.CLEAN_JSON || format == OutputFormat.LOSSLESS_JSON;
    String wrapperParam = request.getParameter("wrapper-array");
    if (wrapperParam != null) {
        wrapperArray = Boolean.valueOf(wrapperParam);
    } else if (accept.contains("wrap-array=true")) {
        wrapperArray = true;
    } else if (accept.contains("wrap-array=false")) {
        wrapperArray = false;
    }
    sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, wrapperArray);
    // Now that format is set, output the content-type
    switch(format) {
        case ADM:
            HttpUtil.setContentType(response, "application/x-adm");
            break;
        case CLEAN_JSON:
        // No need to reflect "clean-ness" in output type; fall through
        case LOSSLESS_JSON:
            HttpUtil.setContentType(response, "application/json");
            break;
        case CSV:
            // Check for header parameter or in Accept:.
            if ("present".equals(request.getParameter("header")) || accept.contains("header=present")) {
                HttpUtil.setContentType(response, "text/csv; header=present");
                sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, true);
            } else {
                HttpUtil.setContentType(response, "text/csv; header=absent");
            }
            break;
        default:
            throw new IOException("Unknown format " + format);
    }
    return new SessionOutput(sessionConfig, response.writer(), null, null, appendHandle, null);
}
Also used : IStorageComponentProvider(org.apache.asterix.common.context.IStorageComponentProvider) ResultDelivery(org.apache.asterix.translator.IStatementExecutor.ResultDelivery) GlobalConfig(org.apache.asterix.common.config.GlobalConfig) AsterixException(org.apache.asterix.common.exceptions.AsterixException) OutputFormat(org.apache.asterix.translator.SessionConfig.OutputFormat) ICcApplicationContext(org.apache.asterix.common.dataflow.ICcApplicationContext) QueryTranslator(org.apache.asterix.app.translator.QueryTranslator) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConcurrentMap(java.util.concurrent.ConcurrentMap) Level(java.util.logging.Level) HyracksDataset(org.apache.hyracks.client.dataset.HyracksDataset) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) IServletResponse(org.apache.hyracks.http.api.IServletResponse) MetadataManager(org.apache.asterix.metadata.MetadataManager) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) ILangCompilationProvider(org.apache.asterix.compiler.provider.ILangCompilationProvider) SessionConfig(org.apache.asterix.translator.SessionConfig) IParser(org.apache.asterix.lang.common.base.IParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HYRACKS_CONNECTION_ATTR(org.apache.asterix.api.http.servlet.ServletConstants.HYRACKS_CONNECTION_ATTR) Logger(java.util.logging.Logger) Statement(org.apache.asterix.lang.common.base.Statement) IStatementExecutorFactory(org.apache.asterix.translator.IStatementExecutorFactory) AbstractServlet(org.apache.hyracks.http.server.AbstractServlet) List(java.util.List) HttpUtil(org.apache.hyracks.http.server.utils.HttpUtil) HYRACKS_DATASET_ATTR(org.apache.asterix.api.http.servlet.ServletConstants.HYRACKS_DATASET_ATTR) SessionOutput(org.apache.asterix.translator.SessionOutput) IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) ResultReader(org.apache.asterix.app.result.ResultReader) TokenMgrError(org.apache.asterix.lang.aql.parser.TokenMgrError) IServletRequest(org.apache.hyracks.http.api.IServletRequest) IParserFactory(org.apache.asterix.lang.common.base.IParserFactory) SessionOutput(org.apache.asterix.translator.SessionOutput) OutputFormat(org.apache.asterix.translator.SessionConfig.OutputFormat) SessionConfig(org.apache.asterix.translator.SessionConfig) IOException(java.io.IOException)

Example 2 with OutputFormat

use of org.apache.asterix.translator.SessionConfig.OutputFormat in project asterixdb by apache.

the class ApiServlet method post.

@Override
protected void post(IServletRequest request, IServletResponse response) {
    // Query language
    ILangCompilationProvider compilationProvider = "AQL".equals(request.getParameter("query-language")) ? aqlCompilationProvider : sqlppCompilationProvider;
    IParserFactory parserFactory = compilationProvider.getParserFactory();
    // Output format.
    PrintWriter out = response.writer();
    OutputFormat format;
    boolean csvAndHeader = false;
    String output = request.getParameter("output-format");
    try {
        format = OutputFormat.valueOf(output);
    } catch (IllegalArgumentException e) {
        LOGGER.log(Level.INFO, output + ": unsupported output-format, using " + OutputFormat.CLEAN_JSON + " instead", e);
        // Default output format
        format = OutputFormat.CLEAN_JSON;
    }
    String query = request.getParameter("query");
    String wrapperArray = request.getParameter("wrapper-array");
    String printExprParam = request.getParameter("print-expr-tree");
    String printRewrittenExprParam = request.getParameter("print-rewritten-expr-tree");
    String printLogicalPlanParam = request.getParameter("print-logical-plan");
    String printOptimizedLogicalPlanParam = request.getParameter("print-optimized-logical-plan");
    String printJob = request.getParameter("print-job");
    String executeQuery = request.getParameter("execute-query");
    try {
        response.setStatus(HttpResponseStatus.OK);
        HttpUtil.setContentType(response, ContentType.TEXT_HTML, Encoding.UTF8);
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Failure setting content type", e);
        response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
        return;
    }
    try {
        IHyracksClientConnection hcc = (IHyracksClientConnection) ctx.get(HYRACKS_CONNECTION_ATTR);
        IHyracksDataset hds = (IHyracksDataset) ctx.get(HYRACKS_DATASET_ATTR);
        if (hds == null) {
            synchronized (ctx) {
                hds = (IHyracksDataset) ctx.get(HYRACKS_DATASET_ATTR);
                if (hds == null) {
                    hds = new HyracksDataset(hcc, appCtx.getCompilerProperties().getFrameSize(), ResultReader.NUM_READERS);
                    ctx.put(HYRACKS_DATASET_ATTR, hds);
                }
            }
        }
        IParser parser = parserFactory.createParser(query);
        List<Statement> aqlStatements = parser.parse();
        SessionConfig sessionConfig = new SessionConfig(format, true, isSet(executeQuery), true);
        sessionConfig.set(SessionConfig.FORMAT_HTML, true);
        sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, csvAndHeader);
        sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, isSet(wrapperArray));
        sessionConfig.setOOBData(isSet(printExprParam), isSet(printRewrittenExprParam), isSet(printLogicalPlanParam), isSet(printOptimizedLogicalPlanParam), isSet(printJob));
        SessionOutput sessionOutput = new SessionOutput(sessionConfig, out);
        MetadataManager.INSTANCE.init();
        IStatementExecutor translator = statementExectorFactory.create(appCtx, aqlStatements, sessionOutput, compilationProvider, componentProvider);
        double duration;
        long startTime = System.currentTimeMillis();
        translator.compileAndExecute(hcc, hds, IStatementExecutor.ResultDelivery.IMMEDIATE, null, new IStatementExecutor.Stats());
        long endTime = System.currentTimeMillis();
        duration = (endTime - startTime) / 1000.00;
        out.println(HTML_STATEMENT_SEPARATOR);
        out.println("<PRE>Duration of all jobs: " + duration + " sec</PRE>");
    } catch (AsterixException | TokenMgrError | org.apache.asterix.aqlplus.parser.TokenMgrError pe) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.INFO, pe.toString(), pe);
        ResultUtil.webUIParseExceptionHandler(out, pe, query);
    } catch (Exception e) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
        ResultUtil.webUIErrorHandler(out, e);
    }
}
Also used : IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) SessionConfig(org.apache.asterix.translator.SessionConfig) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ILangCompilationProvider(org.apache.asterix.compiler.provider.ILangCompilationProvider) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) IParserFactory(org.apache.asterix.lang.common.base.IParserFactory) PrintWriter(java.io.PrintWriter) Statement(org.apache.asterix.lang.common.base.Statement) OutputFormat(org.apache.asterix.translator.SessionConfig.OutputFormat) TokenMgrError(org.apache.asterix.lang.aql.parser.TokenMgrError) IOException(java.io.IOException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IOException(java.io.IOException) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) HyracksDataset(org.apache.hyracks.client.dataset.HyracksDataset) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) SessionOutput(org.apache.asterix.translator.SessionOutput) IParser(org.apache.asterix.lang.common.base.IParser)

Aggregations

IOException (java.io.IOException)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)2 IParser (org.apache.asterix.lang.common.base.IParser)2 IParserFactory (org.apache.asterix.lang.common.base.IParserFactory)2 Statement (org.apache.asterix.lang.common.base.Statement)2 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)2 SessionConfig (org.apache.asterix.translator.SessionConfig)2 OutputFormat (org.apache.asterix.translator.SessionConfig.OutputFormat)2 SessionOutput (org.apache.asterix.translator.SessionOutput)2 IHyracksClientConnection (org.apache.hyracks.api.client.IHyracksClientConnection)2 IHyracksDataset (org.apache.hyracks.api.dataset.IHyracksDataset)2 HyracksDataset (org.apache.hyracks.client.dataset.HyracksDataset)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 PrintWriter (java.io.PrintWriter)1