val sid_df = hiveContext.sql("SELECT a, b, c, d, e FROM my_table")
val new_reformatted_rdd = sid_df.map(row => {
val t = row.getDouble(0)
val f = row.getFloat(1)
val s = row.getShort(2)
val y = row.getString(3).toShort
val originFormat = new java.text.SimpleDateFormat("MM-dd-yyyy")
val targetFormat = new java.text.SimpleDateFormat("yyyy-MM-dd")
val new_date = targetFormat.format(originFormat.parse(row.getString(4)))
})
我需要来自new_reformatted_rdd的数据帧,这是一个RDD [Unit]。请建议我怎么做。感谢
答案 0 :(得分:1)
您需要使用要保留的变量创建一个元组作为map
的最后一步,否则RDD将为空。之后,您可以使用toDF()
命令创建数据框。不要忘记import
。
val spark = SparkSession.builder.getOrCreate()
import spark.implicits._
val new_reformatted_rdd = sid_df.map(row => {
val t = row.getDouble(0)
val f = row.getFloat(1)
val s = row.getShort(2)
val y = row.getString(3).toShort
val originFormat = new java.text.SimpleDateFormat("MM-dd-yyyy")
val targetFormat = new java.text.SimpleDateFormat("yyyy-MM-dd")
val new_date = targetFormat.format(originFormat.parse(row.getString(4)))
(t, f, s, y, new_date)
}).toDF("col1", "col2", "col3", "col4", "col5")