scala - Spark DataFrame 将字符串格式的毫秒时间戳列转换为以毫秒为单位的人类可读时间
问题描述
自纪元以来,我有一个带有时间戳列的Spark DataFrame ,以毫秒为单位。该列是一个字符串。我现在想将该列转换为可读的人类时间,但保留毫秒。 例如:
1614088453671 -> 23-2-2021 13:54:13.671
我发现的每个示例都将时间戳转换为正常的人类可读时间,无需毫秒。
我有的:
+------------------+
|epoch_time_seconds|
+------------------+
|1614088453671 |
+------------------+
我想达到什么:
+------------------+------------------------+
|epoch_time_seconds|human_date |
+------------------+------------------------+
|1614088453671 |23-02-2021 13:54:13.671 |
+------------------+------------------------+
解决方案
可以使用 获得毫秒之前的时间date_format from_unixtime
,而可以使用模获得毫秒。使用format_string
.
val df2 = df.withColumn(
"human_date",
format_string(
"%s.%s",
date_format(
from_unixtime(col("epoch_time_seconds")/1000),
"dd-MM-yyyy HH:mm:ss"
),
col("epoch_time_seconds") % 1000
)
)
df2.show(false)
+------------------+-----------------------+
|epoch_time_seconds|human_date |
+------------------+-----------------------+
|1614088453671 |23-02-2021 13:54:13.671|
+------------------+-----------------------+
推荐阅读
- java - CVN 14 的 ARQC/ARPC 验证
- node.js - Puppeteer:无法启动浏览器进程!产卵
- javascript - 尝试通过 Three.js 模拟 3D 效果
- java - Java 应用程序中使用的 OpenCV JNI 库中的内存使用跟踪工具
- reactjs - 有没有办法对 MUI-dataTable 中的列进行分组?
- php - Increment、Decrement 以及 Model::decrement() 和 Model::increment() 在 Laravel 中无法正常工作
- oracle - 用于动态查询的 Oracle 游标
- flutter - 为什么 onTap 方法在火焰中不起作用?
- react-native - 显示为一行,除非它会溢出容器,在这种情况下显示为列?
- javascript - 在javascript中将字符串的一部分大写