【问题标题】:Error while applying to_date() and add_months function in Spark SQL在 Spark SQL 中应用 to_date() 和 add_months 函数时出错
【发布时间】:2021-03-10 17:13:47
【问题描述】:

我有以下蜂巢表。 cycle_month 列的值采用 YYYYMM 格式。

+---------------+--------------+------------+
| column_value  | metric_name  |cycle_month |
+---------------+--------------+------------+
| A37B          | Mean         | 202005     |
| ACCOUNT_ID    | Mean         | 202005     |
| ANB_200       | Mean         | 202005     |
| ANB_201       | Mean         | 202006     |
| AS82_RE       | Mean         | 202006     |
| ATTR001       | Mean         | 202007     |
| ATTR001_RE    | Mean         | 202007     |
| ATTR002       | Mean         | 202008     |
| ATTR002_RE    | Mean         | 202008     |
| ATTR003       | Mean         | 202009     |
| ATTR004       | Mean         | 202009     |
| ATTR005       | Mean         | 202009     |
| ATTR006       | Mean         | 202010     |

我需要编写一个动态查询来获取用户传递的 cycle_month 值和 cycle_month - 4 个月之间的值。

Spark SQL 查询:

select column_name, metric_name from table where cycle_month between add_months(to_date(202010,'YYYYMM'),-4) and 202010  

遇到错误

[错误 10015]:第 1:323 行参数长度不匹配“YYYYMM”:to_date() 需要 1 个参数,得到 2 个(状态=21000,代码=10015)

预期输出:

+---------------+--------------+------------+
| column_value  | metric_name  |cycle_month |
+---------------+--------------+------------+
| ANB_201       | Mean         | 202006     |
| AS82_RE       | Mean         | 202006     |
| ATTR001       | Mean         | 202007     |
| ATTR001_RE    | Mean         | 202007     |
| ATTR002       | Mean         | 202008     |
| ATTR002_RE    | Mean         | 202008     |
| ATTR003       | Mean         | 202009     |
| ATTR004       | Mean         | 202009     |
| ATTR005       | Mean         | 202009     |
| ATTR006       | Mean         | 202010     |

【问题讨论】:

    标签: sql apache-spark hive apache-spark-sql


    【解决方案1】:

    Y 不是正确的年份格式;它应该是y。你应该使用yyyyMM。详情请见https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html

    SELECT 
        column_name, metric_name, cycle_month
    FROM 
        table
    WHERE 
        to_date(cycle_month, 'yyyyMM') BETWEEN 
            add_months(to_date(202010, 'yyyyMM'), -4)
                AND 
            to_date(202010, 'yyyyMM')
    

    【讨论】:

      【解决方案2】:

      to_date 函数需要字符串作为输入并返回 date 以将日期更改为 YYYYMM 使用 date_format 函数最终将 date 转换为 int

      Try with this query

      select column_name, metric_name from table where cycle_month between int(date_format(add_months(to_date('202010','YYYYMM'),-4),'YYYYMM')) and 202010
      

      UPDATE:

      sql("select int(date_format(add_months(to_date('202010','YYYYMM'),-4),'YYYYMM'))").show()
      #+------------------------------------------------------------------------------------------------+
      #|CAST(date_format(CAST(add_months(to_date('202010', 'YYYYMM'), -4) AS TIMESTAMP), YYYYMM) AS INT)|
      #+------------------------------------------------------------------------------------------------+
      #|                                                                                          201908|
      #+------------------------------------------------------------------------------------------------+
      

      【讨论】:

      • 这仍然给出同样的错误,因为日期格式错误 - 它应该是 yyyyMM,而不是 YYYYMM
      • @mck,仅供参考如果你指定YYYYMM Y 是week of the year,它不会抛出错误。有问题的作者提到了Y,我们不能盲目地假设y,并且请在发表评论/否决之前执行命令 (查看我的答案更新部分) .
      • 更新部分的命令在 Spark 3.0.0 中出现错误:org.apache.spark.SparkUpgradeException: You may get a different result due to the upgrading of Spark 3.0: Fail to recognize 'YYYYMM' pattern in the DateTimeFormatter. 1) You can set spark.sql.legacy.timeParserPolicy to LEGACY to restore the behavior before Spark 3.0. 2) You can form a valid datetime pattern with the guide from https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
      • 另外我认为在 99.99% 的情况下,y 是用户想要的,而不是一年中的一周。
      • 两个答案都对我有用 :) 我使用的是 Spark 2.4.0
      猜你喜欢
      • 2021-01-23
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多