Eda*_*ame 17 scala dataframe apache-spark
我试图通过使用下面的代码过滤掉null或空字符串的行来创建新的数据框:
val df1 = df.filter(df("fieldA") != "").cache()
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误:
<console>:32: error: overloaded method value filter with alternatives:
(conditionExpr: String)org.apache.spark.sql.DataFrame <and>
(condition: org.apache.spark.sql.Column)org.apache.spark.sql.DataFrame
cannot be applied to (Boolean)
val df1 = df.filter(df("fieldA") != "").cache()
^
Run Code Online (Sandbox Code Playgroud)
有谁知道我在这里错过了什么?谢谢!
Dan*_*ula 26
在Scala中,为了按列比较相等,您应该使用===
和!==
(或=!=
在Spark 2.0+中):
val df1 = df.filter(df("fieldA") !== "").cache()
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用表达式:
val df1 = df.filter("fieldA != ''").cache()
Run Code Online (Sandbox Code Playgroud)
您的错误发生是因为!=
操作符存在于每个Scala对象中,并且它用于比较对象,始终返回Boolean.但是,该filter
函数需要一个Column对象或一个String中的表达式,因此该类中有一个!==
运算符Column
,它返回另一个Column,然后可以按照您想要的方式使用.
要查看列的所有可用操作,列scaladoc非常有用.还有,有functions
包.