Search in sources :

Example 6 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class TimestampExtractionPolicyFactoryTest method shouldCreateTimestampPolicyWhenTimestampFieldIsOfTypeTimestamp.

@Test
public void shouldCreateTimestampPolicyWhenTimestampFieldIsOfTypeTimestamp() {
    // Given:
    final String timestamp = "timestamp";
    final LogicalSchema schema = schemaBuilder2.valueColumn(ColumnName.of(timestamp.toUpperCase()), SqlTypes.TIMESTAMP).build();
    // When:
    final TimestampExtractionPolicy result = TimestampExtractionPolicyFactory.create(ksqlConfig, schema, Optional.of(new TimestampColumn(ColumnName.of(timestamp.toUpperCase()), Optional.empty())));
    // Then:
    assertThat(result, instanceOf(TimestampColumnTimestampExtractionPolicy.class));
    assertThat(result.getTimestampField(), equalTo(ColumnName.of(timestamp.toUpperCase())));
}
Also used : TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class TimestampExtractionPolicyFactoryTest method shouldCreateStringTimestampPolicyWhenTimestampFieldIsStringTypeAndFormatProvided.

@Test
public void shouldCreateStringTimestampPolicyWhenTimestampFieldIsStringTypeAndFormatProvided() {
    // Given:
    final String field = "my_string_field";
    final LogicalSchema schema = schemaBuilder2.valueColumn(ColumnName.of(field.toUpperCase()), SqlTypes.STRING).build();
    // When:
    final TimestampExtractionPolicy result = TimestampExtractionPolicyFactory.create(ksqlConfig, schema, Optional.of(new TimestampColumn(ColumnName.of(field.toUpperCase()), Optional.of("yyyy-MM-DD"))));
    // Then:
    assertThat(result, instanceOf(StringTimestampExtractionPolicy.class));
    assertThat(result.getTimestampField(), equalTo(ColumnName.of(field.toUpperCase())));
}
Also used : TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class SinkBuilder method build.

public static <K> void build(final LogicalSchema schema, final Formats formats, final Optional<TimestampColumn> timestampColumn, final String topicName, final KStream<K, GenericRow> stream, final ExecutionKeyFactory<K> executionKeyFactory, final QueryContext queryContext, final RuntimeBuildContext buildContext) {
    final PhysicalSchema physicalSchema = PhysicalSchema.from(schema, formats.getKeyFeatures(), formats.getValueFeatures());
    final Serde<K> keySerde = executionKeyFactory.buildKeySerde(formats.getKeyFormat(), physicalSchema, queryContext);
    final Serde<GenericRow> valueSerde = buildContext.buildValueSerde(formats.getValueFormat(), physicalSchema, queryContext);
    final Optional<TransformTimestamp<K>> tsTransformer = timestampTransformer(buildContext, queryContext, schema, timestampColumn);
    final KStream<K, GenericRow> transformed = tsTransformer.map(t -> stream.transform(t, Named.as(TIMESTAMP_TRANSFORM_NAME + StreamsUtil.buildOpName(queryContext)))).orElse(stream);
    transformed.to(topicName, Produced.with(keySerde, valueSerde));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) Produced(org.apache.kafka.streams.kstream.Produced) Transformer(org.apache.kafka.streams.kstream.Transformer) QueryContext(io.confluent.ksql.execution.context.QueryContext) KeyValue(org.apache.kafka.streams.KeyValue) KStream(org.apache.kafka.streams.kstream.KStream) ExecutionKeyFactory(io.confluent.ksql.execution.plan.ExecutionKeyFactory) Formats(io.confluent.ksql.execution.plan.Formats) KsqlTimestampExtractor(io.confluent.ksql.execution.streams.timestamp.KsqlTimestampExtractor) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) To(org.apache.kafka.streams.processor.To) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) TimestampExtractionPolicy(io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicy) TransformerSupplier(org.apache.kafka.streams.kstream.TransformerSupplier) GenericRow(io.confluent.ksql.GenericRow) TimestampExtractionPolicyFactory(io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicyFactory) Serde(org.apache.kafka.common.serialization.Serde) Objects.requireNonNull(java.util.Objects.requireNonNull) Named(org.apache.kafka.streams.kstream.Named) ProcessingLogger(io.confluent.ksql.logging.processing.ProcessingLogger) Optional(java.util.Optional) RecordProcessingError(io.confluent.ksql.logging.processing.RecordProcessingError) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema)

Example 9 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class SourceDescriptionFactoryTest method shouldReturnTimestampColumnIfPresent.

@Test
public void shouldReturnTimestampColumnIfPresent() {
    // Given:
    final String kafkaTopicName = "kafka";
    final DataSource dataSource = buildDataSource(kafkaTopicName, Optional.of(new TimestampColumn(ColumnName.of("foo"), Optional.empty())));
    // When
    final SourceDescription sourceDescription = SourceDescriptionFactory.create(dataSource, true, Collections.emptyList(), Collections.emptyList(), Optional.empty(), Collections.emptyList(), Collections.emptyList(), new MetricCollectors());
    // Then:
    assertThat(sourceDescription.getTimestamp(), is("foo"));
}
Also used : MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Example 10 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class TimestampExtractionPolicyFactoryTest method shouldThrowIfLongTimestampTypeAndFormatIsSupplied.

@Test
public void shouldThrowIfLongTimestampTypeAndFormatIsSupplied() {
    // Given:
    final String timestamp = "timestamp";
    final LogicalSchema schema = schemaBuilder2.valueColumn(ColumnName.of(timestamp.toUpperCase()), SqlTypes.BIGINT).build();
    // When:
    assertThrows(KsqlException.class, () -> TimestampExtractionPolicyFactory.create(ksqlConfig, schema, Optional.of(new TimestampColumn(ColumnName.of(timestamp.toUpperCase()), Optional.of("b")))));
}
Also used : TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)20 Test (org.junit.Test)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)11 Matchers.containsString (org.hamcrest.Matchers.containsString)7 CreateStreamCommand (io.confluent.ksql.execution.ddl.commands.CreateStreamCommand)3 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)3 DataSource (io.confluent.ksql.metastore.model.DataSource)3 ColumnName (io.confluent.ksql.name.ColumnName)3 KsqlException (io.confluent.ksql.util.KsqlException)3 QueryContext (io.confluent.ksql.execution.context.QueryContext)2 CreateTableCommand (io.confluent.ksql.execution.ddl.commands.CreateTableCommand)2 TimestampExtractionPolicy (io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicy)2 SourceName (io.confluent.ksql.name.SourceName)2 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 Column (io.confluent.ksql.schema.ksql.Column)2 Named (org.apache.kafka.streams.kstream.Named)2 GenericRow (io.confluent.ksql.GenericRow)1 Into (io.confluent.ksql.analyzer.Analysis.Into)1 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)1