【问题标题】:spark streaming rdd to json output without deprecation火花流 rdd 到 json 输出而不弃用
【发布时间】:2018-09-19 08:21:38
【问题描述】:

下面的代码 sn-p:df5 数据帧正确打印 json,但 isStream 为假,这在 spark 2.2.0 中已被弃用,所以我在最后两行代码中尝试了另一种方法,但它无法正确读取 json,有什么建议吗??

val unionStreams = ssc.union(kinesisStreams)
unionStreams.foreachRDD ((rdd: RDD[Array[Byte]], time: Time) => {
  val rowRDD = rdd.map(jstr => new String(jstr))
  val schema = StructType(StructField("clientTime",StringType,nullable= true) :: StructField("clientIPAddress",  StringType,nullable = true) :: Nil)

  val df5 = sqlContext.read.schema(schema).json(rowRDD)
  println(df5.isStreaming)

  val df6 = spark.readStream.schema(schema).json(rdd.toString())
  println(df6.isStreaming) )}

【问题讨论】:

    标签: json apache-spark spark-dataframe spark-streaming rdd


    【解决方案1】:

    使用Dataset[String]:

    import sqlContext.implicits._
    
    sqlContext.read.schema(schema).json(rowRDD.toDS)
    

    【讨论】:

      【解决方案2】:

      试试这个,不会有警告的。

      val df5 = sqlContext.read.schema(schema).json(spark.createDataset(rowRDD)(Encoders.STRING))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 2019-12-14
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        相关资源
        最近更新 更多