Search in sources :

Example 1 with TimestampExtractor

use of org.apache.kafka.streams.processor.TimestampExtractor in project kafka-tutorials by confluentinc.

the class ClickEventTimestampExtractorTest method testExtract.

@Test
public void testExtract() {
    TimestampExtractor timestampExtractor = new ClickEventTimestampExtractor();
    long ts = 3333333L;
    Clicks clickEvent = Clicks.newBuilder().setTimestamp(ts).setIp("ip").setUrl("url").build();
    ConsumerRecord<Object, Object> consumerRecord = new ConsumerRecord<>("topic", 1, 1L, clickEvent.getIp(), clickEvent);
    long extracted = timestampExtractor.extract(consumerRecord, -1L);
    assertEquals(ts, extracted);
}
Also used : TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor) Clicks(io.confluent.developer.avro.Clicks) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Example 2 with TimestampExtractor

use of org.apache.kafka.streams.processor.TimestampExtractor in project ksql by confluentinc.

the class SourceBuilderV1Test method shouldBuildStreamWithCorrectTimestampExtractor.

@Test
@SuppressWarnings("unchecked")
public void shouldBuildStreamWithCorrectTimestampExtractor() {
    // Given:
    givenUnwindowedSourceStream();
    final ConsumerRecord<Object, Object> record = mock(ConsumerRecord.class);
    when(record.value()).thenReturn(GenericRow.genericRow("123", A_ROWTIME));
    // When:
    streamSource.build(planBuilder, planInfo);
    // Then:
    verify(consumed).withTimestampExtractor(timestampExtractorCaptor.capture());
    final TimestampExtractor extractor = timestampExtractorCaptor.getValue();
    assertThat(extractor.extract(record, 789), is(A_ROWTIME));
}
Also used : TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor) Test(org.junit.Test)

Example 3 with TimestampExtractor

use of org.apache.kafka.streams.processor.TimestampExtractor in project ksql by confluentinc.

the class TimestampExtractionPolicyFactoryTest method shouldCreateMetadataPolicyWhenTimestampFieldNotProvided.

@Test
public void shouldCreateMetadataPolicyWhenTimestampFieldNotProvided() {
    // When:
    final TimestampExtractionPolicy result = TimestampExtractionPolicyFactory.create(ksqlConfig, schemaBuilder2.build(), Optional.empty());
    // Then:
    assertThat(result, instanceOf(MetadataTimestampExtractionPolicy.class));
    final TimestampExtractor timestampExtractor = result.create(Optional.empty(), true, logger);
    assertThat(timestampExtractor, instanceOf(LoggingTimestampExtractor.class));
    assertThat(((LoggingTimestampExtractor) timestampExtractor).getDelegate(), instanceOf(MetadataTimestampExtractor.class));
}
Also used : TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor) Test(org.junit.Test)

Example 4 with TimestampExtractor

use of org.apache.kafka.streams.processor.TimestampExtractor in project ksql by confluentinc.

the class SourceBuilderUtils method buildSourceConsumed.

static <K> Consumed<K, GenericRow> buildSourceConsumed(final SourceStep<?> streamSource, final Serde<K> keySerde, final Serde<GenericRow> valueSerde, final Topology.AutoOffsetReset defaultReset, final RuntimeBuildContext buildContext, final ConsumedFactory consumedFactory) {
    final TimestampExtractor timestampExtractor = timestampExtractor(buildContext.getKsqlConfig(), streamSource.getSourceSchema(), streamSource.getTimestampColumn(), streamSource, buildContext);
    final Consumed<K, GenericRow> consumed = consumedFactory.create(keySerde, valueSerde).withTimestampExtractor(timestampExtractor);
    return consumed.withOffsetResetPolicy(getAutoOffsetReset(defaultReset, buildContext));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor)

Example 5 with TimestampExtractor

use of org.apache.kafka.streams.processor.TimestampExtractor in project spring-cloud-stream by spring-cloud.

the class StreamToGlobalKTableFunctionTests method testTimeExtractor.

@Test
void testTimeExtractor() throws Exception {
    SpringApplication app = new SpringApplication(OrderEnricherApplication.class);
    app.setWebApplicationType(WebApplicationType.NONE);
    try (ConfigurableApplicationContext context = app.run("--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.function.definition=forTimeExtractorTest", "--spring.cloud.stream.bindings.forTimeExtractorTest-in-0.destination=orders", "--spring.cloud.stream.bindings.forTimeExtractorTest-in-1.destination=customers", "--spring.cloud.stream.bindings.forTimeExtractorTest-in-2.destination=products", "--spring.cloud.stream.bindings.forTimeExtractorTest-out-0.destination=enriched-order", "--spring.cloud.stream.kafka.streams.bindings.forTimeExtractorTest-in-0.consumer.timestampExtractorBeanName" + "=timestampExtractor", "--spring.cloud.stream.kafka.streams.bindings.forTimeExtractorTest-in-1.consumer.timestampExtractorBeanName" + "=timestampExtractor", "--spring.cloud.stream.kafka.streams.bindings.forTimeExtractorTest-in-2.consumer.timestampExtractorBeanName" + "=timestampExtractor", "--spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms=10000", "--spring.cloud.stream.kafka.streams.bindings.order.consumer.applicationId=" + "testTimeExtractor-abc", "--spring.cloud.stream.kafka.streams.binder.brokers=" + embeddedKafka.getBrokersAsString())) {
        final KafkaStreamsExtendedBindingProperties kafkaStreamsExtendedBindingProperties = context.getBean(KafkaStreamsExtendedBindingProperties.class);
        final Map<String, KafkaStreamsBindingProperties> bindings = kafkaStreamsExtendedBindingProperties.getBindings();
        final KafkaStreamsBindingProperties kafkaStreamsBindingProperties0 = bindings.get("forTimeExtractorTest-in-0");
        final String timestampExtractorBeanName0 = kafkaStreamsBindingProperties0.getConsumer().getTimestampExtractorBeanName();
        final TimestampExtractor timestampExtractor0 = context.getBean(timestampExtractorBeanName0, TimestampExtractor.class);
        assertThat(timestampExtractor0).isNotNull();
        final KafkaStreamsBindingProperties kafkaStreamsBindingProperties1 = bindings.get("forTimeExtractorTest-in-1");
        final String timestampExtractorBeanName1 = kafkaStreamsBindingProperties1.getConsumer().getTimestampExtractorBeanName();
        final TimestampExtractor timestampExtractor1 = context.getBean(timestampExtractorBeanName1, TimestampExtractor.class);
        assertThat(timestampExtractor1).isNotNull();
        final KafkaStreamsBindingProperties kafkaStreamsBindingProperties2 = bindings.get("forTimeExtractorTest-in-2");
        final String timestampExtractorBeanName2 = kafkaStreamsBindingProperties2.getConsumer().getTimestampExtractorBeanName();
        final TimestampExtractor timestampExtractor2 = context.getBean(timestampExtractorBeanName2, TimestampExtractor.class);
        assertThat(timestampExtractor2).isNotNull();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) KafkaStreamsExtendedBindingProperties(org.springframework.cloud.stream.binder.kafka.streams.properties.KafkaStreamsExtendedBindingProperties) SpringApplication(org.springframework.boot.SpringApplication) TimestampExtractor(org.apache.kafka.streams.processor.TimestampExtractor) WallclockTimestampExtractor(org.apache.kafka.streams.processor.WallclockTimestampExtractor) KafkaStreamsBindingProperties(org.springframework.cloud.stream.binder.kafka.streams.properties.KafkaStreamsBindingProperties) Test(org.junit.jupiter.api.Test)

Aggregations

TimestampExtractor (org.apache.kafka.streams.processor.TimestampExtractor)9 Test (org.junit.Test)4 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)3 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 KeyValue (org.apache.kafka.streams.KeyValue)2 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)2 Transformer (org.apache.kafka.streams.kstream.Transformer)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2 IndexNameHelper (org.phoebus.util.indexname.IndexNameHelper)2 Clicks (io.confluent.developer.avro.Clicks)1 GenericRow (io.confluent.ksql.GenericRow)1 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Serde (org.apache.kafka.common.serialization.Serde)1 Serdes (org.apache.kafka.common.serialization.Serdes)1