我正在使用 PySpark 在我的 Hive 表上执行 SparkSQL。
records = sqlContext.sql("SELECT * FROM my_table")
它检索表的内容。
当我使用过滤器参数作为字符串时,它工作正常:
records.filter("field_i = 3")
但是,当我尝试使用过滤方法时,如记录的 here
records.filter(records.field_i == 3)
我遇到了这个错误
py4j.protocol.Py4JJavaError: An error occurred while calling o19.filter.
: org.apache.spark.sql.AnalysisException: resolved attributes field_i missing from field_1,field_2,...,field_i,...field_n
尽管这个 field_i 列显然存在于 DataFrame 对象中。
我更喜欢使用第二种方式,因为我需要使用Python函数来执行记录和字段操作。
我在 Cloudera Quickstart CDH-5.4.0 和 Python 2.6 中使用 Spark 1.3.0。
最佳答案
来自Spark DataFrame documentation
In Python it’s possible to access a DataFrame’s columns either by attribute (df.age) or by indexing (df['age']). While the former is convenient for interactive data exploration, users are highly encouraged to use the latter form, which is future proof and won’t break with column names that are also attributes on the DataFrame class.
看来您的字段名称可以是保留字,请尝试使用:
records.filter(records['field_i'] == 3)
关于apache-spark - PySpark 中的 DataFrame#filter 找不到现有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318468/