简体   繁体   English

使用带有列名列表的 Spark DataFrame 过滤器

[英]Using Spark DataFrame filter with a List of column names

I have to filter non-null column values in a Spark DataFrame using a List[String] :我必须使用List[String]过滤 Spark DataFrame 中的非空列值:

val keyList = List("columnA", "columnB", "columnC", "columnD", ...)

For a single column named key , the syntax should be:对于名为key的单个列,语法应为:

val nonNullDf = df.filter(col("key").isNotNull)

My question is how to use the keyList into the previous filter?我的问题是如何使用keyList进入前一个过滤器?

You can generate a filter by doing a map-reduce on keyList .您可以通过在keyList上执行 map-reduce 来生成过滤器。

Use and if you want to keep the rows where all columns are not null, or use or if you want to keep the rows where any column is not null.如果要保留所有列都不是 null 的行,请使用andor如果要保留任何列不是 null 的行,请使用或。

val nonNullDf = df.filter(keyList.map(col(_).isNotNull).reduce(_ and _))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM