Structed Streaming抽取json数据的某一字段

	val resultTable: DataFrame = inputTable
  		.select(
				get_json_object($"value","$.userID").as("userID") ,
				get_json_object($"value","$.eventType").as("eventType")
			)
  		.dropDuplicates("userID","eventType")
  		.groupBy($"userID",$"eventType")
  		.count()
		
		// 3. 设置Streaming应用输出及启动
		val query: StreamingQuery = resultTable.writeStream
			.outputMode(OutputMode.Complete())
			.format("console")
			.option("numRows", "100")
			.option("truncate", "false")
			.start()

将数据提议封装成Json数据格式

  // 3. 对流式数据进行提取字段
    val schema: StructType = new StructType()
      .add("device", StringType, nullable = true)
      .add("deviceType", StringType, nullable = true)
      .add("signal", DoubleType, nullable = true)
      .add("time", LongType, nullable = true)
    val etlStreamDF = iotStreamDF
      .selectExpr("CAST (value as STRING)")
      .select(
        from_json($"value", schema).as("device")
      )
      .select($"device.*")

你可能感兴趣的:(笔记,json)