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