Search in sources :

Example 1 with SessionOutput

use of org.apache.asterix.translator.SessionOutput in project asterixdb by apache.

the class QueryTranslatorTest method test.

@Test
public void test() throws Exception {
    List<Statement> statements = new ArrayList<>();
    SessionOutput mockSessionOutput = mock(SessionOutput.class);
    RunStatement mockRunStatement = mock(RunStatement.class);
    // Mocks AppContextInfo.
    CcApplicationContext mockAsterixAppContextInfo = mock(CcApplicationContext.class);
    ExternalProperties mockAsterixExternalProperties = mock(ExternalProperties.class);
    when(mockAsterixAppContextInfo.getExternalProperties()).thenReturn(mockAsterixExternalProperties);
    when(mockAsterixExternalProperties.getAPIServerPort()).thenReturn(19002);
    // Mocks AsterixClusterProperties.
    Cluster mockCluster = mock(Cluster.class);
    MasterNode mockMasterNode = mock(MasterNode.class);
    ClusterProperties mockClusterProperties = mock(ClusterProperties.class);
    setFinalStaticField(ClusterProperties.class.getDeclaredField("INSTANCE"), mockClusterProperties);
    when(mockClusterProperties.getCluster()).thenReturn(mockCluster);
    when(mockCluster.getMasterNode()).thenReturn(mockMasterNode);
    when(mockMasterNode.getClientIp()).thenReturn("127.0.0.1");
    IStatementExecutor aqlTranslator = new DefaultStatementExecutorFactory().create(mockAsterixAppContextInfo, statements, mockSessionOutput, new AqlCompilationProvider(), new StorageComponentProvider());
    List<String> parameters = new ArrayList<>();
    parameters.add("examples/pregelix-example-jar-with-dependencies.jar");
    parameters.add("org.apache.pregelix.example.PageRankVertex");
    parameters.add("-ip 10.0.2.15 -port 3199");
    when(mockRunStatement.getParameters()).thenReturn(parameters);
    // Test a customer command without "-cust-prop".
    List<String> cmds = (List<String>) PA.invokeMethod(aqlTranslator, "constructPregelixCommand(org.apache.asterix.lang.common.statement.RunStatement," + "String,String,String,String)", mockRunStatement, "fromDataverse", "fromDataset", "toDataverse", "toDataset");
    List<String> expectedCmds = Arrays.asList(new String[] { "bin/pregelix", "examples/pregelix-example-jar-with-dependencies.jar", "org.apache.pregelix.example.PageRankVertex", "-ip", "10.0.2.15", "-port", "3199", "-cust-prop", "pregelix.asterixdb.url=http://127.0.0.1:19002,pregelix.asterixdb.source=true,pregelix.asterixdb.sink=true,pregelix.asterixdb.input.dataverse=fromDataverse,pregelix.asterixdb.input.dataset=fromDataset,pregelix.asterixdb.output.dataverse=toDataverse,pregelix.asterixdb.output.dataset=toDataset,pregelix.asterixdb.output.cleanup=false,pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.VLongIdInputVertexConverter,pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.VLongIdOutputVertexConverter" });
    Assert.assertEquals(cmds, expectedCmds);
    parameters.remove(parameters.size() - 1);
    parameters.add("-ip 10.0.2.15 -port 3199 -cust-prop " + "pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.TestInputVertexConverter," + "pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.TestOutputVertexConverter");
    // Test a customer command with "-cust-prop".
    cmds = (List<String>) PA.invokeMethod(aqlTranslator, "constructPregelixCommand(org.apache.asterix.lang.common.statement.RunStatement," + "String,String,String,String)", mockRunStatement, "fromDataverse", "fromDataset", "toDataverse", "toDataset");
    expectedCmds = Arrays.asList(new String[] { "bin/pregelix", "examples/pregelix-example-jar-with-dependencies.jar", "org.apache.pregelix.example.PageRankVertex", "-ip", "10.0.2.15", "-port", "3199", "-cust-prop", "pregelix.asterixdb.url=http://127.0.0.1:19002,pregelix.asterixdb.source=true,pregelix.asterixdb.sink=true,pregelix.asterixdb.input.dataverse=fromDataverse,pregelix.asterixdb.input.dataset=fromDataset,pregelix.asterixdb.output.dataverse=toDataverse,pregelix.asterixdb.output.dataset=toDataset,pregelix.asterixdb.output.cleanup=false,pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.TestInputVertexConverter,pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.TestOutputVertexConverter" });
    Assert.assertEquals(cmds, expectedCmds);
}
Also used : RunStatement(org.apache.asterix.lang.common.statement.RunStatement) MasterNode(org.apache.asterix.event.schema.cluster.MasterNode) AqlCompilationProvider(org.apache.asterix.compiler.provider.AqlCompilationProvider) RunStatement(org.apache.asterix.lang.common.statement.RunStatement) Statement(org.apache.asterix.lang.common.base.Statement) ArrayList(java.util.ArrayList) ExternalProperties(org.apache.asterix.common.config.ExternalProperties) Cluster(org.apache.asterix.event.schema.cluster.Cluster) StorageComponentProvider(org.apache.asterix.file.StorageComponentProvider) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) CcApplicationContext(org.apache.asterix.runtime.utils.CcApplicationContext) SessionOutput(org.apache.asterix.translator.SessionOutput) DefaultStatementExecutorFactory(org.apache.asterix.app.translator.DefaultStatementExecutorFactory) ClusterProperties(org.apache.asterix.common.config.ClusterProperties) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 2 with SessionOutput

