@Override public boolean stopExecution(ExecutionContext context, int exeId) { lastErrorMsg = ""; SamzaSqlApplicationRunner runner = executions.get(exeId); if (runner != null) { LOG.debug("Stopping execution ", exeId); try { runner.kill(); } catch (SamzaException ex) { String msg = "Stopping execution failed with exception "; lastErrorMsg = msg + ex.toString(); LOG.warn(msg, ex); return false; } try { Thread.sleep(500); // wait for a second } catch (InterruptedException e) { e.printStackTrace(); } return true; } else { lastErrorMsg = "Trying to stop a non-existing SQL execution " + exeId; LOG.warn(lastErrorMsg); return false; } }
@Override public NonQueryResult executeNonQuery(ExecutionContext context, List<String> statement) { lastErrorMsg = ""; int execId = execIdSeq.incrementAndGet(); Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId, context); 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) { String msg = "Execution of the query failed with exception "; lastErrorMsg = msg + ex.toString(); LOG.error(msg, ex); return new NonQueryResult(execId, false); } executions.put(execId, runner); LOG.debug("Executing sql. Id ", execId); return new NonQueryResult(execId, true); }
@Override public QueryResult executeQuery(ExecutionContext context, String statement) { lastErrorMsg = ""; outputData.clear(); int execId = execIdSeq.incrementAndGet(); Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId, context); List<String> sqlStmts = formatSqlStmts(Collections.singletonList(statement)); staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts)); SamzaSqlApplicationRunner runner; try { runner = new SamzaSqlApplicationRunner(true, new MapConfig(staticConfigs)); runner.run(null); } catch (SamzaException ex) { String msg = "Execution failed with exception "; lastErrorMsg = msg + ex.toString(); LOG.error(msg, ex); return new QueryResult(execId, null, false); } executions.put(execId, runner); LOG.debug("Executing sql. Id ", execId); return new QueryResult(execId, generateResultSchema(new MapConfig(staticConfigs)), true); }
@Override public SqlSchema getTableSchema(ExecutionContext context, String tableName) { /** * currently Shell works only for systems that has Avro schemas */ lastErrorMsg = ""; int execId = execIdSeq.incrementAndGet(); Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId, context); Config samzaSqlConfig = new MapConfig(staticConfigs); SqlSchema sqlSchema = null; 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) { String msg = "getTableSchema failed with exception "; lastErrorMsg = msg + ex.toString(); LOG.error(msg, ex); } return sqlSchema; }