spark scala dataframe时间戳转换排序?

Car*_*Pun 3 scala dataframe apache-spark apache-spark-sql

我有一个csv的形式:

t,value
2012-01-12 12:30:00,4
2012-01-12 12:45:00,3
2012-01-12 12:00:00,12
2012-01-12 12:15:00,13
2012-01-12 13:00:00,7
Run Code Online (Sandbox Code Playgroud)

我使用spark-csv将其转换为数据帧.(所以tString类型,并且value是整数类型).什么是适当的火花scala方式,所以输出按时间排序?

我正在考虑转换t为允许数据帧的某种类型sortBy.但我不熟悉哪种时间戳类型允许按时间排序数据帧.

zer*_*323 7

给定格式,您可以转换为时间戳

import org.apache.spark.sql.types.TimestampType

df.select($"t".cast(TimestampType)) // or df.select($"t".cast("timestamp"))
Run Code Online (Sandbox Code Playgroud)

要获得正确的日期时间或使用unix_timestamp(Spark 1.5+,在Spark <1.5,你可以使用同名的Hive UDF)功能:

import org.apache.spark.sql.functions.unix_timestamp

df.select(unix_timestamp($"t"))
Run Code Online (Sandbox Code Playgroud)

得到一个数字表示(Unix时间戳,以秒为单位).

在旁注中没有理由你不能orderBy($"t")直接.字典顺序应该在这里工作得很好.