scala – Spark SQL datediff在几秒钟内
发布时间:2020-12-16 09:00:37 所属栏目:安全 来源:网络整理
导读:我有以下代码: 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 ) 函数datediff计算天数的差异.但我想在几秒钟内有所不同. 解决方法 您可以使用 unix_timestamp(
我有以下代码:
table.select(datediff(table.col("Start Time"),table.col("End Time"))).show()
函数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() 编辑:(根据评论) 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() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |