简体   繁体   English

如何根据列值是否在Spark DataFrame的一组字符串中过滤行

[英]How do I filter rows based on whether a column value is in a Set of Strings in a Spark DataFrame

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')

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 将添加额外的支持,以使1.5更清洁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spark Notebook:如何根据列值(其中每个列单元格都是字符串数组)过滤行? - Spark Notebook: How can I filter rows based on a column value where each column cell is an array of strings? 如何根据 map 的列值过滤火花 dataframe 条目 - How to filter spark dataframe entries based on a column value which is a map 根据列的最大值过滤火花数据框 - filter spark dataframe based on maximum value of a column 如何基于Spark中的另一个数据框修改数据框行? - How do I modify dataframe rows based on another dataframe in Spark? 如何将Spark数据帧中的WrappedArray列转换为Strings? - How do I convert a WrappedArray column in spark dataframe to Strings? 如何根据列包含的值过滤spark Dataframe? - How can I filter spark Dataframe according to the value that column contains? 如何使用字符串数组过滤 Spark 数据框中的列? - How to filter a column in Spark dataframe using a Array of strings? 如何按日期过滤 Spark dataframe? - How do I filter a Spark dataframe by date? 如何根据另一列的值填充 Spark DataFrame 列? - How to populate a Spark DataFrame column based on another column's value? 我将如何通过 Scala Spark 中列的百分位值过滤数据框 - How would I filter a dataframe by a column's percentile value in Scala Spark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM