use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.
the class KsqlTesterTest method createTopics.
private void createTopics(final PreparedStatement<?> engineStatement) {
if (engineStatement.getStatement() instanceof CreateSource) {
final CreateSource statement = (CreateSource) engineStatement.getStatement();
topicClient.preconditionTopicExists(statement.getProperties().getKafkaTopic(), statement.getProperties().getPartitions().orElse(1), statement.getProperties().getReplicas().orElse((short) 1), ImmutableMap.of());
} else if (engineStatement.getStatement() instanceof CreateAsSelect) {
final CreateAsSelect statement = (CreateAsSelect) engineStatement.getStatement();
topicClient.preconditionTopicExists(statement.getProperties().getKafkaTopic().orElse(statement.getName().toString(FormatOptions.noEscape()).toUpperCase()), statement.getProperties().getPartitions().orElse(1), statement.getProperties().getReplicas().orElse((short) 1), ImmutableMap.of());
}
}
use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.
the class TopicCreateInjectorTest method shouldPassThroughWithClauseToBuilderForCreate.
@Test
public void shouldPassThroughWithClauseToBuilderForCreate() {
// Given:
givenStatement("CREATE STREAM x (FOO VARCHAR) WITH(value_format='avro', kafka_topic='topic', partitions=2);");
final CreateSourceProperties props = ((CreateSource) statement.getStatement()).getProperties();
// When:
injector.inject(statement, builder);
// Then:
verify(builder).withWithClause(Optional.of(props.getKafkaTopic()), props.getPartitions(), props.getReplicas());
}
use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.
the class KsqlParserTest method shouldParseCustomTypesInCreateSource.
@Test
public void shouldParseCustomTypesInCreateSource() {
// Given:
final SqlStruct cookie = SqlStruct.builder().field("type", SqlTypes.STRING).build();
metaStore.registerType("cookie", cookie);
// When:
final PreparedStatement<CreateSource> createSource = KsqlParserTestUtil.buildSingleAst("CREATE STREAM foo (cookie COOKIE) WITH (KAFKA_TOPIC='foo', VALUE_FORMAT='AVRO');", metaStore);
// Then:
final TableElements elements = createSource.getStatement().getElements();
assertThat(Iterables.size(elements), is(1));
final TableElement element = elements.iterator().next();
assertThat(element.getType().getSqlType(), is(cookie));
}
use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.
the class TopicCreateInjector method injectForCreateSource.
private ConfiguredStatement<? extends CreateSource> injectForCreateSource(final ConfiguredStatement<? extends CreateSource> statement, final TopicProperties.Builder topicPropertiesBuilder) {
final CreateSource createSource = statement.getStatement();
final CreateSourceProperties properties = createSource.getProperties();
final String topicName = properties.getKafkaTopic();
if (topicClient.isTopicExists(topicName)) {
topicPropertiesBuilder.withSource(() -> topicClient.describeTopic(topicName));
} else if (!properties.getPartitions().isPresent()) {
final CreateSource example = createSource.copyWith(createSource.getElements(), properties.withPartitions(2));
throw new KsqlException("Topic '" + topicName + "' does not exist. If you want to create a new topic for the " + "stream/table please re-run the statement providing the required '" + CommonCreateConfigs.SOURCE_NUMBER_OF_PARTITIONS + "' configuration in the WITH " + "clause (and optionally '" + CommonCreateConfigs.SOURCE_NUMBER_OF_REPLICAS + "'). " + "For example: " + SqlFormatter.formatSql(example));
}
topicPropertiesBuilder.withName(topicName).withWithClause(Optional.of(properties.getKafkaTopic()), properties.getPartitions(), properties.getReplicas());
final String topicCleanUpPolicy = createSource instanceof CreateTable ? TopicConfig.CLEANUP_POLICY_COMPACT : TopicConfig.CLEANUP_POLICY_DELETE;
createTopic(topicPropertiesBuilder, topicCleanUpPolicy);
return statement;
}
use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.
the class DefaultSchemaInjector method forCreateStatement.
private Optional<ConfiguredStatement<CreateSource>> forCreateStatement(final ConfiguredStatement<CreateSource> statement) {
final Optional<SchemaAndId> keySchema = getKeySchema(statement);
final Optional<SchemaAndId> valueSchema = getValueSchema(statement);
if (!keySchema.isPresent() && !valueSchema.isPresent()) {
return Optional.empty();
}
final CreateSource withSchema = addSchemaFields(statement, keySchema, valueSchema);
final PreparedStatement<CreateSource> prepared = buildPreparedStatement(withSchema);
final ImmutableMap.Builder<String, Object> overrideBuilder = ImmutableMap.builder();
// Only store raw schema if schema id is provided by user
if (withSchema.getProperties().getKeySchemaId().isPresent()) {
keySchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.KEY_SCHEMA_ID, schemaAndId));
}
if (withSchema.getProperties().getValueSchemaId().isPresent()) {
valueSchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.VALUE_SCHEMA_ID, schemaAndId));
}
final ConfiguredStatement<CreateSource> configured = ConfiguredStatement.of(prepared, statement.getSessionConfig().copyWith(overrideBuilder.build()));
return Optional.of(configured);
}
Aggregations