Search in sources :

Example 6 with CreateSource

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());
    }
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect)

Example 7 with CreateSource

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());
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 8 with CreateSource

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));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) TableElements(io.confluent.ksql.parser.tree.TableElements) CreateSource(io.confluent.ksql.parser.tree.CreateSource) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 9 with CreateSource

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;
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) CreateTable(io.confluent.ksql.parser.tree.CreateTable) KsqlException(io.confluent.ksql.util.KsqlException) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

Example 10 with CreateSource

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);
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSource(io.confluent.ksql.parser.tree.CreateSource) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

CreateSource (io.confluent.ksql.parser.tree.CreateSource)13 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)7 FormatInfo (io.confluent.ksql.serde.FormatInfo)5 Format (io.confluent.ksql.serde.Format)4 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)3 TableElements (io.confluent.ksql.parser.tree.TableElements)3 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)3 SerdeFeatures (io.confluent.ksql.serde.SerdeFeatures)3 KsqlException (io.confluent.ksql.util.KsqlException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 TableElement (io.confluent.ksql.parser.tree.TableElement)2 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)2 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 SetView (com.google.common.collect.Sets.SetView)1 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1