HeartSaVioR commented on a change in pull request #26153:
[SPARK-29500][SQL][SS] Support partition column when writing to Kafka
URL: https://github.com/apache/spark/pull/26153#discussion_r337408634
##########
File path:
external/kafka-0-10-sql/src/test/scala/org/apache/spark/sql/kafka010/KafkaSinkSuite.scala
##########
@@ -418,6 +435,93 @@ abstract class KafkaSinkBatchSuiteBase extends
KafkaSinkSuiteBase {
)
}
+ def writeToKafka(df: DataFrame, topic: String, options: Map[String, String]
= Map.empty): Unit = {
+ df
+ .write
+ .format("kafka")
+ .option("kafka.bootstrap.servers", testUtils.brokerAddress)
+ .option("topic", topic)
+ .options(options)
+ .mode("append")
+ .save()
+ }
+
+ def partitionsInTopic(topic: String): Set[Int] = {
+ createKafkaReader(topic)
+ .select("partition")
+ .map(_.getInt(0))
+ .collect()
+ .toSet
+ }
+
+ test("batch - partition column sets partition in kafka writes") {
+ val fixedKey = "fixed_key"
+ val nrPartitions = 4
+
+ val topic = newTopic()
+ testUtils.createTopic(topic, nrPartitions)
+
+ // default Kafka partitioner calculates partitions deterministically based
on the key
+ val df = (0 until 5)
+ .map(n => (topic, fixedKey, s"$n"))
+ .toDF("topic", "key", "value")
+ writeToKafka(df, topic)
+ val partitionsForFixedKey = partitionsInTopic(topic)
+ assert(partitionsForFixedKey.size == 1)
+ val keyPartition = partitionsForFixedKey.head
+
+ val topic2 = newTopic()
+ testUtils.createTopic(topic2, nrPartitions)
+
+ val differentPartition = (0 until nrPartitions).find(p => p !=
keyPartition).get
+ val df2 = df.withColumn("partition", lit(differentPartition))
+ writeToKafka(df2, topic2)
+ val partitions = partitionsInTopic(topic2)
+ assert(partitions.size == 1)
+ assert(partitions.head != keyPartition)
+ }
+
+ test("batch - partition column and partitioner priorities") {
Review comment:
Looks like this test covers everything. Great.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]