如何在Spark中使用日期和时间值对列进行排序? [英] How to sort a column with Date and time values in Spark?

查看:215
本文介绍了如何在Spark中使用日期和时间值对列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我将其作为火花中的数据框.此时间/日期"值构成了数据框中的一列.

Note: I have this as a Dataframe in spark. This Time/Date values constitute a single column in the Dataframe.

输入:

04年11月4日03.36.13.000000000 PM
15年11月6日03.42.21.000000000 PM
15年11月5日03.32.05.000000000 PM
2015年11月6日03.32.14.000000000 AM

04-NOV-16 03.36.13.000000000 PM
06-NOV-15 03.42.21.000000000 PM
05-NOV-15 03.32.05.000000000 PM
06-NOV-15 03.32.14.000000000 AM

预期输出:

05-NOV-15 03.32.05.000000000 PM
06-NOV-15 03.32.14.000000000 AM
06-NOV-15 03.42.21.000000000 PM
04-NOV-16 03.36.13.000000000 PM

推荐答案

由于此格式不是标准格式,因此您需要使用unix_timestamp函数来解析字符串并将其转换为时间戳类型:

As this format is not standard, you need to use the unix_timestamp function to parse the string and convert into a timestamp type:

import org.apache.spark.sql.functions._

// Example data
val df = Seq(
  Tuple1("04-NOV-16 03.36.13.000000000 PM"),
  Tuple1("06-NOV-15 03.42.21.000000000 PM"),
  Tuple1("05-NOV-15 03.32.05.000000000 PM"),
  Tuple1("06-NOV-15 03.32.14.000000000 AM")
).toDF("stringCol")

// Timestamp pattern found in string
val pattern = "dd-MMM-yy hh.mm.ss.S a"

// Creating new DataFrame and ordering
val newDF = df
  .withColumn("timestampCol", unix_timestamp(df("stringCol"), pattern).cast("timestamp"))
  .orderBy("timestampCol")

newDF.show(false)

结果:

+-------------------------------+---------------------+
|stringCol                      |timestampCol         |
+-------------------------------+---------------------+
|05-NOV-15 03.32.05.000000000 PM|2015-11-05 15:32:05.0|
|06-NOV-15 03.32.14.000000000 AM|2015-11-06 03:32:14.0|
|06-NOV-15 03.42.21.000000000 PM|2015-11-06 15:42:21.0|
|04-NOV-16 03.36.13.000000000 PM|2016-11-04 15:36:13.0|
+-------------------------------+---------------------+

有关unix_timestamp和其他实用程序功能的更多信息,请参见

More about the unix_timestamp and other utility functions can be found here.

要构建时间戳记格式,可以参考 SimpleDateFormatter文档

For building the timestamp format, one can refer to the SimpleDateFormatter docs

,如pheeleeppoo所说,假设您只想在数据框中保留字符串类型的列,则可以直接按表达式排序,而不用创建新列:

Edit 1: as said by pheeleeppoo, you could order directly by the expression, instead of creating a new column, assuming you want to keep only the string-typed column in your dataframe:

val newDF = df.orderBy(unix_timestamp(df("stringCol"), pattern).cast("timestamp"))


请注意,unix_timestamp函数的精度以秒为单位,因此,如果毫秒真的很重要,则可以使用udf:


Edit 2: Please note that the precision of the unix_timestamp function is in seconds, so if the milliseconds are really important, an udf can be used:

def myUDF(p: String) = udf(
  (value: String) => {
    val dateFormat = new SimpleDateFormat(p)
    val parsedDate = dateFormat.parse(value)
    new java.sql.Timestamp(parsedDate.getTime())
  }
)

val pattern = "dd-MMM-yy hh.mm.ss.S a"
val newDF = df.withColumn("timestampCol", myUDF(pattern)(df("stringCol"))).orderBy("timestampCol")

这篇关于如何在Spark中使用日期和时间值对列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