scala - spark scala 在日期列和时间列中拆分时间戳列
问题描述
我在将时间戳列拆分为日期和时间列时遇到问题。首先时间不考虑 24 小时格式...其次日期是错误的,我不明白为什么
这是我的输出
+----------+----------+-------------------+---------+
| Date| Timestamp| Time|EventTime|
+----------+----------+-------------------+---------+
|2018-00-30|1540857600|2018-10-30 00:00:00| 12:00:00|
|2018-00-30|1540857610|2018-10-30 00:00:10| 12:00:10|
|2018-00-30|1540857620|2018-10-30 00:00:20| 12:00:20|
|2018-00-30|1540857630|2018-10-30 00:00:30| 12:00:30|
|2018-00-30|1540857640|2018-10-30 00:00:40| 12:00:40|
|2018-00-30|1540857650|2018-10-30 00:00:50| 12:00:50|
|2018-01-30|1540857660|2018-10-30 00:01:00| 12:01:00|
|2018-01-30|1540857670|2018-10-30 00:01:10| 12:01:10|
|2018-01-30|1540857680|2018-10-30 00:01:20| 12:01:20|
|2018-01-30|1540857690|2018-10-30 00:01:30| 12:01:30|
|2018-01-30|1540857700|2018-10-30 00:01:40| 12:01:40|
和我的代码:
val df = data_input
.withColumn("Time", to_timestamp(from_unixtime(col("Timestamp"))))
.withColumn("Date", date_format(col("Time"), "yyyy-mm-dd"))
.withColumn("EventTime", date_format(col("Time"), "hh:mm:ss"))
首先我将 unix Timestamp 列转换为 Time 列,然后我想拆分 Time..
先感谢您
解决方案
您使用了错误的格式代码。具体来说,日期中的“mm”代表分钟,“hh”代表 12 小时值。相反,您需要“MM”和“HH”。像这样:
val df = data_input
.withColumn("Time", to_timestamp(from_unixtime(col("Timestamp"))))
.withColumn("Date", date_format(col("Time"), "yyyy-MM-dd"))
.withColumn("EventTime", date_format(col("Time"), "HH:mm:ss"))
作为参考,以下是您可以使用的日期格式代码:SimpleDateFormat
推荐阅读
- css - 在 Blogger 上添加边距
- hibernate - 使用 jpa、hibernate 的以数据为中心的应用程序的最佳实践/常见模式
- javascript - JavaScript“不是函数”
- java - 由于授权无法访问 API
- dart - 从元素中取消事件侦听器
- sqlite - SQLite 在一个范围内选择日期,但没有星期天和一些假期
- python - 使用更多功能时,功能选择中的错误栏会增加?
- html - 在“媒体”属性中组合逻辑 OR 和逻辑 AND
- r - 从 sapply 中的 strsplit 获取最后一个元素
- apache-spark - Spark 序列化:Tungsten 和 Kryo 如何协同工作?