9 scala apache-spark apache-spark-sql
我有以下代码:
table.select(datediff(table.col("Start Time"), table.col("End Time"))).show()
Run Code Online (Sandbox Code Playgroud)
日期格式为2016-05-19 09:23:28(
YYYY-MM-DD HH:mm:SS)
函数datediff计算天数的差异.但我想在几秒钟内有所不同.
mrs*_*vas 10
您可以使用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()
Run Code Online (Sandbox Code Playgroud)
编辑:(根据评论)
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11445 次 |
| 最近记录: |