Search in sources :

Example 6 with PersistentQueryMetadata

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

the class StatementExecutor method handleRunScript.

private void handleRunScript(Command command) throws Exception {
    if (command.getKsqlProperties().containsKey(KsqlConstants.RUN_SCRIPT_STATEMENTS_CONTENT)) {
        String queries = (String) command.getKsqlProperties().get(KsqlConstants.RUN_SCRIPT_STATEMENTS_CONTENT);
        List<QueryMetadata> queryMetadataList = ksqlEngine.buildMultipleQueries(queries, command.getKsqlProperties());
        for (QueryMetadata queryMetadata : queryMetadataList) {
            if (queryMetadata instanceof PersistentQueryMetadata) {
                PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
                persistentQueryMetadata.getKafkaStreams().start();
            }
        }
    } else {
        throw new KsqlException("No statements received for LOAD FROM FILE.");
    }
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 7 with PersistentQueryMetadata

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

the class StatementExecutor method startQuery.

private boolean startQuery(String queryString, Query query, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, Command command, boolean wasDropped) throws Exception {
    if (query.getQueryBody() instanceof QuerySpecification) {
        QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
        Relation into = querySpecification.getInto();
        if (into instanceof Table) {
            Table table = (Table) into;
            if (ksqlEngine.getMetaStore().getSource(table.getName().getSuffix()) != null) {
                throw new Exception(String.format("Sink specified in INTO clause already exists: %s", table.getName().getSuffix().toUpperCase()));
            }
        }
    }
    QueryMetadata queryMetadata = ksqlEngine.buildMultipleQueries(queryString, command.getKsqlProperties()).get(0);
    if (queryMetadata instanceof PersistentQueryMetadata) {
        PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
        final QueryId queryId = persistentQueryMetadata.getId();
        if (terminatedQueries != null && terminatedQueries.containsKey(queryId)) {
            CommandId terminateId = terminatedQueries.get(queryId);
            statusStore.put(terminateId, new CommandStatus(CommandStatus.Status.SUCCESS, "Termination request granted"));
            statusStore.put(commandId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else if (wasDropped) {
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else {
            persistentQueryMetadata.getKafkaStreams().start();
            return true;
        }
    } else {
        throw new Exception(String.format("Unexpected query metadata type: %s; was expecting %s", queryMetadata.getClass().getCanonicalName(), PersistentQueryMetadata.class.getCanonicalName()));
    }
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) Table(io.confluent.ksql.parser.tree.Table) 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) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 8 with PersistentQueryMetadata

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

the class KsqlResource method describe.

private SourceDescription describe(String name, boolean extended) throws KsqlException {
    StructuredDataSource dataSource = ksqlEngine.getMetaStore().getSource(name);
    if (dataSource == null) {
        throw new KsqlException(String.format("Could not find STREAM/TABLE '%s' in the Metastore", name));
    }
    List<PersistentQueryMetadata> queries = ksqlEngine.getPersistentQueries().values().stream().filter(meta -> ((KsqlStructuredDataOutputNode) meta.getOutputNode()).getKafkaTopicName().equals(dataSource.getKsqlTopic().getTopicName())).collect(Collectors.toList());
    return new SourceDescription(dataSource, extended, dataSource.getKsqlTopic().getKsqlTopicSerDe().getSerDe().name(), "", "", getReadQueryIds(queries), getWriteQueryIds(queries), ksqlEngine.getTopicClient());
}
Also used : CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) Query(io.confluent.ksql.parser.tree.Query) CreateTableAsSelect(io.confluent.ksql.parser.tree.CreateTableAsSelect) Interval(org.antlr.v4.runtime.misc.Interval) StreamsList(io.confluent.ksql.rest.entity.StreamsList) Produces(javax.ws.rs.Produces) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) TimeoutException(java.util.concurrent.TimeoutException) KsqlEngine(io.confluent.ksql.KsqlEngine) KsqlParser(io.confluent.ksql.parser.KsqlParser) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) MediaType(javax.ws.rs.core.MediaType) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) CommandId(io.confluent.ksql.rest.server.computation.CommandId) Pair(io.confluent.ksql.util.Pair) Consumes(javax.ws.rs.Consumes) CreateTable(io.confluent.ksql.parser.tree.CreateTable) Explain(io.confluent.ksql.parser.tree.Explain) Map(java.util.Map) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) KsqlStream(io.confluent.ksql.metastore.KsqlStream) QueryId(io.confluent.ksql.query.QueryId) DropTopic(io.confluent.ksql.parser.tree.DropTopic) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) DdlCommandResult(io.confluent.ksql.ddl.commands.DdlCommandResult) QueryMetadata(io.confluent.ksql.util.QueryMetadata) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) PropertiesList(io.confluent.ksql.rest.entity.PropertiesList) DropStream(io.confluent.ksql.parser.tree.DropStream) Collection(java.util.Collection) KsqlTable(io.confluent.ksql.metastore.KsqlTable) KafkaConsumerGroupClient(io.confluent.ksql.util.KafkaConsumerGroupClient) Collectors(java.util.stream.Collectors) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) List(java.util.List) Response(javax.ws.rs.core.Response) SetProperty(io.confluent.ksql.parser.tree.SetProperty) TopicDescription(io.confluent.ksql.rest.entity.TopicDescription) KafkaConsumerGroupClientImpl(io.confluent.ksql.util.KafkaConsumerGroupClientImpl) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) DdlCommandExec(io.confluent.ksql.ddl.commands.DdlCommandExec) DdlCommand(io.confluent.ksql.ddl.commands.DdlCommand) KsqlException(io.confluent.ksql.util.KsqlException) Statement(io.confluent.ksql.parser.tree.Statement) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) ListTopics(io.confluent.ksql.parser.tree.ListTopics) KsqlRestApplication(io.confluent.ksql.rest.server.KsqlRestApplication) HashMap(java.util.HashMap) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) ListProperties(io.confluent.ksql.parser.tree.ListProperties) ArrayList(java.util.ArrayList) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CharStream(org.antlr.v4.runtime.CharStream) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode) Queries(io.confluent.ksql.rest.entity.Queries) TablesList(io.confluent.ksql.rest.entity.TablesList) StatementExecutor(io.confluent.ksql.rest.server.computation.StatementExecutor) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) ListTables(io.confluent.ksql.parser.tree.ListTables) DropTable(io.confluent.ksql.parser.tree.DropTable) POST(javax.ws.rs.POST) ListStreams(io.confluent.ksql.parser.tree.ListStreams) RunScript(io.confluent.ksql.parser.tree.RunScript) ListQueries(io.confluent.ksql.parser.tree.ListQueries) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) ListRegisteredTopics(io.confluent.ksql.parser.tree.ListRegisteredTopics) DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) CommandStore(io.confluent.ksql.rest.server.computation.CommandStore) DataSource(io.confluent.ksql.serde.DataSource) TimeUnit(java.util.concurrent.TimeUnit) KsqlTopicsList(io.confluent.ksql.rest.entity.KsqlTopicsList) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) AvroUtil(io.confluent.ksql.util.AvroUtil) Collections(java.util.Collections) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 9 with PersistentQueryMetadata

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

