Search in sources :

Example 71 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project kafka by apache.

the class InternalStreamsBuilderTest method shouldAddGlobalTablesToEachGroup.

@Test
public void shouldAddGlobalTablesToEachGroup() {
    final String one = "globalTable";
    final String two = "globalTable2";
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materializedInternal = new MaterializedInternal<>(Materialized.as(one), builder, storePrefix);
    final GlobalKTable<String, String> globalTable = builder.globalTable("table", consumed, materializedInternal);
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materializedInternal2 = new MaterializedInternal<>(Materialized.as(two), builder, storePrefix);
    final GlobalKTable<String, String> globalTable2 = builder.globalTable("table2", consumed, materializedInternal2);
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materializedInternalNotGlobal = new MaterializedInternal<>(Materialized.as("not-global"), builder, storePrefix);
    builder.table("not-global", consumed, materializedInternalNotGlobal);
    final KeyValueMapper<String, String, String> kvMapper = (key, value) -> 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 (final Integer groupId : nodeGroups.keySet()) {
        final ProcessorTopology topology = builder.internalTopologyBuilder.buildSubtopology(groupId);
        final List<StateStore> stateStores = topology.globalStateStores();
        final Set<String> names = new HashSet<>();
        for (final StateStore stateStore : stateStores) {
            names.add(stateStore.name());
        }
        assertEquals(2, stateStores.size());
        assertTrue(names.contains(one));
        assertTrue(names.contains(two));
    }
}
Also used : StreamsConfig(org.apache.kafka.streams.StreamsConfig) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) KStream(org.apache.kafka.streams.kstream.KStream) AutoOffsetReset(org.apache.kafka.streams.Topology.AutoOffsetReset) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) HashSet(java.util.HashSet) Arrays.asList(java.util.Arrays.asList) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Utils(org.apache.kafka.common.utils.Utils) MockValueJoiner(org.apache.kafka.test.MockValueJoiner) MockMapper(org.apache.kafka.test.MockMapper) KTable(org.apache.kafka.streams.kstream.KTable) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Consumed(org.apache.kafka.streams.kstream.Consumed) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Bytes(org.apache.kafka.common.utils.Bytes) MockTimestampExtractor(org.apache.kafka.test.MockTimestampExtractor) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) GlobalKTable(org.apache.kafka.streams.kstream.GlobalKTable) StateStore(org.apache.kafka.streams.processor.StateStore) Materialized(org.apache.kafka.streams.kstream.Materialized) InternalTopologyBuilder(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) Pattern(java.util.regex.Pattern) ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 72 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project kafka by apache.

the class InternalStreamsBuilderTest method ktableShouldHaveNullTimestampExtractorWhenNoneSupplied.

