Skip to content

Commit fb65aaa

Browse files
committed
Merge branch 'spark-3.1' into spark-3.0
# Conflicts: # kotlin-spark-api/3.0/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaHelper.kt # kotlin-spark-api/3.0/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaStreamingTest.kt # pom.xml
2 parents 187bdde + c6cc4b2 commit fb65aaa

File tree

7 files changed

+85
-213
lines changed

7 files changed

+85
-213
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
2626
restore-keys: ${{ runner.os }}-m2
2727
- name: Build with Maven
28-
run: ./mvnw -B package --file pom.xml -Pscala-2.12 -Dkotest.tags="!Kafka"
28+
run: ./mvnw -B package --file pom.xml -Pscala-2.12
2929
# qodana:
3030
# runs-on: ubuntu-latest
3131
# steps:

.github/workflows/publish_dev_version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
2525
restore-keys: ${{ runner.os }}-m2
2626
- name: Deploy to GH Packages
27-
run: ./mvnw --batch-mode deploy -Dkotest.tags="!Kafka"
27+
run: ./mvnw --batch-mode deploy
2828
env:
2929
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3030

kotlin-spark-api/3.0/pom_2.12.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
<version>${kotest-extensions-allure.version}</version>
7474
<scope>test</scope>
7575
</dependency>
76+
<dependency>
77+
<groupId>io.kotest.extensions</groupId>
78+
<artifactId>kotest-extensions-testcontainers</artifactId>
79+
<version>${kotest-extensions-testcontainers.version}</version>
80+
<scope>test</scope>
81+
</dependency>
7682
<dependency>
7783
<groupId>io.github.embeddedkafka</groupId>
7884
<artifactId>embedded-kafka_${scala.compat.version}</artifactId>

kotlin-spark-api/3.0/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaHelper.kt

Lines changed: 0 additions & 148 deletions
This file was deleted.

kotlin-spark-api/3.0/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaStreamingTest.kt

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,8 +20,13 @@
2020
package org.jetbrains.kotlinx.spark.api
2121

2222
import io.kotest.core.Tag
23-
import io.kotest.core.spec.style.ShouldSpec
24-
import io.kotest.matchers.collections.shouldBeIn
23+
import io.kotest.core.extensions.install
24+
import io.kotest.core.spec.style.FunSpec
25+
import io.kotest.extensions.testcontainers.TestContainerExtension
26+
import io.kotest.extensions.testcontainers.kafka.createStringStringConsumer
27+
import io.kotest.extensions.testcontainers.kafka.createStringStringProducer
28+
import io.kotest.matchers.collections.shouldContain
29+
import io.kotest.matchers.collections.shouldContainAll
2530
import org.apache.kafka.clients.consumer.ConsumerConfig
2631
import org.apache.kafka.clients.consumer.ConsumerRecord
2732
import org.apache.kafka.clients.producer.ProducerRecord
@@ -32,85 +37,94 @@ import org.apache.spark.streaming.kafka010.ConsumerStrategies
3237
import org.apache.spark.streaming.kafka010.KafkaUtils
3338
import org.apache.spark.streaming.kafka010.LocationStrategies
3439
import org.jetbrains.kotlinx.spark.api.tuples.*
40+
import org.testcontainers.containers.KafkaContainer
41+
import org.testcontainers.utility.DockerImageName
42+
import scala.Tuple3
3543
import java.io.Serializable
44+
import java.time.Duration
3645

3746
object Kafka : Tag()
3847

