通过检查值是否在列表中以及其他条件来过滤 Spark DataFrame [英] Filter Spark DataFrame by checking if value is in a list, with other criteria

查看:32
本文介绍了通过检查值是否在列表中以及其他条件来过滤 Spark DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个简化的例子,我尝试使用以下代码过滤 Spark DataFrame:

As a simplified example, I tried to filter a Spark DataFrame with following code:

val xdf = sqlContext.createDataFrame(Seq(
  ("A", 1), ("B", 2), ("C", 3)
)).toDF("name", "cnt")
xdf.filter($"cnt" >1 || $"name" isin ("A","B")).show()

然后报错:

org.apache.spark.sql.AnalysisException: cannot resolve '((cnt > 1) || name)' due to data type mismatch: differing types in '((cnt > 1) || name)' (boolean and string).;

正确的做法是什么?在我看来,它在 name 列之后停止阅读.这是解析器中的错误吗?我使用的是 Spark 1.5.1

What's the right way to do it? It seems to me that it stops reading after name column. Is it a bug in the parser? I'm using Spark 1.5.1

推荐答案

您必须将单个表达式括起来:

You have to parenthesize individual expressions:

xdf.filter(($"cnt" > 1) || ($"name" isin ("A","B"))).show()

这篇关于通过检查值是否在列表中以及其他条件来过滤 Spark DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