Search in sources :

Example 21 with QueryMetadata

use of io.confluent.ksql.util.QueryMetadata 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 22 with QueryMetadata

use of io.confluent.ksql.util.QueryMetadata in project ksql by confluentinc.

the class KsqlResource method registerDdlCommandTasks.

private void registerDdlCommandTasks() {
    ddlCommandTasks.put(Query.class, (statement, statementText, properties) -> ksqlEngine.getQueryExecutionPlan((Query) statement).getExecutionPlan());
    ddlCommandTasks.put(CreateStreamAsSelect.class, (statement, statementText, properties) -> {
        QueryMetadata queryMetadata = ksqlEngine.getQueryExecutionPlan(((CreateStreamAsSelect) statement).getQuery());
        if (queryMetadata.getDataSourceType() == DataSource.DataSourceType.KTABLE) {
            throw new KsqlException("Invalid result type. Your SELECT query produces a TABLE. Please " + "use CREATE TABLE AS SELECT statement instead.");
        }
        if (queryMetadata instanceof PersistentQueryMetadata) {
            new AvroUtil().validatePersistentQueryResults((PersistentQueryMetadata) queryMetadata, ksqlEngine.getSchemaRegistryClient());
        }
        queryMetadata.close();
        return queryMetadata.getExecutionPlan();
    });
    ddlCommandTasks.put(CreateTableAsSelect.class, (statement, statementText, properties) -> {
        QueryMetadata queryMetadata = ksqlEngine.getQueryExecutionPlan(((CreateTableAsSelect) statement).getQuery());
        if (queryMetadata.getDataSourceType() != DataSource.DataSourceType.KTABLE) {
            throw new KsqlException("Invalid result type. Your SELECT query produces a STREAM. Please " + "use CREATE STREAM AS SELECT statement instead.");
        }
        if (queryMetadata instanceof PersistentQueryMetadata) {
            new AvroUtil().validatePersistentQueryResults((PersistentQueryMetadata) queryMetadata, ksqlEngine.getSchemaRegistryClient());
        }
        queryMetadata.close();
        return queryMetadata.getExecutionPlan();
    });
    ddlCommandTasks.put(RegisterTopic.class, (statement, statementText, properties) -> {
        RegisterTopicCommand registerTopicCommand = new RegisterTopicCommand((RegisterTopic) statement);
        new DdlCommandExec(ksqlEngine.getMetaStore().clone()).execute(registerTopicCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(CreateStream.class, (statement, statementText, properties) -> {
        CreateStreamCommand createStreamCommand = new CreateStreamCommand(statementText, (CreateStream) statement, properties, ksqlEngine.getTopicClient(), true);
        executeDdlCommand(createStreamCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(CreateTable.class, (statement, statementText, properties) -> {
        CreateTableCommand createTableCommand = new CreateTableCommand(statementText, (CreateTable) statement, properties, ksqlEngine.getTopicClient(), true);
        executeDdlCommand(createTableCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(DropTopic.class, (statement, statementText, properties) -> {
        DropTopicCommand dropTopicCommand = new DropTopicCommand((DropTopic) statement);
        new DdlCommandExec(ksqlEngine.getMetaStore().clone()).execute(dropTopicCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(DropStream.class, (statement, statementText, properties) -> {
        DropSourceCommand dropSourceCommand = new DropSourceCommand((DropStream) statement, DataSource.DataSourceType.KSTREAM, ksqlEngine);
        executeDdlCommand(dropSourceCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(DropTable.class, (statement, statementText, properties) -> {
        DropSourceCommand dropSourceCommand = new DropSourceCommand((DropTable) statement, DataSource.DataSourceType.KTABLE, ksqlEngine);
        executeDdlCommand(dropSourceCommand);
        return statement.toString();
    });
    ddlCommandTasks.put(TerminateQuery.class, (statement, statementText, properties) -> statement.toString());
}
Also used : DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) DdlCommandExec(io.confluent.ksql.ddl.commands.DdlCommandExec) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) AvroUtil(io.confluent.ksql.util.AvroUtil) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand)

Example 23 with QueryMetadata

use of io.confluent.ksql.util.QueryMetadata in project ksql by confluentinc.

the class StandaloneExecutor method executeStatements.

private void executeStatements(final String queries) throws Exception {
    final List<QueryMetadata> queryMetadataList = ksqlEngine.createQueries(queries);
    for (QueryMetadata queryMetadata : queryMetadataList) {
        if (queryMetadata instanceof PersistentQueryMetadata) {
            PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
            persistentQueryMetadata.start();
        } else {
            final String message = String.format("Ignoring statements: %s" + "%nOnly CREATE statements can run in standalone mode.", queryMetadata.getStatementString());
            System.err.println(message);
            log.warn(message);
        }
    }
}
Also used : QueryMetadata(io.confluent.ksql.util.QueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Aggregations

QueryMetadata (io.confluent.ksql.util.QueryMetadata)23 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)13 Test (org.junit.Test)10 KsqlException (io.confluent.ksql.util.KsqlException)5 IntegrationTest (io.confluent.common.utils.IntegrationTest)4 GenericRow (io.confluent.ksql.GenericRow)4 KafkaTopicClient (io.confluent.ksql.util.KafkaTopicClient)4 KafkaTopicClientImpl (io.confluent.ksql.util.KafkaTopicClientImpl)4 HashMap (java.util.HashMap)4 AdminClient (org.apache.kafka.clients.admin.AdminClient)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 Schema (org.apache.kafka.connect.data.Schema)4 Windowed (org.apache.kafka.streams.kstream.Windowed)3 QueryId (io.confluent.ksql.query.QueryId)2 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)2 WakeupException (org.apache.kafka.common.errors.WakeupException)2 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)1 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)1 DdlCommandExec (io.confluent.ksql.ddl.commands.DdlCommandExec)1 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)1