火花斯卡拉数据帧时间戳转换排序? [英] spark scala dataframe timestamp conversion sorting?
问题描述
我有以下形式的CSV:
I have a csv of the form:
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
我转换成该数据框使用火花CSV 。 (所以 T
是字符串
类型和值
是整型)。什么是适当的火花Scala的方式,使输出按时间排序?
I convert that into dataframe using spark-csv. (so t
is in String
type and value
is in Integer type). What's the appropriate spark scala way so the output is sorted by time?
我想转换 T
某些类型,它可以让数据帧 sortBy
。但我不熟悉哪些时间戳类型允许数据帧按时间排序。
I was thinking to convert t
to certain type which can allow dataframe sortBy
. But I am not familiar which timestamp type allow dataframe sorting by time.
推荐答案
由于您可以转换为timestamp的格式
Given the format you can either cast to timestamp to
import org.apache.spark.sql.types.TimestampType
df.select($"t".cast(TimestampType)) // or df.select($"t".cast("timestamp"))
要得到适当的日期和时间,或使用 UNIX_TIMESTAMP
(星火1.5+,在星火< 1.5则可以使用同一个名字的蜂巢UDF)功能:
to get proper date time or use unix_timestamp
(Spark 1.5+, in Spark < 1.5 you can use a Hive UDF of the same name) function:
import org.apache.spark.sql.functions.unix_timestamp
df.select(unix_timestamp($"t"))
要得到一个数值再presentation(Unix时间戳中的秒)。
to get a numerical representation (Unix timestamp in seconds).
在一个侧面说明,没有理由你不能的OrderBy($T)
直接。字典顺序应该只是罚款这里。
On a side note there is no reason you couldn't orderBy($"t")
directly. Lexicographic order should work just fine here.
这篇关于火花斯卡拉数据帧时间戳转换排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!