按值过滤数据帧不存在于其他数据帧的列中

Cho*_*ops 3 scala apache-spark apache-spark-sql spark-dataframe

用这一个敲我的脑袋,我怀疑答案很简单.给定两个数据帧,我想过滤第一列,其中一列中的值不存在于另一个数据帧的列中.

我想这样做而不需要使用完整的Spark SQL,所以只使用DataFrame.filter,或Column.contains或"isin"关键字,或其中一个连接方法.

val df1 = Seq(("Hampstead", "London"), 
              ("Spui", "Amsterdam"), 
              ("Chittagong", "Chennai")).toDF("location", "city")
val df2 = Seq(("London"),("Amsterdam"), ("New York")).toDF("cities")

val res = df1.filter(df2("cities").contains("city") === false)
// doesn't work, nor do the 20 other variants I have tried
Run Code Online (Sandbox Code Playgroud)

有人有任何想法吗?

Cho*_*ops 6

我发现我可以使用更简单的方法解决这个问题 - 似乎反连接可以作为连接方法的参数,但Spark Scaladoc没有描述它:

import org.apache.spark.sql.functions._

val df1 = Seq(("Hampstead", "London"), 
              ("Spui", "Amsterdam"), 
              ("Chittagong", "Chennai")).toDF("location", "city")
val df2 = Seq(("London"),("Amsterdam"), ("New York")).toDF("cities")

df1.join(df2, df1("city") === df2("cities"), "leftanti").show
Run Code Online (Sandbox Code Playgroud)

结果是:

+----------+-------+ 
|  location|   city| 
+----------+-------+ 
|Chittagong|Chennai| 
+----------+-------+  
Run Code Online (Sandbox Code Playgroud)

PS感谢指向重复的指针 - 正确标记为这样


Alb*_*nto 3

如果您尝试DataFrame使用另一个过滤,您应该使用join(或其任何变体)。如果您需要使用List适合您的 master 和workers 的或任何数据结构来过滤它,您可以广播它,然后在filterorwhere方法中引用它。

例如我会做类似的事情:

import org.apache.spark.sql.functions._

val df1 = Seq(("Hampstead", "London"), 
              ("Spui", "Amsterdam"), 
              ("Chittagong", "Chennai")).toDF("location", "city")
val df2 = Seq(("London"),("Amsterdam"), ("New York")).toDF("cities")

df2.join(df1, joinExprs=df1("city") === df2("cities"), joinType="full_outer")
   .select("city", "cities")
   .where(isnull($"cities"))
   .drop("cities").show()
Run Code Online (Sandbox Code Playgroud)