Search in sources :

Example 11 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project apache-kafka-on-k8s by banzaicloud.

the class KStreamImplTest method shouldUseRecordMetadataTimestampExtractorWhenInternalRepartitioningTopicCreated.

@Test
public // TODO: this test should be refactored when we removed KStreamBuilder so that the created Topology contains internal topics as well
void shouldUseRecordMetadataTimestampExtractorWhenInternalRepartitioningTopicCreated() {
    final KStreamBuilder builder = new KStreamBuilder();
    KStream<String, String> kStream = builder.stream(stringSerde, stringSerde, "topic-1");
    ValueJoiner<String, String, String> valueJoiner = MockValueJoiner.instance(":");
    long windowSize = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
    final KStream<String, String> stream = kStream.map(new KeyValueMapper<String, String, KeyValue<? extends String, ? extends String>>() {

        @Override
        public KeyValue<? extends String, ? extends String> apply(String key, String value) {
            return KeyValue.pair(value, value);
        }
    });
    stream.join(kStream, valueJoiner, JoinWindows.of(windowSize).until(3 * windowSize), Joined.with(Serdes.String(), Serdes.String(), Serdes.String())).to(Serdes.String(), Serdes.String(), "output-topic");
    ProcessorTopology processorTopology = builder.setApplicationId("X").build(null);
    SourceNode originalSourceNode = processorTopology.source("topic-1");
    for (SourceNode sourceNode : processorTopology.sources()) {
        if (sourceNode.name().equals(originalSourceNode.name())) {
            assertEquals(sourceNode.getTimestampExtractor(), null);
        } else {
            assertThat(sourceNode.getTimestampExtractor(), instanceOf(FailOnInvalidTimestamp.class));
        }
    }
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) SourceNode(org.apache.kafka.streams.processor.internals.SourceNode) KeyValue(org.apache.kafka.streams.KeyValue) FailOnInvalidTimestamp(org.apache.kafka.streams.processor.FailOnInvalidTimestamp) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 12 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project apache-kafka-on-k8s by banzaicloud.

the class InternalStreamsBuilderTest method shouldAddGlobalTablesToEachGroup.

@Test
public void shouldAddGlobalTablesToEachGroup() throws Exception {
    final String one = "globalTable";
    final String two = "globalTable2";
    final GlobalKTable<String, String> globalTable = builder.globalTable("table", consumed, new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(one), builder, storePrefix));
    final GlobalKTable<String, String> globalTable2 = builder.globalTable("table2", consumed, new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(two), builder, storePrefix));
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materialized = new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("not-global"), builder, storePrefix);
    builder.table("not-global", consumed, materialized);
    final KeyValueMapper<String, String, String> kvMapper = new KeyValueMapper<String, String, String>() {

        @Override
        public String apply(final String key, final String value) {
            return value;
        }
    };
    final KStream<String, String> stream = builder.stream(Collections.singleton("t1"), consumed);
    stream.leftJoin(globalTable, kvMapper, MockValueJoiner.TOSTRING_JOINER);
    final KStream<String, String> stream2 = builder.stream(Collections.singleton("t2"), consumed);
    stream2.leftJoin(globalTable2, kvMapper, MockValueJoiner.TOSTRING_JOINER);
    final Map<Integer, Set<String>> nodeGroups = builder.internalTopologyBuilder.nodeGroups();
    for (Integer groupId : nodeGroups.keySet()) {
        final ProcessorTopology topology = builder.internalTopologyBuilder.build(groupId);
        final List<StateStore> stateStores = topology.globalStateStores();
        final Set<String> names = new HashSet<>();
        for (StateStore stateStore : stateStores) {
            names.add(stateStore.name());
        }
        assertEquals(2, stateStores.size());
        assertTrue(names.contains(one));
        assertTrue(names.contains(two));
    }
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) HashSet(java.util.HashSet) Set(java.util.Set) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Bytes(org.apache.kafka.common.utils.Bytes) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project apache-kafka-on-k8s by banzaicloud.

the class InternalStreamsBuilderTest method shouldStillMaterializeSourceKTableIfMaterializedIsntQueryable.

@Test
public void shouldStillMaterializeSourceKTableIfMaterializedIsntQueryable() throws Exception {
    KTable table1 = builder.table("topic2", consumed, new MaterializedInternal<>(Materialized.<String, String, KeyValueStore<Bytes, byte[]>>with(null, null), builder, storePrefix));
    final ProcessorTopology topology = builder.internalTopologyBuilder.build(null);
    assertEquals(1, topology.stateStores().size());
    final String storeName = "prefix-STATE-STORE-0000000000";
    assertEquals(storeName, topology.stateStores().get(0).name());
    assertEquals(1, topology.storeToChangelogTopic().size());
    assertEquals("topic2", topology.storeToChangelogTopic().get(storeName));
    assertNull(table1.queryableStoreName());
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) KTable(org.apache.kafka.streams.kstream.KTable) GlobalKTable(org.apache.kafka.streams.kstream.GlobalKTable) Test(org.junit.Test)

Example 14 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project apache-kafka-on-k8s by banzaicloud.

the class InternalStreamsBuilderTest method doBuildGlobalTopologyWithAllGlobalTables.

private void doBuildGlobalTopologyWithAllGlobalTables() throws Exception {
    final ProcessorTopology topology = builder.internalTopologyBuilder.buildGlobalStateTopology();
    final List<StateStore> stateStores = topology.globalStateStores();
    final Set<String> sourceTopics = topology.sourceTopics();
    assertEquals(Utils.mkSet("table", "table2"), sourceTopics);
    assertEquals(2, stateStores.size());
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) StateStore(org.apache.kafka.streams.processor.StateStore)

Example 15 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project apache-kafka-on-k8s by banzaicloud.

the class InternalStreamsBuilderTest method ktableShouldHaveNullTimestampExtractorWhenNoneSupplied.

@Test
public void ktableShouldHaveNullTimestampExtractorWhenNoneSupplied() throws Exception {
    builder.table("topic", consumed, materialized);
    final ProcessorTopology processorTopology = builder.internalTopologyBuilder.build(null);
    assertNull(processorTopology.source("topic").getTimestampExtractor());
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) Test(org.junit.Test)

Aggregations

ProcessorTopology (org.apache.kafka.streams.processor.internals.ProcessorTopology)95 Test (org.junit.Test)89 Bytes (org.apache.kafka.common.utils.Bytes)17 MockTimestampExtractor (org.apache.kafka.test.MockTimestampExtractor)14 StateStore (org.apache.kafka.streams.processor.StateStore)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)10 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)8 FailOnInvalidTimestamp (org.apache.kafka.streams.processor.FailOnInvalidTimestamp)6 Pattern (java.util.regex.Pattern)5 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)5 HashSet (java.util.HashSet)4 Properties (java.util.Properties)4 Set (java.util.Set)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 List (java.util.List)3 KTable (org.apache.kafka.streams.kstream.KTable)3 InternalTopologyBuilder (org.apache.kafka.streams.processor.internals.InternalTopologyBuilder)3 SourceNode (org.apache.kafka.streams.processor.internals.SourceNode)3 Arrays.asList (java.util.Arrays.asList)2 Collections (java.util.Collections)2