从Spark Dataframe删除空白字段

时间:2020-10-26 13:16:59

标签: apache-spark apache-kafka apache-spark-sql spark-structured-streaming

我使用Spark结构化流媒体来消费一个kafka主题,该主题具有多种消息类型(每种类型的模式不同)。我定义了一个架构,其中包含用于不同类型消息的所有字段。
如何从每一行的数据框中过滤空字段,或者如何从具有动态架构的kafka中读取数据框。

    val inputDS = spark
      .readStream
      .format("kafka")
      .option("kafka.bootstrap.servers", "localhost:9092")
      .option("subscribe", "overview")
      .load()

    val schemaa: StructType = StructType(
      Array(
       StructField("title", StringType, true),
       StructField("url", StringType, true),
       StructField("content", StringType, true),
       StructField("collect_time", StringType, true),
       StructField("time", StringType, true),
       StructField("user_head", StringType, true),
       StructField("image", StringType, true)
      )
    )

    inputDS.withColumn("value", from_json($"value".cast(StringType), schemaa))
      //.filter()     // todo filter empty field
      .writeStream
      .format("console")
      .start()
      .awaitTermination()

0 个答案:

没有答案