Search in sources :

Example 11 with QueryMetadata

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

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

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

the class KsqlEngine method close.

@Override
public void close() {
    for (QueryMetadata queryMetadata : allLiveQueries) {
        queryMetadata.close();
    }
    topicClient.close();
    engineMetrics.close();
    aggregateMetricsCollector.shutdown();
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata)

Example 14 with QueryMetadata

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

the class KsqlEngine method planQueries.

private List<QueryMetadata> planQueries(final List<Pair<String, Statement>> statementList, final Map<String, Object> overriddenProperties, final MetaStore tempMetaStore) throws Exception {
    // Logical plan creation from the ASTs
    List<Pair<String, PlanNode>> logicalPlans = queryEngine.buildLogicalPlans(tempMetaStore, statementList);
    // Physical plan creation from logical plans.
    List<QueryMetadata> runningQueries = queryEngine.buildPhysicalPlans(logicalPlans, statementList, overriddenProperties, true);
    for (QueryMetadata queryMetadata : runningQueries) {
        if (queryMetadata instanceof PersistentQueryMetadata) {
            livePersistentQueries.add(queryMetadata);
            PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
            persistentQueries.put(persistentQueryMetadata.getId(), persistentQueryMetadata);
        }
        allLiveQueries.add(queryMetadata);
    }
    return runningQueries;
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Pair(io.confluent.ksql.util.Pair)

Example 15 with QueryMetadata

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

the class KsqlEngine method terminateQuery.

@Override
public boolean terminateQuery(final QueryId queryId, final boolean closeStreams) {
    QueryMetadata queryMetadata = persistentQueries.remove(queryId);
    if (queryMetadata == null) {
        return false;
    }
    livePersistentQueries.remove(queryMetadata);
    allLiveQueries.remove(queryMetadata);
    if (closeStreams) {
        queryMetadata.close();
    }
    return true;
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata)

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