the class KsqlContext method sql.

/**
 * Execute the ksql statement in this context.
 */
public void sql(String sql) throws Exception {
    List<QueryMetadata> queryMetadataList = ksqlEngine.buildMultipleQueries(sql, Collections.emptyMap());
    for (QueryMetadata queryMetadata : queryMetadataList) {
        if (queryMetadata instanceof PersistentQueryMetadata) {
            PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
            persistentQueryMetadata.getKafkaStreams().start();
            ksqlEngine.getPersistentQueries().put(persistentQueryMetadata.getId(), persistentQueryMetadata);
        } else {
            System.err.println("Ignoring statemenst: " + sql);
            System.err.println("Only CREATE statements can run in KSQL embedded mode.");
            log.warn("Ignoring statemenst: {}", sql);
            log.warn("Only CREATE statements can run in KSQL embedded mode.");
        }
    }
}
Also used : QueryMetadata(io.confluent.ksql.util.QueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 10 with PersistentQueryMetadata

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

the class KsqlEngine method terminateQueryForEntity.

@Override
public void terminateQueryForEntity(final String entity) {
    final Optional<PersistentQueryMetadata> query = persistentQueries.values().stream().filter(persistentQueryMetadata -> persistentQueryMetadata.getEntity().equalsIgnoreCase(entity)).findFirst();
    if (query.isPresent()) {
        final PersistentQueryMetadata metadata = query.get();
        log.info("Terminating persistent query {}", metadata.getId());
        metadata.close();
        persistentQueries.remove(metadata.getId());
        livePersistentQueries.remove(metadata);
        allLiveQueries.remove(metadata);
    }
}
Also used : CreateTableCommand(io.confluent.ksql.ddl.commands.CreateTableCommand) Query(io.confluent.ksql.parser.tree.Query) Interval(org.antlr.v4.runtime.misc.Interval) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) LoggerFactory(org.slf4j.LoggerFactory) CommandFactories(io.confluent.ksql.ddl.commands.CommandFactories) KsqlParser(io.confluent.ksql.parser.KsqlParser) KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) Pair(io.confluent.ksql.util.Pair) CreateTable(io.confluent.ksql.parser.tree.CreateTable) Map(java.util.Map) RegisterTopic(io.confluent.ksql.parser.tree.RegisterTopic) QueryId(io.confluent.ksql.query.QueryId) DropTopic(io.confluent.ksql.parser.tree.DropTopic) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) DdlCommandResult(io.confluent.ksql.ddl.commands.DdlCommandResult) QueryMetadata(io.confluent.ksql.util.QueryMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) DropStream(io.confluent.ksql.parser.tree.DropStream) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Expression(io.confluent.ksql.parser.tree.Expression) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) List(java.util.List) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) SetProperty(io.confluent.ksql.parser.tree.SetProperty) DdlCommandExec(io.confluent.ksql.ddl.commands.DdlCommandExec) Optional(java.util.Optional) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) KsqlException(io.confluent.ksql.util.KsqlException) Statement(io.confluent.ksql.parser.tree.Statement) StreamsConfig(org.apache.kafka.streams.StreamsConfig) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) DdlConfig(io.confluent.ksql.ddl.DdlConfig) HashMap(java.util.HashMap) QualifiedName(io.confluent.ksql.parser.tree.QualifiedName) RegisterTopicCommand(io.confluent.ksql.ddl.commands.RegisterTopicCommand) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CreateStream(io.confluent.ksql.parser.tree.CreateStream) CharStream(org.antlr.v4.runtime.CharStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MetaStore(io.confluent.ksql.metastore.MetaStore) DropSourceCommand(io.confluent.ksql.ddl.commands.DropSourceCommand) DropTable(io.confluent.ksql.parser.tree.DropTable) Logger(org.slf4j.Logger) KsqlSchemaRegistryClientFactory(io.confluent.ksql.schema.registry.KsqlSchemaRegistryClientFactory) DropTopicCommand(io.confluent.ksql.ddl.commands.DropTopicCommand) CreateStreamCommand(io.confluent.ksql.ddl.commands.CreateStreamCommand) Table(io.confluent.ksql.parser.tree.Table) DataSource(io.confluent.ksql.serde.DataSource) TimeUnit(java.util.concurrent.TimeUnit) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) Closeable(java.io.Closeable) PlanNode(io.confluent.ksql.planner.plan.PlanNode) QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Collections(java.util.Collections) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Aggregations

PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)17 QueryMetadata (io.confluent.ksql.util.QueryMetadata)11 QueryId (io.confluent.ksql.query.QueryId)6 KsqlException (io.confluent.ksql.util.KsqlException)6 CreateStreamCommand (io.confluent.ksql.ddl.commands.CreateStreamCommand)3 CreateTableCommand (io.confluent.ksql.ddl.commands.CreateTableCommand)3 DdlCommandExec (io.confluent.ksql.ddl.commands.DdlCommandExec)3 DropSourceCommand (io.confluent.ksql.ddl.commands.DropSourceCommand)3 DropTopicCommand (io.confluent.ksql.ddl.commands.DropTopicCommand)3 RegisterTopicCommand (io.confluent.ksql.ddl.commands.RegisterTopicCommand)3 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)3 KafkaTopicClient (io.confluent.ksql.util.KafkaTopicClient)3 Pair (io.confluent.ksql.util.Pair)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DdlCommandResult (io.confluent.ksql.ddl.commands.DdlCommandResult)2 KsqlParser (io.confluent.ksql.parser.KsqlParser)2 SqlBaseParser (io.confluent.ksql.parser.SqlBaseParser)2 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2