Late answer ... but still, if that helps anyone, the timestamp can be retrieved in milliseconds. First, define a function using the Java API for formatting:
Using Java 7 - util.Date/DateFormat style:
def returnFormattedTime(ts: Long): String = {
val date = new Date(ts)
val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val formattedDate = formatter.format(date)
formattedDate
}
Or, using Java 8-style util.time:
def returnFormattedTime(ts: Long): String = {
val date = Instant.ofEpochMilli(ts)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault())
val formattedDate = formatter.format(date)
formattedDate
}
Finally, use the foreachRDD method to get the timestamp:
dstreamIns.foreachRDD((rdd, time) =>
....
println(s"${returnFormattedTime(time.milliseconds)}")
....
)
source
share