Search in sources :

Example 1 with ExecutorException

use of org.apache.samza.sql.client.exceptions.ExecutorException in project samza by apache.

the class SamzaExecutor method executeNonQuery.

@Override
public NonQueryResult executeNonQuery(ExecutionContext context, File sqlFile) throws ExecutorException {
    LOG.info("Sql file path: " + sqlFile.getPath());
    List<String> executedStmts;
    try {
        executedStmts = Files.lines(Paths.get(sqlFile.getPath())).collect(Collectors.toList());
    } catch (IOException e) {
        throw new ExecutorException(e);
    }
    LOG.info("Sql statements in Sql file: " + executedStmts.toString());
    List<String> submittedStmts = new ArrayList<>();
    List<String> nonSubmittedStmts = new ArrayList<>();
    validateExecutedStmts(executedStmts, submittedStmts, nonSubmittedStmts);
    if (submittedStmts.isEmpty()) {
        throw new ExecutorException("Nothing to execute. Note: SELECT statements are ignored.");
    }
    NonQueryResult result = executeNonQuery(context, submittedStmts);
    return new NonQueryResult(result.getExecutionId(), submittedStmts, nonSubmittedStmts);
}
Also used : ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) IOException(java.io.IOException)

Example 2 with ExecutorException

use of org.apache.samza.sql.client.exceptions.ExecutorException in project samza by apache.

the class SamzaExecutor method getTableSchema.

@Override
public SqlSchema getTableSchema(ExecutionContext context, String tableName) throws ExecutorException {
    /*
     *  currently Shell works only for systems that has Avro schemas
     */
    int execId = execIdSeq.incrementAndGet();
    Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId);
    Config samzaSqlConfig = new MapConfig(staticConfigs);
    SqlSchema sqlSchema;
    try {
        SqlIOResolver ioResolver = SamzaSqlApplicationConfig.createIOResolver(samzaSqlConfig);
        SqlIOConfig sourceInfo = ioResolver.fetchSourceInfo(tableName);
        RelSchemaProvider schemaProvider = SamzaSqlApplicationConfig.initializePlugin("RelSchemaProvider", sourceInfo.getRelSchemaProviderName(), samzaSqlConfig, SamzaSqlApplicationConfig.CFG_FMT_REL_SCHEMA_PROVIDER_DOMAIN, (o, c) -> ((RelSchemaProviderFactory) o).create(sourceInfo.getSystemStream(), c));
        sqlSchema = schemaProvider.getSqlSchema();
    } catch (SamzaException ex) {
        throw new ExecutorException(ex);
    }
    return sqlSchema;
}
Also used : SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) SqlSchema(org.apache.samza.sql.schema.SqlSchema) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) SqlIOResolver(org.apache.samza.sql.interfaces.SqlIOResolver) RelSchemaProvider(org.apache.samza.sql.interfaces.RelSchemaProvider) SamzaException(org.apache.samza.SamzaException)

Example 3 with ExecutorException

use of org.apache.samza.sql.client.exceptions.ExecutorException in project samza by apache.

the class SamzaExecutor method executeNonQuery.

@Override
public NonQueryResult executeNonQuery(ExecutionContext context, List<String> statement) throws ExecutorException {
    int execId = execIdSeq.incrementAndGet();
    Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(formatSqlStmts(statement)));
    SamzaSqlApplicationRunner runner;
    try {
        runner = new SamzaSqlApplicationRunner(true, new MapConfig(staticConfigs));
        runner.run(null);
    } catch (SamzaException ex) {
        throw new ExecutorException(ex);
    }
    executions.put(execId, runner);
    LOG.debug("Executing sql. Id ", execId);
    return new NonQueryResult(execId);
}
Also used : ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) SamzaSqlApplicationRunner(org.apache.samza.sql.runner.SamzaSqlApplicationRunner) SamzaException(org.apache.samza.SamzaException)

Example 4 with ExecutorException

use of org.apache.samza.sql.client.exceptions.ExecutorException in project samza by apache.

the class CliCommandHandler method commandLs.

private void commandLs(CliCommand command) {
    List<Integer> execIds = new ArrayList<>();
    String parameters = command.getParameters();
    if (CliUtil.isNullOrEmpty(parameters)) {
        execIds.addAll(executions.keySet());
    } else {
        execIds.addAll(splitExecutionIds(parameters));
    }
    if (execIds.size() == 0) {
        return;
    }
    execIds.sort(Integer::compareTo);
    final int terminalWidth = terminal.getWidth();
    final int idWidth = 3;
    final int statusWidth = 20;
    final int cmdWidth = terminalWidth - idWidth - statusWidth - 4;
    AttributedStyle oddLineStyle = AttributedStyle.DEFAULT.BOLD.foreground(AttributedStyle.BLUE);
    AttributedStyle evenLineStyle = AttributedStyle.DEFAULT.BOLD.foreground(AttributedStyle.CYAN);
    for (int i = 0; i < execIds.size(); ++i) {
        Integer id = execIds.get(i);
        String cmd = executions.get(id);
        if (cmd == null)
            continue;
        String status = "UNKNOWN";
        try {
            ExecutionStatus execStatus = executor.queryExecutionStatus(id);
            status = execStatus.name();
        } catch (ExecutorException e) {
            LOG.error("Error in commandLs: ", e);
        }
        int cmdStartIdx = 0;
        int cmdLength = cmd.length();
        StringBuilder line;
        while (cmdStartIdx < cmdLength) {
            line = new StringBuilder(terminalWidth);
            if (cmdStartIdx == 0) {
                line.append(CliConstants.SPACE);
                line.append(id);
                CliUtil.appendTo(line, 1 + idWidth + 1, CliConstants.SPACE);
                line.append(status);
            }
            CliUtil.appendTo(line, 1 + idWidth + 1 + statusWidth + 1, CliConstants.SPACE);
            int numToWrite = Math.min(cmdWidth, cmdLength - cmdStartIdx);
            if (numToWrite > 0) {
                line.append(cmd, cmdStartIdx, cmdStartIdx + numToWrite);
                cmdStartIdx += numToWrite;
            }
            if (i % 2 == 0) {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
                attrBuilder.append(line.toString());
                writer.println(attrBuilder.toAnsi());
            } else {
                AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
                attrBuilder.append(line.toString());
                writer.println(attrBuilder.toAnsi());
            }
        }
    }
    writer.flush();
}
Also used : AttributedStyle(org.jline.utils.AttributedStyle) ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) ExecutionStatus(org.apache.samza.sql.client.interfaces.ExecutionStatus) ArrayList(java.util.ArrayList) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 5 with ExecutorException

use of org.apache.samza.sql.client.exceptions.ExecutorException in project samza by apache.

the class CliCommandHandler method commandRm.

private void commandRm(CliCommand command) {
    String parameters = command.getParameters();
    List<Integer> execIds = new ArrayList<>();
    execIds.addAll(splitExecutionIds(parameters));
    for (Integer id : execIds) {
        try {
            ExecutionStatus status = executor.queryExecutionStatus(id);
            if (status == ExecutionStatus.Running) {
                writer.println(String.format("Execution %d is still running. Stop it first.", id));
                continue;
            }
            executor.removeExecution(exeContext, id);
            executions.remove(id);
        } catch (ExecutorException e) {
            writer.println("Error: " + e);
            LOG.error("Error in commandRm: ", e);
        }
    }
    writer.flush();
}
Also used : ExecutorException(org.apache.samza.sql.client.exceptions.ExecutorException) ExecutionStatus(org.apache.samza.sql.client.interfaces.ExecutionStatus) ArrayList(java.util.ArrayList)

Aggregations

ExecutorException (org.apache.samza.sql.client.exceptions.ExecutorException)9 SamzaException (org.apache.samza.SamzaException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CommandHandlerException (org.apache.samza.sql.client.exceptions.CommandHandlerException)2 ExecutionStatus (org.apache.samza.sql.client.interfaces.ExecutionStatus)2 SamzaSqlApplicationRunner (org.apache.samza.sql.runner.SamzaSqlApplicationRunner)2 SqlSchema (org.apache.samza.sql.schema.SqlSchema)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CliException (org.apache.samza.sql.client.exceptions.CliException)1 RelSchemaProvider (org.apache.samza.sql.interfaces.RelSchemaProvider)1 SqlIOConfig (org.apache.samza.sql.interfaces.SqlIOConfig)1 SqlIOResolver (org.apache.samza.sql.interfaces.SqlIOResolver)1 SamzaSqlApplicationConfig (org.apache.samza.sql.runner.SamzaSqlApplicationConfig)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1 AttributedStyle (org.jline.utils.AttributedStyle)1