use of io.confluent.ksql.util.AvroUtil in project ksql by confluentinc.
the class QueryEngine method maybeAddFieldsFromSchemaRegistry.
private Pair<DdlStatement, String> maybeAddFieldsFromSchemaRegistry(AbstractStreamCreateStatement streamCreateStatement) {
if (streamCreateStatement.getProperties().containsKey(DdlConfig.TOPIC_NAME_PROPERTY)) {
String ksqlRegisteredTopicName = StringUtil.cleanQuotes(streamCreateStatement.getProperties().get(DdlConfig.TOPIC_NAME_PROPERTY).toString().toUpperCase());
KsqlTopic ksqlTopic = ksqlEngine.getMetaStore().getTopic(ksqlRegisteredTopicName);
if (ksqlTopic == null) {
throw new KsqlException(String.format("Could not find %s topic in the metastore.", ksqlRegisteredTopicName));
}
Map<String, Expression> newProperties = new HashMap<>();
newProperties.put(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral(ksqlTopic.getKafkaTopicName()));
newProperties.put(DdlConfig.VALUE_FORMAT_PROPERTY, new StringLiteral(ksqlTopic.getKsqlTopicSerDe().getSerDe().toString()));
streamCreateStatement = streamCreateStatement.copyWith(streamCreateStatement.getElements(), newProperties);
}
Pair<AbstractStreamCreateStatement, String> avroCheckResult = new AvroUtil().checkAndSetAvroSchema(streamCreateStatement, new HashMap<>(), ksqlEngine.getSchemaRegistryClient());
if (avroCheckResult.getRight() != null) {
if (avroCheckResult.getLeft() instanceof CreateStream) {
return new Pair<>((CreateStream) avroCheckResult.getLeft(), avroCheckResult.getRight());
} else if (avroCheckResult.getLeft() instanceof CreateTable) {
return new Pair<>((CreateTable) avroCheckResult.getLeft(), avroCheckResult.getRight());
}
}
return new Pair<>(null, null);
}
use of io.confluent.ksql.util.AvroUtil 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());
}
Aggregations