【发布时间】:2019-07-16 04:46:46
【问题描述】:
我有 2 个数据框 df1 和 df2,
df1 的列名称包含 a、b、c 等值
df2 的列 ID 的值类似于 a,b
如果df1 中的Name 列与df2 中的Id 列匹配,那么我们需要匹配状态为0。如果没有匹配,我们需要匹配状态为1。
我知道我可以使用 collect 将 df2 ID 列放入集合中,然后检查 df1 中的 Name 列是否有匹配的条目。
val df1 = Seq(“Rey”, “John”).toDF(“Name”)
val df2 = Seq(“Rey”).toDF(“Id”)
val collect = df2.select("Id").map(r => r.getString(0)).collect.toList
类似的,
val df3 =
df1.withColumn("match_sts",when(df1("Name").isin(collect).then(0).else(1)))
Expected output
+ — — + — -+
|Name|match_sts|
+ — — + — -+
| Rey| 0 |
|John| 1 |
+ — — + — -+
但我不想在这里使用收集。有没有其他可用的方法。
【问题讨论】:
-
可能是一个例子和你自己的尝试?可以使用带有 EXISTS 或外连接的 spark sql...
-
正如 thebluephantom 所说,请分享您的尝试或至少是您的数据框示例。
-
@thebluephantom 分享了这个方法,我不想为 df1 和 df2 做一个联接
-
确实如此,但您需要采取某种行动来获得所谓的副作用。啊哈,但你的收藏有多大?
-
@thebluephantom 应该少于 10 万行
标签: scala apache-spark