How to implement EXISTS condition as like SQL in s

2020-01-20 15:45发布

I am curious to know, how can i implement sql like exists clause in spark Dataframe way.

2条回答
神经病院院长
2楼-- · 2020-01-20 16:19

LEFT SEMI JOIN is equivalent to the EXISTS function in Spark.

val cityDF= Seq(("Delhi","India"),("Kolkata","India"),("Mumbai","India"),("Nairobi","Kenya"),("Colombo","Srilanka")).toDF("City","Country")

df1

val CodeDF= Seq(("011","Delhi"),("022","Mumbai"),("033","Kolkata"),("044","Chennai")).toDF("Code","City")

df2

val finalDF= cityDF.join(CodeDF, cityDF("City") === CodeDF("City"), "left_semi")

df3

查看更多
Melony?
3楼-- · 2020-01-20 16:23

If the data to be compared is small like a broadcasted list then you can use -

df.filter(col("columnName").isin(list...) === true)

查看更多
登录 后发表回答