use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.
the class KsqlResource method getStatementExecutionPlan.
private SourceDescription getStatementExecutionPlan(String queryId, Statement statement, String statementText, Map<String, Object> properties) throws KsqlException {
if (queryId != null) {
PersistentQueryMetadata metadata = ksqlEngine.getPersistentQueries().get(new QueryId(queryId));
if (metadata == null) {
throw new KsqlException(("Query with id:" + queryId + " does not exist, use SHOW QUERIES to view the full set of " + "queries."));
}
KsqlStructuredDataOutputNode outputNode = (KsqlStructuredDataOutputNode) metadata.getOutputNode();
return new SourceDescription(outputNode, metadata.getStatementString(), metadata.getStatementString(), metadata.getTopologyDescription(), metadata.getExecutionPlan(), ksqlEngine.getTopicClient());
}
DdlCommandTask ddlCommandTask = ddlCommandTasks.get(statement.getClass());
if (ddlCommandTask != null) {
try {
String executionPlan = ddlCommandTask.execute(statement, statementText, properties);
return new SourceDescription("", "User-Evaluation", Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, "QUERY", "", "", "", "", true, "", "", "", executionPlan, 0, 0);
} catch (KsqlException ksqlException) {
throw ksqlException;
} catch (Throwable t) {
throw new KsqlException("Cannot RUN execution plan for this statement, " + statement, t);
}
}
throw new KsqlException("Cannot FIND execution plan for this statement:" + statement);
}
use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.
the class ConsoleTest method testPrintKSqlEntityList.
@Test
public void testPrintKSqlEntityList() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put("k1", 1);
properties.put("k2", "v2");
properties.put("k3", true);
List<Queries.RunningQuery> queries = new ArrayList<>();
queries.add(new Queries.RunningQuery("select * from t1", "TestTopic", new QueryId("0")));
for (int i = 0; i < 5; i++) {
KsqlEntityList entityList = new KsqlEntityList(Arrays.asList(new CommandStatusEntity("e", "topic/1/create", "SUCCESS", "Success Message"), new ErrorMessageEntity("e", new FakeException()), new PropertiesList("e", properties), new Queries("e", queries), new SourceDescription("e", "TestSource", Collections.EMPTY_LIST, Collections.EMPTY_LIST, buildTestSchema(i), DataSource.DataSourceType.KTABLE.getKqlType(), "key", "2000-01-01", "stats", "errors", false, "avro", "kadka-topic", "topology", "executionPlan", 1, 1), new TopicDescription("e", "TestTopic", "TestKafkaTopic", "AVRO", "schemaString"), new StreamsList("e", Arrays.asList(new SourceInfo.Stream("TestStream", "TestTopic", "AVRO"))), new TablesList("e", Arrays.asList(new SourceInfo.Table("TestTable", "TestTopic", "JSON", false))), new KsqlTopicsList("e", Arrays.asList(new KsqlTopicInfo("TestTopic", "TestKafkaTopic", DataSource.DataSourceSerDe.JSON))), new KafkaTopicsList("e", Arrays.asList(new KafkaTopicInfo("TestKafkaTopic", true, ImmutableList.of(1), 1, 1))), new ExecutionPlan("Test Execution Plan")));
terminal.printKsqlEntityList(entityList);
}
}
use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.
the class PhysicalPlanBuilder method buildPlanForStructuredOutputNode.
private QueryMetadata buildPlanForStructuredOutputNode(String sqlExpression, final SchemaKStream schemaKStream, final KsqlStructuredDataOutputNode outputNode, final String serviceId, final String persistanceQueryPrefix, final String statement) {
if (metaStore.getTopic(outputNode.getKafkaTopicName()) == null) {
metaStore.putTopic(outputNode.getKsqlTopic());
}
StructuredDataSource sinkDataSource;
if (schemaKStream instanceof SchemaKTable) {
SchemaKTable schemaKTable = (SchemaKTable) schemaKStream;
sinkDataSource = new KsqlTable(sqlExpression, outputNode.getId().toString(), outputNode.getSchema(), schemaKStream.getKeyField(), outputNode.getTimestampField(), outputNode.getKsqlTopic(), outputNode.getId().toString() + ksqlConfig.get(KsqlConfig.KSQL_TABLE_STATESTORE_NAME_SUFFIX_CONFIG), schemaKTable.isWindowed());
} else {
sinkDataSource = new KsqlStream(sqlExpression, outputNode.getId().toString(), outputNode.getSchema(), schemaKStream.getKeyField(), outputNode.getTimestampField(), outputNode.getKsqlTopic());
}
if (updateMetastore) {
metaStore.putSource(sinkDataSource.cloneWithTimeKeyColumns());
}
final QueryId queryId = sinkDataSource.getPersistentQueryId();
final String applicationId = serviceId + persistanceQueryPrefix + queryId;
KafkaStreams streams = buildStreams(builder, applicationId, ksqlConfig, overriddenStreamsProperties);
Topology topology = builder.build();
return new PersistentQueryMetadata(statement, streams, outputNode, schemaKStream.getExecutionPlan(""), queryId, (schemaKStream instanceof SchemaKTable) ? DataSource.DataSourceType.KTABLE : DataSource.DataSourceType.KSTREAM, applicationId, kafkaTopicClient, outputNode.getSchema(), sinkDataSource.getKsqlTopic(), topology);
}
use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.
the class StatementExecutor method terminateQuery.
private void terminateQuery(TerminateQuery terminateQuery) throws Exception {
final QueryId queryId = terminateQuery.getQueryId();
final QueryMetadata queryMetadata = ksqlEngine.getPersistentQueries().get(queryId);
if (!ksqlEngine.terminateQuery(queryId, true)) {
throw new Exception(String.format("No running query with id %s was found", queryId));
}
CommandId.Type commandType;
DataSource.DataSourceType sourceType = queryMetadata.getOutputNode().getTheSourceNode().getDataSourceType();
switch(sourceType) {
case KTABLE:
commandType = CommandId.Type.TABLE;
break;
case KSTREAM:
commandType = CommandId.Type.STREAM;
break;
default:
throw new Exception(String.format("Unexpected source type for running query: %s", sourceType));
}
String queryEntity = ((KsqlStructuredDataOutputNode) queryMetadata.getOutputNode()).getKsqlTopic().getName();
CommandId queryStatementId = new CommandId(commandType, queryEntity, CommandId.Action.CREATE);
statusStore.put(queryStatementId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
}
use of io.confluent.ksql.query.QueryId in project ksql by confluentinc.
the class RestoreCommandsTest method shouldHaveTerminatedQueriesWhenMultipleCreateDropTerminateForCommand.
@Test
public void shouldHaveTerminatedQueriesWhenMultipleCreateDropTerminateForCommand() {
// create
restoreCommands.addCommand(createId, createCommand);
// terminate
restoreCommands.addCommand(terminateId, terminateCommand);
// drop
restoreCommands.addCommand(dropId, dropCommand);
// recreate
restoreCommands.addCommand(createId, createCommand);
// another one for good measure
restoreCommands.addCommand(new CommandId(CommandId.Type.STREAM, "bar", CommandId.Action.CREATE), createCommand);
// terminate again
restoreCommands.addCommand(terminateId, terminateCommand);
final Map<CommandId, Map<QueryId, CommandId>> commandIdToTerminate = new HashMap<>();
restoreCommands.forEach((commandId, command, terminatedQueries, dropped) -> commandIdToTerminate.put(commandId, terminatedQueries));
assertThat(commandIdToTerminate.get(createId), equalTo(Collections.singletonMap(new QueryId("queryId"), terminateId)));
}
Aggregations