【发布时间】:2016-08-28 12:16:43
【问题描述】:
Spark documentation 展示了如何从 RDD 创建 DataFrame,使用 Scala 案例类来推断模式。我正在尝试使用sqlContext.createDataFrame(RDD, CaseClass) 重现此概念,但我的 DataFrame 最终为空。这是我的 Scala 代码:
// sc is the SparkContext, while sqlContext is the SQLContext.
// Define the case class and raw data
case class Dog(name: String)
val data = Array(
Dog("Rex"),
Dog("Fido")
)
// Create an RDD from the raw data
val dogRDD = sc.parallelize(data)
// Print the RDD for debugging (this works, shows 2 dogs)
dogRDD.collect().foreach(println)
// Create a DataFrame from the RDD
val dogDF = sqlContext.createDataFrame(dogRDD, classOf[Dog])
// Print the DataFrame for debugging (this fails, shows 0 dogs)
dogDF.show()
我看到的输出是:
Dog(Rex)
Dog(Fido)
++
||
++
||
||
++
我错过了什么?
谢谢!
【问题讨论】:
标签: scala apache-spark dataframe apache-spark-sql rdd