我正在做一些作业,其中我们有两个具有相同/不同列的表。如果 的记录table A
有一些列值,null
那么它必须更新为值 in table B
,反之亦然。
table A
id | code | type
1 | null | A
2 | null | null
3 | 123 | C
table B
id | code | type
1 | 456 | A
2 | 789 | A1
3 | null | C
到目前为止我所做的工作
Dataset<Row> df1 = spark.read().format("csv").option("header", "true").load("C:\\Users\\System2\\Videos\\1199_data\\d1_1.csv");
Dataset<Row> df2 = spark.read().format("csv").option("header", "true").load("C:\\Users\\System2\\Videos\\1199_data\\d2_1.csv");
df1
.as("a").join(df2.as("b"))
.where("a.id== b.id")
.withColumn("a.code",
functions.when(
df1.col("code").isNull(),
df2.col("code") )
).show();
所需输出
table C
id | code | type
1 | 456 | A
2 | 789 | A1
3 | 123 | C
你可以使用coalesce功能吗?
df1.join(df2, "id")
.select(df1("id"),
coalesce(df1("code"),
df2("code")).as("code"),
coalesce(df1("type"),
df2("type")).as("type"))
和输出:
+---+----+----+
| id|code|type|
+---+----+----+
| 1| 456| A|
| 2| 789| A1|
| 3| 123| C|
+---+----+----+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句