我有以下代码:
table.select(datediff(table.col("Start Time"), table.col("End Time"))).show()
日期格式为2016-05-19 09:23:28(
YYYY-MM-DD HH:mm:SS
)
函数datediff计算天数之差。但我想在几秒钟内有所不同。
您可以使用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()
编辑:(根据评论)
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句