[英]Add timestamp column to RDD using Scala
我有以下RDD,其中包含數千個條目作為(Int,Double),我想添加一列作為時間戳,使其變為(Int,Double,Datetime)。 我嘗試了以下方法:
val addTimeStampRDD = OriginalRDD.map {
case(a, b) =>
(a, b, current_timestamp())}
不幸的是,這項工作因錯誤而失敗,例如:
java.lang.NoClassDefFoundError: scala/Product$class
這是因為時間戳是 SQL function? 還有比這更好的選擇嗎?
要在 Spark 中獲得結果,您可以使用java.time.LocalDateTime
中的LocalDateTime
你可以關注這個問題
scala> import java.time.LocalDateTime
import java.time.LocalDateTime
scala> LocalDateTime.now()
您還可以使用DateTimeFormatter
將日期格式化為所需格式
scala> import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter
scala> DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm").format(LocalDateTime.now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.