Search in sources :

Example 6 with QueryId

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);
}
Also used : QueryId(io.confluent.ksql.query.QueryId) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 7 with QueryId

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);
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) Queries(io.confluent.ksql.rest.entity.Queries) HashMap(java.util.HashMap) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) ArrayList(java.util.ArrayList) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) ExecutionPlan(io.confluent.ksql.rest.entity.ExecutionPlan) StreamsList(io.confluent.ksql.rest.entity.StreamsList) QueryId(io.confluent.ksql.query.QueryId) KsqlTopicInfo(io.confluent.ksql.rest.entity.KsqlTopicInfo) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) PropertiesList(io.confluent.ksql.rest.entity.PropertiesList) TablesList(io.confluent.ksql.rest.entity.TablesList) FakeException(io.confluent.ksql.FakeException) KafkaTopicInfo(io.confluent.ksql.rest.entity.KafkaTopicInfo) TopicDescription(io.confluent.ksql.rest.entity.TopicDescription) KsqlTopicsList(io.confluent.ksql.rest.entity.KsqlTopicsList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Test(org.junit.Test)

Example 8 with QueryId

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);
}
Also used : SchemaKTable(io.confluent.ksql.structured.SchemaKTable) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) KafkaStreams(org.apache.kafka.streams.KafkaStreams) KsqlTable(io.confluent.ksql.metastore.KsqlTable) QueryId(io.confluent.ksql.query.QueryId) Topology(org.apache.kafka.streams.Topology) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 9 with QueryId

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"));
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.serde.DataSource)

Example 10 with QueryId

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)));
}
Also used : HashMap(java.util.HashMap) QueryId(io.confluent.ksql.query.QueryId) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

QueryId (io.confluent.ksql.query.QueryId)10 Test (org.junit.Test)6 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)5 KsqlException (io.confluent.ksql.util.KsqlException)3 HashMap (java.util.HashMap)3 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 SourceDescription (io.confluent.ksql.rest.entity.SourceDescription)2 QueryMetadata (io.confluent.ksql.util.QueryMetadata)2 ArrayList (java.util.ArrayList)2 WakeupException (org.apache.kafka.common.errors.WakeupException)2 FakeException (io.confluent.ksql.FakeException)1 KsqlStream (io.confluent.ksql.metastore.KsqlStream)1 KsqlTable (io.confluent.ksql.metastore.KsqlTable)1 MetaStoreImpl (io.confluent.ksql.metastore.MetaStoreImpl)1 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)1 Relation (io.confluent.ksql.parser.tree.Relation)1 Table (io.confluent.ksql.parser.tree.Table)1 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)1 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)1