0

我们如何在 Spark 结构化流中为多个分区构建 JSON。我在此处粘贴的以下示例仅用于一个分区。感谢你的帮助。

spark.readStream().format("kafka")
        .option("kafka.bootstrap.servers", "****")
        .option("subscribePattern", "****.*")
        .option("startingOffsets", "{\"Topic01\": {\"0\":250, \"1\": -1}}").load();
4

1 回答 1

0

您可以使用您最喜欢的 JSON 库来创建字符串。下面是一个json4s的例子:

scala> import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization

scala> import org.json4s.NoTypeHints
import org.json4s.NoTypeHints

scala> implicit val formats = Serialization.formats(NoTypeHints)
formats: org.json4s.Formats{val dateFormat: org.json4s.DateFormat; val typeHints: org.json4s.TypeHints} = org.json4s.Serialization$$anon$1@7c206b14

scala> val offsets = Map("topic1" -> Map("0" -> 1, "1" -> -1, "2" -> -2), "topic2" -> Map("0" -> 0, "1" -> -1))
offsets: scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,Int]] = Map(topic1 -> Map(0 -> 1, 1 -> -1, 2 -> -2), topic2 -> Map(0 -> 0, 1 -> -1))

scala> Serialization.write(offsets)
res0: String = {"topic1":{"0":1,"1":-1,"2":-2},"topic2":{"0":0,"1":-1}} 
于 2017-05-23T20:29:20.137 回答