有没有一种更优雅的方法来基于字符串集中的值进行过滤?

def myFilter(actions: Set[String], myDF: DataFrame): DataFrame = {
  val containsAction = udf((action: String) => {
    actions.contains(action)
  })

  myDF.filter(containsAction('action))
}


在SQL中你可以做

select * from myTable where action in ('action1', 'action2', 'action3')

最佳答案

这个怎么样:

myDF.filter("action in (1,2)")


要么

import org.apache.spark.sql.functions.lit
myDF.where($"action".in(Seq(1,2).map(lit(_)):_*))


要么

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

09-26 06:39