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()));
}
}
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.");
}
}
}
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();
}
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;
}
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;
}
Aggregations