-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-7301: Fix streams Scala join ambiguous overload #5502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
streams/streams-scala/src/test/scala/org/apache/kafka/streams/scala/KStreamTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright (C) 2018 Joan Goyeau. | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.kafka.streams.scala | ||
|
|
||
| import org.apache.kafka.streams.kstream.JoinWindows | ||
| import org.apache.kafka.streams.scala.Serdes._ | ||
| import org.apache.kafka.streams.scala.ImplicitConversions._ | ||
| import org.apache.kafka.streams.scala.utils.TestDriver | ||
| import org.junit.runner.RunWith | ||
| import org.scalatest.junit.JUnitRunner | ||
| import org.scalatest.{FlatSpec, Matchers} | ||
|
|
||
| @RunWith(classOf[JUnitRunner]) | ||
| class KStreamTest extends FlatSpec with Matchers with TestDriver { | ||
|
|
||
| "selectKey a KStream" should "select a new key" in { | ||
| val builder = new StreamsBuilder() | ||
| val sourceTopic = "source" | ||
| val sinkTopic = "sink" | ||
|
|
||
| builder.stream[String, String](sourceTopic).selectKey((_, value) => value).to(sinkTopic) | ||
|
|
||
| val testDriver = createTestDriver(builder) | ||
|
|
||
| testDriver.pipeRecord(sourceTopic, ("1", "value1")) | ||
| testDriver.readRecord[String, String](sinkTopic).key shouldBe "value1" | ||
|
|
||
| testDriver.pipeRecord(sourceTopic, ("1", "value2")) | ||
| testDriver.readRecord[String, String](sinkTopic).key shouldBe "value2" | ||
|
|
||
| testDriver.close() | ||
| } | ||
|
|
||
| "join 2 KStreams" should "join correctly records" in { | ||
| val builder = new StreamsBuilder() | ||
| val sourceTopic1 = "source1" | ||
| val sourceTopic2 = "source2" | ||
| val sinkTopic = "sink" | ||
|
|
||
| val stream1 = builder.stream[String, String](sourceTopic1) | ||
| val stream2 = builder.stream[String, String](sourceTopic2) | ||
| stream1.join(stream2)((a, b) => s"$a-$b", JoinWindows.of(1000)).to(sinkTopic) | ||
|
|
||
| val testDriver = createTestDriver(builder) | ||
|
|
||
| testDriver.pipeRecord(sourceTopic1, ("1", "topic1value1")) | ||
| testDriver.pipeRecord(sourceTopic2, ("1", "topic2value1")) | ||
| testDriver.readRecord[String, String](sinkTopic).value shouldBe "topic1value1-topic2value1" | ||
|
|
||
| testDriver.readRecord[String, String](sinkTopic) shouldBe null | ||
|
|
||
| testDriver.close() | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
streams/streams-scala/src/test/scala/org/apache/kafka/streams/scala/KTableTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright (C) 2018 Joan Goyeau. | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.kafka.streams.scala | ||
|
|
||
| import org.apache.kafka.streams.kstream.Materialized | ||
| import org.apache.kafka.streams.scala.ImplicitConversions._ | ||
| import org.apache.kafka.streams.scala.Serdes._ | ||
| import org.apache.kafka.streams.scala.utils.TestDriver | ||
| import org.junit.runner.RunWith | ||
| import org.scalatest.junit.JUnitRunner | ||
| import org.scalatest.{FlatSpec, Matchers} | ||
|
|
||
| @RunWith(classOf[JUnitRunner]) | ||
| class KTableTest extends FlatSpec with Matchers with TestDriver { | ||
|
|
||
| "join 2 KTables" should "join correctly records" in { | ||
| val builder = new StreamsBuilder() | ||
| val sourceTopic1 = "source1" | ||
| val sourceTopic2 = "source2" | ||
| val sinkTopic = "sink" | ||
|
|
||
| val table1 = builder.stream[String, String](sourceTopic1).groupBy((key, _) => key).count() | ||
| val table2 = builder.stream[String, String](sourceTopic2).groupBy((key, _) => key).count() | ||
| table1.join(table2)((a, b) => a + b).toStream.to(sinkTopic) | ||
|
|
||
| val testDriver = createTestDriver(builder) | ||
|
|
||
| testDriver.pipeRecord(sourceTopic1, ("1", "topic1value1")) | ||
| testDriver.pipeRecord(sourceTopic2, ("1", "topic2value1")) | ||
| testDriver.readRecord[String, Long](sinkTopic).value shouldBe 2 | ||
|
|
||
| testDriver.readRecord[String, Long](sinkTopic) shouldBe null | ||
|
|
||
| testDriver.close() | ||
| } | ||
|
|
||
| "join 2 KTables with a Materialized" should "join correctly records and state store" in { | ||
| val builder = new StreamsBuilder() | ||
| val sourceTopic1 = "source1" | ||
| val sourceTopic2 = "source2" | ||
| val sinkTopic = "sink" | ||
| val stateStore = "store" | ||
| val materialized = Materialized | ||
| .as[String, Long, ByteArrayKeyValueStore](stateStore) | ||
| .withKeySerde(Serdes.String) | ||
| .withValueSerde(Serdes.Long) | ||
|
|
||
| val table1 = builder.stream[String, String](sourceTopic1).groupBy((key, _) => key).count() | ||
| val table2 = builder.stream[String, String](sourceTopic2).groupBy((key, _) => key).count() | ||
| table1.join(table2, materialized)((a, b) => a + b).toStream.to(sinkTopic) | ||
|
|
||
| val testDriver = createTestDriver(builder) | ||
|
|
||
| testDriver.pipeRecord(sourceTopic1, ("1", "topic1value1")) | ||
| testDriver.pipeRecord(sourceTopic2, ("1", "topic2value1")) | ||
| testDriver.readRecord[String, Long](sinkTopic).value shouldBe 2 | ||
| testDriver.getKeyValueStore[String, Long](stateStore).get("1") shouldBe 2 | ||
|
|
||
| testDriver.readRecord[String, Long](sinkTopic) shouldBe null | ||
|
|
||
| testDriver.close() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
streams/streams-scala/src/test/scala/org/apache/kafka/streams/scala/utils/TestDriver.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright (C) 2018 Joan Goyeau. | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.kafka.streams.scala.utils | ||
|
|
||
| import java.util.{Properties, UUID} | ||
|
|
||
| import org.apache.kafka.clients.producer.ProducerRecord | ||
| import org.apache.kafka.common.serialization.Serde | ||
| import org.apache.kafka.streams.scala.StreamsBuilder | ||
| import org.apache.kafka.streams.test.ConsumerRecordFactory | ||
| import org.apache.kafka.streams.{StreamsConfig, TopologyTestDriver} | ||
| import org.scalatest.Suite | ||
|
|
||
| trait TestDriver { this: Suite => | ||
|
|
||
| def createTestDriver(builder: StreamsBuilder, initialWallClockTimeMs: Long = System.currentTimeMillis()) = { | ||
| val config = new Properties() | ||
| config.put(StreamsConfig.APPLICATION_ID_CONFIG, "test") | ||
| config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234") | ||
| config.put(StreamsConfig.STATE_DIR_CONFIG, s"out/state-store-${UUID.randomUUID()}") | ||
| new TopologyTestDriver(builder.build(), config, initialWallClockTimeMs) | ||
| } | ||
|
|
||
| implicit class TopologyTestDriverOps(inner: TopologyTestDriver) { | ||
| def pipeRecord[K, V](topic: String, record: (K, V), timestampMs: Long = System.currentTimeMillis())( | ||
| implicit serdeKey: Serde[K], | ||
| serdeValue: Serde[V] | ||
| ): Unit = { | ||
| val recordFactory = new ConsumerRecordFactory[K, V](serdeKey.serializer, serdeValue.serializer) | ||
| inner.pipeInput(recordFactory.create(topic, record._1, record._2, timestampMs)) | ||
| } | ||
|
|
||
| def readRecord[K, V](topic: String)(implicit serdeKey: Serde[K], serdeValue: Serde[V]): ProducerRecord[K, V] = | ||
| inner.readOutput(topic, serdeKey.deserializer, serdeValue.deserializer) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it good to put my name here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this is binary compatible.