mt8*_*t88 16 scala apache-spark
我在数据框中有两个时间戳列,我希望得到它的小时差,或者小时差.目前,我可以通过这样做来获得日常差异,四舍五入
val df2 = df1.withColumn("time", datediff(df1("ts1"), df1("ts2")))
Run Code Online (Sandbox Code Playgroud)
但是,当我查看文档页面 https://issues.apache.org/jira/browse/SPARK-8185时, 我没有看到任何额外的参数来更改单位.他们应该为此使用不同的功能吗?
Dan*_*ula 21
您可以在几秒钟内获得差异
import org.apache.spark.sql.functions._
val diff_secs_col = col("ts1").cast("long") - col("ts2").cast("long")
Run Code Online (Sandbox Code Playgroud)
然后你可以做一些数学计算来得到你想要的单位.例如:
val df2 = df1
.withColumn( "diff_secs", diff_secs_col )
.withColumn( "diff_mins", diff_secs_col / 60D )
.withColumn( "diff_hrs", diff_secs_col / 3600D )
.withColumn( "diff_days", diff_secs_col / (24D * 3600D) )
Run Code Online (Sandbox Code Playgroud)
或者,在pyspark:
from pyspark.sql.functions import *
diff_secs_col = col("ts1").cast("long") - col("ts2").cast("long")
df2 = df1 \
.withColumn( "diff_secs", diff_secs_col ) \
.withColumn( "diff_mins", diff_secs_col / 60D ) \
.withColumn( "diff_hrs", diff_secs_col / 3600D ) \
.withColumn( "diff_days", diff_secs_col / (24D * 3600D) )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19120 次 |
| 最近记录: |