Search in sources :

Example 1 with TableJoined

use of org.apache.kafka.streams.kstream.TableJoined in project kafka by apache.

the class KTableKTableForeignKeyInnerJoinCustomPartitionerIntegrationTest method prepareTopology.

private static KafkaStreams prepareTopology(final String queryableName, final Properties streamsConfig) {
    final UniqueTopicSerdeScope serdeScope = new UniqueTopicSerdeScope();
    final StreamsBuilder builder = new StreamsBuilder();
    final KTable<String, String> table1 = builder.stream(TABLE_1, Consumed.with(serdeScope.decorateSerde(Serdes.String(), streamsConfig, true), serdeScope.decorateSerde(Serdes.String(), streamsConfig, false))).repartition(repartitionA()).toTable(Named.as("table.a"));
    final KTable<String, String> table2 = builder.stream(TABLE_2, Consumed.with(serdeScope.decorateSerde(Serdes.String(), streamsConfig, true), serdeScope.decorateSerde(Serdes.String(), streamsConfig, false))).repartition(repartitionB()).toTable(Named.as("table.b"));
    final Materialized<String, String, KeyValueStore<Bytes, byte[]>> materialized;
    if (queryableName != null) {
        materialized = Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as(queryableName).withKeySerde(serdeScope.decorateSerde(Serdes.String(), streamsConfig, true)).withValueSerde(serdeScope.decorateSerde(Serdes.String(), streamsConfig, false)).withCachingDisabled();
    } else {
        throw new RuntimeException("Current implementation of joinOnForeignKey requires a materialized store");
    }
    final ValueJoiner<String, String, String> joiner = (value1, value2) -> "value1=" + value1 + ",value2=" + value2;
    final TableJoined<String, String> tableJoined = TableJoined.with((topic, key, value, numPartitions) -> Math.abs(getKeyB(key).hashCode()) % numPartitions, (topic, key, value, numPartitions) -> Math.abs(key.hashCode()) % numPartitions);
    table1.join(table2, KTableKTableForeignKeyInnerJoinCustomPartitionerIntegrationTest::getKeyB, joiner, tableJoined, materialized).toStream().to(OUTPUT, Produced.with(serdeScope.decorateSerde(Serdes.String(), streamsConfig, true), serdeScope.decorateSerde(Serdes.String(), streamsConfig, false)));
    return new KafkaStreams(builder.build(streamsConfig), streamsConfig);
}
Also used : UniqueTopicSerdeScope(org.apache.kafka.streams.utils.UniqueTopicSerdeScope) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsConfig(org.apache.kafka.streams.StreamsConfig) TableJoined(org.apache.kafka.streams.kstream.TableJoined) IntegrationTestUtils.startApplicationAndWaitUntilRunning(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.startApplicationAndWaitUntilRunning) BeforeClass(org.junit.BeforeClass) Produced(org.apache.kafka.streams.kstream.Produced) Repartitioned(org.apache.kafka.streams.kstream.Repartitioned) IntegrationTest(org.apache.kafka.test.IntegrationTest) Duration.ofSeconds(java.time.Duration.ofSeconds) HashSet(java.util.HashSet) MockTime(kafka.utils.MockTime) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) Arrays.asList(java.util.Arrays.asList) Named(org.apache.kafka.streams.kstream.Named) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) After(org.junit.After) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) Before(org.junit.Before) IntegrationTestUtils.waitUntilMinKeyValueRecordsReceived(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.waitUntilMinKeyValueRecordsReceived) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KTable(org.apache.kafka.streams.kstream.KTable) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) TestUtils(org.apache.kafka.test.TestUtils) Consumed(org.apache.kafka.streams.kstream.Consumed) KeyValue(org.apache.kafka.streams.KeyValue) Set(java.util.Set) IOException(java.io.IOException) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Bytes(org.apache.kafka.common.utils.Bytes) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) List(java.util.List) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) Materialized(org.apache.kafka.streams.kstream.Materialized) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert.assertEquals(org.junit.Assert.assertEquals) UniqueTopicSerdeScope(org.apache.kafka.streams.utils.UniqueTopicSerdeScope) KafkaStreams(org.apache.kafka.streams.KafkaStreams) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore)

Aggregations

IOException (java.io.IOException)1 Duration.ofSeconds (java.time.Duration.ofSeconds)1 Arrays.asList (java.util.Arrays.asList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 MockTime (kafka.utils.MockTime)1 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)1 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 Bytes (org.apache.kafka.common.utils.Bytes)1 KafkaStreams (org.apache.kafka.streams.KafkaStreams)1 KeyValue (org.apache.kafka.streams.KeyValue)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 EmbeddedKafkaCluster (org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster)1 IntegrationTestUtils (org.apache.kafka.streams.integration.utils.IntegrationTestUtils)1