【问题标题】:Writing where query using pyspark on SQL table在 SQL 表上使用 pyspark 编写 where 查询
【发布时间】:2019-06-20 08:56:51
【问题描述】:

我正在使用 pyspark 查询 sql 表。

如果我有一个包含两列 (value, isDelayed) 的 sql 表,其中 "value" 是双精度类型,而 "isDelayed" > 值为 0 或 1。如何使用 pyspark 聚合查询编写查询,当“isDelayed”为 1 时,该查询给出“值”的总和。

我已经尝试过下面给出错误的代码

def __main__(self, data):
    delayedData = data.where(col('isDelayed').cast('int')==='1')
    groupByIsDelayed = delayedData.agg(sum(total))
    return groupByIsDelayed

我来了

“语法错误:无效语法”

在下一行

delayedData = data.where(col('isDelayed').cast('int')==='1')

【问题讨论】:

    标签: sql apache-spark pyspark apache-spark-sql where-clause


    【解决方案1】:

    data.where(col('isDelayed').cast('int')==='1') 替换为data.where(col('isDelayed').cast('int') == 1)

    仅 2 =(python 中的等号运算符是 2 = 符号)
    1 不带引号(因为您比较的是 int,而不是 string)

    data.where("isDelayed=1")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-12
      • 2012-10-21
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多