我想获取每一行的当前时间戳。
我使用下面的代码
dataframe.withColumn("current_date",current_timestamp());
但是 current_timestamp() 是在序列化之前计算的,所以我总是会得到相同的日期。
如何为数据帧的每一行评估 current_timestamp()。
我需要你的帮助。
谢谢。
最佳答案
试试这个-
df2.withColumn("current_date", expr("reflect('java.lang.System', 'currentTimeMillis')"))
.show(false)
/**
* +-----+------+-------------+
* |class|gender|current_date |
* +-----+------+-------------+
* |1 |m |1594137247247|
* |1 |m |1594137247247|
* |1 |f |1594137247247|
* |2 |f |1594137247272|
* |2 |f |1594137247272|
* |3 |m |1594137247272|
* |3 |m |1594137247272|
* +-----+------+-------------+
*/
df2.withColumn("current_date", expr("reflect('java.time.LocalDateTime', 'now')"))
.show(false)
/**
* +-----+------+-----------------------+
* |class|gender|current_date |
* +-----+------+-----------------------+
* |1 |m |2020-07-07T21:24:07.377|
* |1 |m |2020-07-07T21:24:07.378|
* |1 |f |2020-07-07T21:24:07.378|
* |2 |f |2020-07-07T21:24:07.398|
* |2 |f |2020-07-07T21:24:07.398|
* |3 |m |2020-07-07T21:24:07.398|
* |3 |m |2020-07-07T21:24:07.398|
* +-----+------+-----------------------+
*/
// you can convert current_date to timestamp by casting it to "timestamp"
关于java - 使用 Spark/Java 获取每行数据帧的当前时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62778701/