如何获取RDD[Row](包含所有值)而不是RDD[Unit](为空)


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]。请建议我怎么做。谢谢

您需要创建一个元组作为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")

最新更新