作为简化示例,我尝试使用以下代码过滤Spark DataFrame:
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
答案 0 :(得分:31)
希望这会对你有所帮助:
val list = List("x","y","t")
xdf.filter($"column".isin(list:_*))
答案 1 :(得分:26)
您必须将单个表达式括起来:
xdf.filter(($"cnt" > 1) || ($"name" isin ("A","B"))).show()