@Test
public void ktableShouldHaveNullTimestampExtractorWhenNoneSupplied() {
    builder.table("topic", consumed, materialized);
    builder.buildAndOptimizeTopology();
    final ProcessorTopology processorTopology = builder.internalTopologyBuilder.rewriteTopology(new StreamsConfig(StreamsTestUtils.getStreamsConfig(APP_ID))).buildTopology();
    assertNull(processorTopology.source("topic").getTimestampExtractor());
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 73 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project kafka by apache.

the class InternalStreamsBuilderTest method shouldBuildSimpleGlobalTableTopology.

@Test
public void shouldBuildSimpleGlobalTableTopology() {
    final MaterializedInternal<String, String, KeyValueStore<Bytes, byte[]>> materializedInternal = new MaterializedInternal<>(Materialized.as("globalTable"), builder, storePrefix);
    builder.globalTable("table", consumed, materializedInternal);
    builder.buildAndOptimizeTopology();
    final ProcessorTopology topology = builder.internalTopologyBuilder.rewriteTopology(new StreamsConfig(StreamsTestUtils.getStreamsConfig(APP_ID))).buildGlobalStateTopology();
    final List<StateStore> stateStores = topology.globalStateStores();
    assertEquals(1, stateStores.size());
    assertEquals("globalTable", stateStores.get(0).name());
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 74 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project kafka by apache.

the class StreamsBuilderTest method shouldNotAddThirdStateStoreIfStreamStreamJoinFixIsDisabledViaOldApi.

@SuppressWarnings("deprecation")
@Test
public void shouldNotAddThirdStateStoreIfStreamStreamJoinFixIsDisabledViaOldApi() {
    final KStream<String, String> streamOne = builder.stream(STREAM_TOPIC);
    final KStream<String, String> streamTwo = builder.stream(STREAM_TOPIC_TWO);
    streamOne.leftJoin(streamTwo, (value1, value2) -> value1, JoinWindows.of(Duration.ofHours(1)), StreamJoined.<String, String, String>as(STREAM_OPERATION_NAME).withName(STREAM_OPERATION_NAME));
    final Properties properties = new Properties();
    builder.build(properties);
    final ProcessorTopology topology = builder.internalTopologyBuilder.rewriteTopology(new StreamsConfig(props)).buildTopology();
    assertNamesForStateStore(topology.stateStores(), STREAM_OPERATION_NAME + "-this-join-store", STREAM_OPERATION_NAME + "-outer-other-join-store");
    assertNamesForOperation(topology, "KSTREAM-SOURCE-0000000000", "KSTREAM-SOURCE-0000000001", STREAM_OPERATION_NAME + "-this-windowed", STREAM_OPERATION_NAME + "-other-windowed", STREAM_OPERATION_NAME + "-this-join", STREAM_OPERATION_NAME + "-outer-other-join", STREAM_OPERATION_NAME + "-merge");
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) Properties(java.util.Properties) Test(org.junit.Test)

Example 75 with ProcessorTopology

use of org.apache.kafka.streams.processor.internals.ProcessorTopology in project kafka by apache.

the class StreamsBuilderTest method shouldUseSpecifiedNameForSplitOperation.

@Test
public void shouldUseSpecifiedNameForSplitOperation() {
    builder.stream(STREAM_TOPIC).split(Named.as("branch-processor")).branch((k, v) -> true, Branched.as("-1")).branch((k, v) -> false, Branched.as("-2"));
    builder.build();
    final ProcessorTopology topology = builder.internalTopologyBuilder.rewriteTopology(new StreamsConfig(props)).buildTopology();
    assertNamesForOperation(topology, "KSTREAM-SOURCE-0000000000", "branch-processor", "branch-processor-1", "branch-processor-2");
}
Also used : Arrays(java.util.Arrays) Produced(org.apache.kafka.streams.kstream.Produced) Stores(org.apache.kafka.streams.state.Stores) Joined(org.apache.kafka.streams.kstream.Joined) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) Arrays.asList(java.util.Arrays.asList) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Duration(java.time.Duration) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Assert.fail(org.junit.Assert.fail) NoopValueTransformer(org.apache.kafka.test.NoopValueTransformer) MockValueJoiner(org.apache.kafka.test.MockValueJoiner) MockMapper(org.apache.kafka.test.MockMapper) ProcessorContext(org.apache.kafka.streams.processor.api.ProcessorContext) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) Instant(java.time.Instant) Bytes(org.apache.kafka.common.utils.Bytes) NoopValueTransformerWithKey(org.apache.kafka.test.NoopValueTransformerWithKey) List(java.util.List) ProcessorNode(org.apache.kafka.streams.processor.internals.ProcessorNode) Materialized(org.apache.kafka.streams.kstream.Materialized) Pattern(java.util.regex.Pattern) ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertThrows(org.junit.Assert.assertThrows) SUBTOPOLOGY_0(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.SUBTOPOLOGY_0) SUBTOPOLOGY_1(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.SUBTOPOLOGY_1) HashMap(java.util.HashMap) AutoOffsetReset(org.apache.kafka.streams.Topology.AutoOffsetReset) ForeachAction(org.apache.kafka.streams.kstream.ForeachAction) KStream(org.apache.kafka.streams.kstream.KStream) TopologyException(org.apache.kafka.streams.errors.TopologyException) StreamJoined(org.apache.kafka.streams.kstream.StreamJoined) JoinWindows(org.apache.kafka.streams.kstream.JoinWindows) MockPredicate(org.apache.kafka.test.MockPredicate) Named(org.apache.kafka.streams.kstream.Named) Record(org.apache.kafka.streams.processor.api.Record) Processor(org.apache.kafka.streams.processor.api.Processor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Printed(org.apache.kafka.streams.kstream.Printed) Utils(org.apache.kafka.common.utils.Utils) KTable(org.apache.kafka.streams.kstream.KTable) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Branched(org.apache.kafka.streams.kstream.Branched) Grouped(org.apache.kafka.streams.kstream.Grouped) StateStore(org.apache.kafka.streams.processor.StateStore) InternalTopologyBuilder(org.apache.kafka.streams.processor.internals.InternalTopologyBuilder) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) 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