gpt4 book ai didi

apache-spark - 无法比较 Spark SQL 查询中的日期

转载 作者:行者123 更新时间:2023-12-05 01:26:56 24 4
gpt4 key购买 nike

使用适用于 MySQL 的 PySpark 和 JDBC 驱动程序我无法查询日期类型的列。抛出 java.lang.ClassCastException。

sqlContext = SQLContext(sc)
df = sqlContext.load(source="jdbc", url=url, dbtable="reports")
sqlContext.registerDataFrameAsTable(df, "reports")
df.printSchema()
# root
# |-- id: integer (nullable = false)
# |-- day: date (nullable = false)
query = sqlContext.sql("select * from reports where day > '2015-05-01'")
query.collect() # ... most recent failure: ... java.lang.ClassCastException

将日期列的类型更改为时间戳可以解决问题,但我必须保留原始架构。

最佳答案

查看 relevant unit tests在 Spark 源代码中,您似乎需要显式转换:

select * from reports where day > cast('2015-05-01' as date)

在 Spark SQL 文档中没有它的迹象,但它似乎已经在 Transact-SQL 和 Hive 中可用了一段时间。

关于apache-spark - 无法比较 Spark SQL 查询中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com