39-
class KafkaStreamingTest : ShouldSpec({
48+
class KafkaStreamingTest : FunSpec() {
49+
init {
4050

41-
// making sure it can be skipped on Github actions since it times out
42-
tags(Kafka)
51+
tags(Kafka)
4352

44-
xcontext("kafka") {
45-
val port = 9092
46-
val broker = "localhost:$port"
47-
val topic1 = "test1"
48-
val topic2 = "test2"
49-
val kafkaListener = EmbeddedKafkaListener(port)
50-
listener(kafkaListener)
53+
val kafka = install(
54+
TestContainerExtension(KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1")))
55+
) {
56+
withEmbeddedZookeeper()
57+
withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "true")
58+
}
59+
println(kafka.bootstrapServers)
60+
test("Streaming should support kafka") {
61+
val topic1 = "test1"
62+
val topic2 = "test2"
5163

52-
should("support kafka streams") {
53-
val producer = kafkaListener.stringStringProducer()
54-
producer.send(ProducerRecord(topic1, "Hello this is a test test test"))
55-
producer.send(ProducerRecord(topic2, "This is also also a test test something"))
56-
producer.close()
64+
val resultLists = mapOf(
65+
topic1 to listOf(
66+
"Hello" X 1,
67+
"this" X 1,
68+
"is" X 1,
69+
"a" X 1,
70+
"test" X 3,
71+
),
72+
topic2 to listOf(
73+
"This" X 1,
74+
"is" X 1,
75+
"also" X 2,
76+
"a" X 1,
77+
"test" X 2,
78+
"something" X 1,
79+
)
80+
)
81+
val data = arrayListOf<List<Tuple3<String, String, Int>>>()
5782

5883
withSparkStreaming(
59-
batchDuration = Durations.seconds(2),
84+
batchDuration = Durations.milliseconds(1000),
6085
appName = "KotlinDirectKafkaWordCount",
61-
timeout = 1000L,
86+
timeout = 10_000L,
87+
master = "local"
6288
) {
6389

90+
setRunAfterStart {
91+
val producer = autoClose(kafka.createStringStringProducer())
92+
producer.send(ProducerRecord(topic1, "Hello this is a test test test"))
93+
producer.send(ProducerRecord(topic2, "This is also also a test test something"))
94+
}
95+
6496
val kafkaParams: Map<String, Serializable> = mapOf(
65-
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to broker,
97+
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to "${kafka.host}:${kafka.getMappedPort(KafkaContainer.KAFKA_PORT)}",
6698
ConsumerConfig.GROUP_ID_CONFIG to "consumer-group",
6799
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
68100
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
69101
)
70-
71102
// Create direct kafka stream with brokers and topics
72103
val messages: JavaInputDStream<ConsumerRecord<String, String>> = KafkaUtils.createDirectStream(
73104
ssc,
74-
LocationStrategies.PreferConsistent(),
105+
LocationStrategies.PreferBrokers(),
75106
ConsumerStrategies.Subscribe(setOf(topic1, topic2), kafkaParams),
76107
)
77108

78109
// Get the lines, split them into words, count the words and print
79-
val lines = messages.map { it.topic() X it.value() }
80-
val words = lines.flatMapValues { it.split(" ").iterator() }
81110

82-
val wordCounts = words
111+
val wordCounts = messages
112+
.map { it.topic() X it.value() }
113+
.flatMapValues { it.split(" ").iterator() }
83114
.map { t(it, 1) }
84115
.reduceByKey { a: Int, b: Int -> a + b }
85116
.map { (tup, counter) -> tup + counter }
86117

87-
val resultLists = mapOf(
88-
topic1 to listOf(
89-
"Hello" X 1,
90-
"this" X 1,
91-
"is" X 1,
92-
"a" X 1,
93-
"test" X 3,
94-
),
95-
topic2 to listOf(
96-
"This" X 1,
97-
"is" X 1,
98-
"also" X 2,
99-
"a" X 1,
100-
"test" X 2,
101-
"something" X 1,
102-
)
103-
)
104118

105119
wordCounts.foreachRDD { rdd, _ ->
106-
rdd.foreach { (topic, word, count) ->
107-
t(word, count).shouldBeIn(collection = resultLists[topic]!!)
108-
}
120+
data.add(rdd.collect())
109121
}
122+
}
110123

111-
wordCounts.print()
124+
val resultList = resultLists.flatMap { (topic, tuples) ->
125+
tuples.map { it.prependedBy(topic) }
112126
}
127+
data.flatten() shouldContainAll resultList
113128
}
114-
115129
}
116-
})
130+
}

kotlin-spark-api/3.0/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ProjectConfig.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,7 +24,6 @@ import io.kotest.extensions.allure.AllureTestReporter
2424

2525
@Suppress("unused")
2626
object ProjectConfig : AbstractProjectConfig() {
27-
override fun listeners() = super.listeners() + AllureTestReporter(true)
28-
2927
override fun extensions() = super.extensions() + AllureTestReporter(true)
28+
3029
}

0 commit comments

Comments
 (0)