use of org.apache.asterix.translator.SessionOutput in project asterixdb by apache.

the class AsterixJavaClient method compile.

public void compile(boolean optimize, boolean printRewrittenExpressions, boolean printLogicalPlan, boolean printOptimizedPlan, boolean printPhysicalOpsOnly, boolean generateBinaryRuntime, boolean printJob) throws Exception {
    queryJobSpec = null;
    dmlJobs = null;
    if (queryText == null) {
        return;
    }
    int ch;
    StringBuilder builder = new StringBuilder();
    while ((ch = queryText.read()) != -1) {
        builder.append((char) ch);
    }
    IParser parser = parserFactory.createParser(builder.toString());
    List<Statement> statements = parser.parse();
    MetadataManager.INSTANCE.init();
    SessionConfig conf = new SessionConfig(OutputFormat.ADM, optimize, true, generateBinaryRuntime);
    conf.setOOBData(false, printRewrittenExpressions, printLogicalPlan, printOptimizedPlan, printJob);
    if (printPhysicalOpsOnly) {
        conf.set(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS, true);
    }
    SessionOutput output = new SessionOutput(conf, writer);
    IStatementExecutor translator = statementExecutorFactory.create(appCtx, statements, output, compilationProvider, storageComponentProvider);
    translator.compileAndExecute(hcc, null, QueryTranslator.ResultDelivery.IMMEDIATE, null, new IStatementExecutor.Stats());
    writer.flush();
}
Also used : IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) SessionOutput(org.apache.asterix.translator.SessionOutput) Statement(org.apache.asterix.lang.common.base.Statement) SessionConfig(org.apache.asterix.translator.SessionConfig) IParser(org.apache.asterix.lang.common.base.IParser)

Example 3 with SessionOutput

use of org.apache.asterix.translator.SessionOutput in project asterixdb by apache.

the class QueryServiceServlet method createSessionOutput.

private static SessionOutput createSessionOutput(RequestParameters param, String handleUrl, PrintWriter resultWriter) {
    SessionOutput.ResultDecorator resultPrefix = ResultUtil.createPreResultDecorator();
    SessionOutput.ResultDecorator resultPostfix = ResultUtil.createPostResultDecorator();
    SessionOutput.ResultAppender appendHandle = ResultUtil.createResultHandleAppender(handleUrl);
    SessionOutput.ResultAppender appendStatus = ResultUtil.createResultStatusAppender();
    SessionConfig.OutputFormat format = getFormat(param.format);
    SessionConfig sessionConfig = new SessionConfig(format);
    sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, true);
    sessionConfig.set(SessionConfig.FORMAT_INDENT_JSON, param.pretty);
    sessionConfig.set(SessionConfig.FORMAT_QUOTE_RECORD, format != SessionConfig.OutputFormat.CLEAN_JSON && format != SessionConfig.OutputFormat.LOSSLESS_JSON);
    sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, format == SessionConfig.OutputFormat.CSV && "present".equals(getParameterValue(param.format, Attribute.HEADER.str())));
    return new SessionOutput(sessionConfig, resultWriter, resultPrefix, resultPostfix, appendHandle, appendStatus);
}
Also used : SessionOutput(org.apache.asterix.translator.SessionOutput) SessionConfig(org.apache.asterix.translator.SessionConfig)

Example 4 with SessionOutput

use of org.apache.asterix.translator.SessionOutput in project asterixdb by apache.

the class QueryServiceServlet method handleRequest.

