scala 如何根据列值是否在 Spark DataFrame 中的一组字符串中过滤行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31396228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:20:22 来源:igfitidea点击:
How do I filter rows based on whether a column value is in a Set of Strings in a Spark DataFrame
提问by zzztimbo
Is there a more elegant way of filtering based on values in a Set of String?
是否有更优雅的基于一组字符串中的值进行过滤的方法?
def myFilter(actions: Set[String], myDF: DataFrame): DataFrame = {
val containsAction = udf((action: String) => {
actions.contains(action)
})
myDF.filter(containsAction('action))
}
In SQL you can do
在 SQL 中你可以做
select * from myTable where action in ('action1', 'action2', 'action3')
回答by Justin Pihony
How about this:
这个怎么样:
myDF.filter("action in (1,2)")
OR
或者
import org.apache.spark.sql.functions.lit
myDF.where($"action".in(Seq(1,2).map(lit(_)):_*))
OR
或者
import org.apache.spark.sql.functions.lit
myDF.where($"action".in(Seq(lit(1),lit(2)):_*))
Additional support will be added to make this cleaner in 1.5

