我们如何为Spark结构化流媒体中的多个分区构建JSON。以下示例我在这里只粘贴了一个分区。感谢您的帮助。
spark.readStream().format("kafka")
.option("kafka.bootstrap.servers", "****")
.option("subscribePattern", "****.*")
.option("startingOffsets", "{\"Topic01\": {\"0\":250, \"1\": -1}}").load();
答案 0 :(得分: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}}