UnixTime 在 Spark/Java 中的使用

huangapple go评论71阅读模式
英文:

UnixTime on spark / java

问题

我有以下数据框:

+------------------------+
| Tracking_Time          |
+------------------------+
|2020-02-18 14:50:07     |
|2020-02-17 17:15:45     |
+------------------------+

我使用以下代码:

df.withColumn("current_date", expr("reflect('java.lang.System', 'currentTimeMillis')"))
  .withColumn("Tracking_Time_ms", unix_timestamp(col("Tracking_Time")));

我得到以下输出

+------------------------+------------------------+------------------------+
| Tracking_Time          |current_date            |Tracking_Time_ms        |
+------------------------+------------------------+------------------------+
|2020-02-18 14:50:07     |1598108268389           |1582037407              |              
|2020-02-17 17:15:45     |1598108270182           |1581959745              |
+------------------------+------------------------+------------------------+

如何将“Tracking_Time_ms”的格式转换为“current_date”的格式?
英文:

I have the following dataframe :

+------------------------+
| Tracking_Time          |
+------------------------+
|2020-02-18 14:50:07     |
|2020-02-17 17:15:45     |
+------------------------+

I use the following code :

df.withColumn("current_date", expr("reflect('java.lang.System', 'currentTimeMillis')"))
  .withColumn("Tracking_Time_ms", unix_timestamp(col("Tracking_Time")));

I get the following output

+------------------------+------------------------+------------------------+
| Tracking_Time          |current_date            |Tracking_Time_ms        |
+------------------------+------------------------+------------------------+
|2020-02-18 14:50:07     |1598108268389           |1582037407              |              
|2020-02-17 17:15:45     |1598108270182           |1581959745              |
+------------------------+------------------------+------------------------+

How can I convert the format of Tracking_Time_ms to the format of current_date?

答案1

得分: 1

Unix时间戳是秒数的累加。此计数从1970年1月1日的Unix纪元(UTC时间)开始。

因此,您在Tracking_Time_ms下看到的值实际上是以秒为单位的。

要将其转换为毫秒(与current_date相同),您需要将其乘以1000。

英文:

The unix time stamp is a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.

So the values you see under Tracking_Time_ms are actually in seconds.

To convert it to ms (to be the same as current_date) you need to multiply it by 1000.

huangapple
  • 本文由 发表于 2020年8月23日 00:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63538272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定