我有以下代码:
table.select(datediff(table.col("Start Time"), table.col("End Time"))).show()
Date format is 2016-05-19 09:23:28 (
YYYY-MM-DD HH:mm:SS
)
功能 日期差异 计算天数的差异。但我想在几秒钟内有所不同。
最佳答案
您可以使用 unix_timestamp()
函数将日期转换为秒。
import org.apache.spark.sql.functions._
//For $ notation columns // Spark 2.0
import spark.implicits._
table.withColumn("date_diff",
(unix_timestamp($"Start Time") - unix_timestamp($"End Time"))
).show()
编辑: (根据评论)UDF 将秒转换为 HH:mm:ss
sqlContext.udf.register("sec_to_time", (s: Long) =>
((s / 3600L) + ":" + (s / 60L) + ":" + (s % 60L))
)
//Use registered UDF now
table.withColumn("date_diff",
sec_to_time(unix_timestamp($"Start Time") - unix_timestamp($"End Time"))
).show()
关于scala - Spark SQL datediff 以秒为单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41342785/