【发布时间】:2021-09-22 22:25:16
【问题描述】:
我有两个数据框。我想使用来自col1 的df2 的值替换col1 的df1 中的值为空的值。请记住,df1 与df2 类似,可以有 > 10^6 行,并且df1 有一些额外的列,这些列与df2 的一些额外列不同。
我知道如何加入,但我不知道如何在 Spark 中使用 Scala 进行某种条件加入。
df1
name | col1 | col2 | col3
----------------------------
foo | 0.1 | ...
bar | null |
hello | 0.6 |
foobar | null |
df2
name | col1 | col7
--------------------
lorem | 0.1 |
bar | 0.52 |
foobar | 0.47 |
编辑:
这是我目前的解决方案:
df1.select("name", "col2", "col3").join(df2, (df1("name") === df2("name")), "left").select(df1("name"), col("col1"))
EDIT2:
val df1 = Seq(
("foo", Seq(0.1), 10, "a"),
("bar", Seq(), 20, "b"),
("hello", Seq(0.1), 30, "c"),
("foobar", Seq(), 40, "d")
).toDF("name", "col1", "col2", "col3")
val df2 = Seq(
("lorem", Seq(0.1), "x"),
("bar", Seq(0.52), "y"),
("foobar", Seq(0.47), "z")
).toDF("name", "col1", "col7")
display(df1.
join(df2, Seq("name"), "left_outer").
select(df1("name"), coalesce(df1("col1"), df2("col1")).as("col1")))
返回:
name | col1
bar | []
foo | [0.1]
foobar | []
hello | [0.1]
【问题讨论】:
标签: scala apache-spark join conditional-statements