我尝试搜索了很多,但只能在 Spark SQL 中找到 add_month 函数,因此最终在这里打开了一个新线程。希望有人可以提供任何帮助。
我正在尝试使用 sqlContext 将 12、24 和 48 小时添加到 Spark SQL 中的日期列。我正在使用 1.6.1 版本的 Spark,我需要这样的东西:
SELECT N1.subject_id, '12-HOUR' AS notes_period, N1.chartdate_start, N2.chartdate, N2.text
FROM NOTEEVENTS N2,
(SELECT subject_id, MIN(chartdate) chartdate_start
FROM NOTEEVENTS
WHERE subject_id = 283
AND category != 'Discharge summary'
GROUP BY subject_id) N1
WHERE N2.subject_id = N1.subject_id
and n2.chartdate < n1.chartdate_start + interval '1 hour' * 12
请注意最后一个子句,它是用 PostgreSql 编写的,也是我在 Spark SQL 中所需要的。我真的很感激我能得到的任何帮助。
谢谢。
最佳答案
与 PostgreSQL 相同,您可以使用 INTERVAL
.在 SQL 中
spark.sql("""SELECT current_timestamp() AS now,
current_timestamp() + INTERVAL 12 HOURS AS now_plus_twelve"""
).show(false)
+-----------------------+-----------------------+
|now |now_plus_twelve |
+-----------------------+-----------------------+
|2017-12-14 10:49:15.115|2017-12-14 22:49:15.115|
+-----------------------+-----------------------+
与
Dataset
- 斯卡拉:import org.apache.spark.sql.functions.{current_timestamp, expr}
spark.range(1)
.select(
current_timestamp as "now",
current_timestamp + expr("INTERVAL 12 HOURS") as "now_plus_twelve"
).show(false)
+-----------------------+-----------------------+
|now |now_plus_twelve |
+-----------------------+-----------------------+
|2017-12-14 10:56:59.185|2017-12-14 22:56:59.185|
+-----------------------+-----------------------+
Python:
from pyspark.sql.functions import current_timestamp, expr
(spark.range(1).select(
current_timestamp().alias("now"),
(current_timestamp() + expr("INTERVAL 12 HOURS")).alias("now_plus_twelve")))
关于apache-spark - 将 12 小时添加到 Spark 中的日期时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40883084/