【问题标题】:Structured Streaming to Save JSON to HDFS将 JSON 保存到 HDFS 的结构化流
【发布时间】:2019-12-05 11:57:42
【问题描述】:

我的结构化 Spark Streaming 程序是从 Kafka 读取 JSON 数据 并以 JSON 格式写入 HDFS。我能够将 JSON 保存到 HDFS 但 它将 JSON 字符串保存为:

 "jsontostructs(CAST(value AS STRING))"
key as below: {"jsontostructs(CAST(value AS STRING))":{"age":42,"name":"John"}}.

如何只保存

{"age":42,"name":"John"}?




StructType schema = kafkaPrimerRow.schema();

//Read json from kafka. JSON is: {"age":42,"name":"John"}
Dataset<Row> df = spark
                    .readStream()
                    .format("kafka")
                    .option("kafka.bootstrap.servers", input_bootstrap_server)
                    .option("subscribe", topics[0])
                    .load();




    //Save Stream to HDFS
    StreamingQuery ds = df             
.select(functions.from_json(col("value").cast(DataTypes.StringType),schema)) 
.writeStream()

.format("json")
.outputMode(OutputMode.Append())
.option("path", destPath)
.option("checkpointLocation", checkpoint)
.start();

【问题讨论】:

    标签: java apache-spark apache-kafka hdfs spark-streaming-kafka


    【解决方案1】:

    下面的 .select("data.*") 成功了。

    StreamingQuery ds = df
                            .select(functions.from_json(col("value").cast(DataTypes.StringType),schema).as("data"))
                            .select("data.*")
                            .writeStream()
                            .format("json")
                            .outputMode(OutputMode.Append())
                            .option("path", destPath)
                            .option("checkpointLocation", checkpoint)
                            .start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2018-03-28
      • 2019-08-13
      • 2017-12-06
      相关资源
      最近更新 更多