private void handleRequest(RequestParameters param, IServletResponse response) throws IOException {
    LOGGER.info(param.toString());
    long elapsedStart = System.nanoTime();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter resultWriter = new PrintWriter(stringWriter);
    ResultDelivery delivery = parseResultDelivery(param.mode);
    String handleUrl = getHandleUrl(param.host, param.path, delivery);
    SessionOutput sessionOutput = createSessionOutput(param, handleUrl, resultWriter);
    SessionConfig sessionConfig = sessionOutput.config();
    HttpUtil.setContentType(response, HttpUtil.ContentType.APPLICATION_JSON, HttpUtil.Encoding.UTF8);
    HttpResponseStatus status = HttpResponseStatus.OK;
    Stats stats = new Stats();
    long[] execStartEnd = new long[] { -1, -1 };
    resultWriter.print("{\n");
    printRequestId(resultWriter);
    printClientContextID(resultWriter, param);
    printSignature(resultWriter);
    printType(resultWriter, sessionConfig);
    try {
        if (param.statement == null || param.statement.isEmpty()) {
            throw new AsterixException("Empty request, no statement provided");
        }
        String statementsText = param.statement + ";";
        executeStatement(statementsText, sessionOutput, delivery, stats, param, handleUrl, execStartEnd);
        if (ResultDelivery.IMMEDIATE == delivery || ResultDelivery.DEFERRED == delivery) {
            ResultUtil.printStatus(sessionOutput, ResultStatus.SUCCESS);
        }
    } catch (AlgebricksException | TokenMgrError | org.apache.asterix.aqlplus.parser.TokenMgrError pe) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.INFO, pe.getMessage(), pe);
        ResultUtil.printError(resultWriter, pe);
        ResultUtil.printStatus(sessionOutput, ResultStatus.FATAL);
        status = HttpResponseStatus.BAD_REQUEST;
    } catch (HyracksException pe) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.WARNING, pe.getMessage(), pe);
        ResultUtil.printError(resultWriter, pe);
        ResultUtil.printStatus(sessionOutput, ResultStatus.FATAL);
        status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    } catch (Exception e) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, "Unexpected exception", e);
        ResultUtil.printError(resultWriter, e);
        ResultUtil.printStatus(sessionOutput, ResultStatus.FATAL);
        status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    } finally {
        if (execStartEnd[0] == -1) {
            execStartEnd[1] = -1;
        } else if (execStartEnd[1] == -1) {
            execStartEnd[1] = System.nanoTime();
        }
    }
    printMetrics(resultWriter, System.nanoTime() - elapsedStart, execStartEnd[1] - execStartEnd[0], stats.getCount(), stats.getSize());
    resultWriter.print("}\n");
    resultWriter.flush();
    String result = stringWriter.toString();
    GlobalConfig.ASTERIX_LOGGER.log(Level.FINE, result);
    response.setStatus(status);
    response.writer().print(result);
    if (response.writer().checkError()) {
        LOGGER.warning("Error flushing output writer");
    }
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) SessionConfig(org.apache.asterix.translator.SessionConfig) TokenMgrError(org.apache.asterix.lang.aql.parser.TokenMgrError) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) ResultDelivery(org.apache.asterix.translator.IStatementExecutor.ResultDelivery) AsterixException(org.apache.asterix.common.exceptions.AsterixException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) StringWriter(java.io.StringWriter) SessionOutput(org.apache.asterix.translator.SessionOutput) Stats(org.apache.asterix.translator.IStatementExecutor.Stats) PrintWriter(java.io.PrintWriter)

Example 5 with SessionOutput

use of org.apache.asterix.translator.SessionOutput 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)

Aggregations

SessionOutput (org.apache.asterix.translator.SessionOutput)9 Statement (org.apache.asterix.lang.common.base.Statement)5 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)5 SessionConfig (org.apache.asterix.translator.SessionConfig)5 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)4 IParser (org.apache.asterix.lang.common.base.IParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)3 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)3 StringWriter (java.io.StringWriter)2 List (java.util.List)2 ResultReader (org.apache.asterix.app.result.ResultReader)2 QueryTranslator (org.apache.asterix.app.translator.QueryTranslator)2 IStorageComponentProvider (org.apache.asterix.common.context.IStorageComponentProvider)2 IParserFactory (org.apache.asterix.lang.common.base.IParserFactory)2 ResultDelivery (org.apache.asterix.translator.IStatementExecutor.ResultDelivery)2 IHyracksDataset (org.apache.hyracks.api.dataset.IHyracksDataset)2