我的数据集包含名为“key(string),value(long)”
的列列密钥的值,如prefix.20171012.111.2222,列值的值如9999。
我想将数据集转换为一个新的数据集,将colmun键拆分为其他类似“day,rt,item_id,value”。
怎么做,非常感谢
答案 0 :(得分:0)
// input ds looks like this
+--------+-----+
| key|value|
+--------+-----+
|20171011| 9999|
+--------+-----+
//import the functions you need
import org.apache.spark.sql.functions.{to_date, month, year, dayofmonth}
// ds2
val ds2 = ds.withColumn("date", to_date($"key", "yyyyMMdd"))
// ds2.show()
+--------+-----+----------+
| key|value| date|
+--------+-----+----------+
|20171011| 9999|2017-10-11|
+--------+-----+----------+
// ds3
val ds3 = ds2.withColumn("Month", month($"date"))
.withColumn("Year", year($"date"))
.withColumn("Date", dayofmonth($"date"))
// ds3.show()
+--------+-----+----+-----+----+
| key|value|Date|Month|Year|
+--------+-----+----+-----+----+
|20171011| 9999| 11| 10|2017|
+--------+-----+----+-----+----+