更改数据帧中的列值 spark scala [英] Change column value in a dataframe spark scala
本文介绍了更改数据帧中的列值 spark scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我的数据框现在的样子
This is how my dataframe looks like at the moment
+------------+
| DATE |
+------------+
| 19931001|
| 19930404|
| 19930603|
| 19930805|
+------------+
我正在尝试将此字符串值重新格式化为 yyyy-mm-dd hh:mm:ss.fff 并将其保留为字符串而不是日期类型或时间戳.
I am trying to reformat this string value to yyyy-mm-dd hh:mm:ss.fff and keep it as a string not a date type or time stamp.
我将如何使用 withColumn 方法做到这一点?
How would I do that using the withColumn method ?
推荐答案
这里是使用 UDF
和 withcolumn
的解决方案,我假设你有一个字符串日期字段在 Dataframe
Here is the solution using UDF
and withcolumn
, I have assumed that you have a string date field in Dataframe
//Create dfList dataframe
val dfList = spark.sparkContext
.parallelize(Seq("19931001","19930404", "19930603", "19930805")).toDF("DATE")
dfList.withColumn("DATE", dateToTimeStamp($"DATE")).show()
val dateToTimeStamp = udf((date: String) => {
val stringDate = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6,8)
val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
format.format(new SimpleDateFormat("yyy/MM/dd").parse(stringDate))
})
这篇关于更改数据帧中的列值 spark scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文