[英]using one table to update another table in spark
我有兩個表或dataframes
,我想用一個來更新另一個。 我也知道spark sql不支持update a set a.1= b.1 from b where a.2 = b.2 and a.update < b.update
。 請建議我如何實現此目標,因為這不可能產生火花。
表格1
+------+----+------+
|number|name|update|
+------+--- -------+
| 1| a| 08-01|
| 2| b| 08-02|
+------+----+------+
表2
+------+----+------+
|number|name|update|
+------+--- -------+
| 1| a2| 08-03|
| 3| b| 08-02|
+------+----+------+
我想得到這個:
+------+----+------+
|number|name|update|
+------+--- -------+
| 1| a2| 08-03|
| 2| b| 08-02|
| 3| b| 08-02|
+------+----+------+
還有其他方法可以做到這一點嗎?
使用pyspark
,你可以使用subtract()
找到number
的值table1
不存在於table2
,因此使用unionAll
其中兩個表table1
被過濾下來從丟失的觀測table2
。
diff = (table1.select('number')
.subtract(table2.select('number'))
.rdd.map(lambda x: x[0]).collect())
table2.unionAll(table1[table1.number.isin(diff)]).orderBy('number').show()
+------+----+------+
|number|name|update|
+------+----+------+
| 1| a2| 08-03|
| 2| b| 08-02|
| 3| b| 08-02|
+------+----+------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.