【发布时间】:2020-06-08 16:38:33
【问题描述】:
我正在尝试以MM/dd/yyyy hh:mm:ss AM/PM 的形式转换两个时间戳之间的分钟差。我是使用 SparkSQL 的新手,并尝试使用其他 SQL 语法支持的基本 datediff 函数,即 datediff(minute,start_time,end_time),但这产生了错误:
org.apache.spark.sql.AnalysisException: cannot resolve '`minute`' given input columns: [taxisub.tpep_dropoff_datetime, taxisub.DOLocationID, taxisub.improvement_surcharge, taxisub.VendorID, taxisub.trip_distance, taxisub.tip_amount, taxisub.tolls_amount, taxisub.payment_type, taxisub.fare_amount, taxisub.tpep_pickup_datetime, taxisub.total_amount, taxisub.store_and_fwd_flag, taxisub.extra, taxisub.passenger_count, taxisub.PULocationID, taxisub.mta_tax, taxisub.RatecodeID]; line 1 pos 153;
sparkSQL 的 datediff 似乎不支持 minute 参数。我目前的查询是:
spark.sqlContext.sql("Select to_timestamp(tpep_pickup_datetime,'MM/dd/yyyy hh:mm:ss') as pickup,to_timestamp(tpep_dropoff_datetime,'MM/dd/yyyy hh:mm:ss') as dropoff, datediff(to_timestamp(tpep_pickup_datetime,'MM/dd/yyyy hh:mm:ss'),to_timestamp(tpep_dropoff_datetime,'MM/dd/yyyy hh:mm:ss')) as diff from taxisub ").show()
我的结果是:
+-------------------+-------------------+----+
| pickup| dropoff|diff|
+-------------------+-------------------+----+
|2018-12-15 08:53:20|2018-12-15 08:57:57| 0|
|2018-12-15 08:03:08|2018-12-15 08:07:30| 0|
|2018-12-15 08:28:34|2018-12-15 08:33:31| 0|
|2018-12-15 08:37:53|2018-12-15 08:43:47| 0|
|2018-12-15 08:51:02|2018-12-15 08:55:54| 0|
|2018-12-15 08:03:47|2018-12-15 08:03:50| 0|
|2018-12-15 08:45:21|2018-12-15 08:57:08| 0|
|2018-12-15 08:04:47|2018-12-15 08:29:05| 0|
|2018-12-15 08:01:22|2018-12-15 08:12:15| 0|
+-------------------+-------------------+----+
我假设 datediff 的默认值是以天为单位的差异,因为结果中的值为 0。我应该使用其他参数/函数来确定这两个时间戳之间的分钟差吗?
提前致谢。
【问题讨论】:
-
@Shaido-ReinstateMonica 这个问题是利用 Spark Scala,我正在尝试使用 Spark SQL 解决问题。
-
这有什么不同?将值转换为长整数,减去,然后除以 60。
标签: sql apache-spark apache-spark-sql