apache-spark - PySpark 中的 DataFrame#filter 找不到现有列

标签 apache-spark pyspark

我正在使用 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/

相关文章:

scala - Spark消费者不读取Kafka生产者消息Scala

scala - VectorAssembler 不支持 StringType 类型的 scala spark 转换

python - 将符合上次修改窗口的 S3 文件读入 DataFrame

python - pyspark:从现有列创建 MapType 列

python - 将列表的 koalas 列拆分为多列

postgresql - 从 Spark/pyspark 连接到 PostgreSQL

python - 替换 PySpark 中的字符串

scala - Spark Dataframe - 如何访问 json 结构

python - pyspark 中的函数 input()

python - 引用 Pyspark DataFrame 中的列