数据处理在Spark中使用Java

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

Data processing on spark using java

问题

我有一个 DataSet。

    +-------------------+
    |       日期         |
    +-------------------+
    |1970-01-01 06:00:00|
    +-------------------+

我想要只提取小时和分钟。列 "日期" 是时间戳类型。
我尝试了以下代码:

           String format = "HH:mm";
           df.withColumn("时:分",unix_timestamp(col("日期"),format)).show(100);
但对我来说没有起作用。

谢谢。
英文:

I have a DataSet.

+-------------------+
|     Date          |
+-------------------+
|1970-01-01 06:00:00|
+-------------------+

I want to take only the hour and the minute.The column Date is on timestamp.
I tried the following code :

       String format = "HH:mm";
       df.withColumn("hour:minute",unix_timestamp(col("Date"),format)).show(100);

But doesn't work for me .

Thank you .

答案1

得分: 1

unix_timestamp 函数将时间戳转换为 epoch/unix 时间

  • 使用 date_formatfrom_unixtime(unix_timestamp()) 函数之一来提取 hour:minute

示例:

df.withColumn("hour:minute", date_format(col("Date"), "HH:mm")).show()
df.withColumn("hour:minute", from_unixtime(unix_timestamp(col("Date"), "yyyy-MM-dd HH:mm:ss"), "HH:mm")).show()

//+-------------------+-----------+
//|               Date|hour:minute|
//+-------------------+-----------+
//|1970-01-01 06:00:00|      06:00|
//+-------------------+-----------+
英文:

unix_timestamp converts timestamp to epoch/unix time.

  • Use either date_format,from_unixtime(unix_timestamp()) functions to extract hour:minute.

Example:

df.withColumn("hour:minute",date_format(col("Date"),"HH:mm")).show()
df.withColumn("hour:minute",from_unixtime(unix_timestamp(col("Date"),"yyyy-MM-dd HH:mm:ss"),"HH:mm")).show()

//+-------------------+-----------+
//|               Date|hour:minute|
//+-------------------+-----------+
//|1970-01-01 06:00:00|      06:00|
//+-------------------+-----------+

huangapple
  • 本文由 发表于 2020年5月5日 01:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598411.html
匿名

发表评论

匿名网友

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

确定