我需要比较两个DataFrame。其中之一是静态的,另一种是流式的。 示例静态DataFrame如下所示:
id, value
2786, 5
7252, 3
2525, 4
8038, 1
流式传输DataFrame的示例如下所示:
id, value
2786, 9
7252, 8
2525, 7
结果DataFrame应该如下所示:
id, value
8038, 1
价值一点都不重要。我只需要查找该迷你批处理,就没有指定ID为8038的值。我试图为此使用joins和减去()函数,但问题是流-静态联接不支持我需要的那种联接,当静态DataFrame在左侧时,减去不起作用。例如,这些表达式将返回错误:
staticDF.subtract(streamingDF)
staticDF.join(streamingDF, staticDF.id = streamingDF.id, "left_anti")
有什么方法可以获取staticDF中存在的id,但不能获取Spark结构化流中的streamDF中的id?
答案 0 :(得分:0)
您可以使用foreachBatch接收器,然后将左反连接用于静态数据帧和微批处理。
streamingDf.writeStream.foreachBatch { (batchDF: DataFrame, batchId: Long) =>
println("------------------------")
println("Batch "+batchId+ " data")
println("Total Records " + batchDF.count())
println("------------------------")
staticDf.join(batchDF, staticDf("id") === batchDF("id"),"left_anti")
.select(staticDf("*")).show()
//You can also write your output using any writer
//e.g. df.write.format("csv").save("src/test/resources")
}.start()
输入:
static df
+----+-----+
| id|value|
+----+-----+
|2786| 5|
|7252| 3|
|2525| 4|
|8038| 1|
+----+-----+
streaming batch 0
2786,9
7252,8
2525,7
streaming batch 1
2786,9
7252,8
输出:
------------------------
Batch 0 data
Total Records 3
------------------------
+----+-----+
| id|value|
+----+-----+
|8038| 1|
+----+-----+
------------------------
Batch 1 data
Total Records 2
------------------------
+----+-----+
| id|value|
+----+-----+
|2525| 4|
|8038| 1|
+----+-----+