Spark documentationは、Scalaのケースクラスを使用してスキーマを推論する、RDDからDataFrameを作成する方法を示しています。 sqlContext.createDataFrame(RDD, CaseClass)
を使用してこのコンセプトを再現しようとしていますが、DataFrameが空になります。ここに私のScalaのコードは次のとおりです。ケースクラスベースのRDDをDataFrameに変換するにはどうすればよいですか?
// 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)
++
||
++
||
||
++
?
ありがとうございます!
このどちらかが働いて文句を言わない仕事を
ようStructField
であなたのRDD
のスキーマを定義し、createDataFrame
toDF()
。私はまた、エラーを回避するために、私の主な機能の外にケースクラスの定義を移動しなければなりませんでした:Dogタグは利用できません。ありがとう! – sparkour私は非常に興味深いので、2番目のパラメータはJava APIから呼び出すときにのみ必要なので、scalaは自動的に列に変換されるべきTypeのフィールドを検出するでしょうか? – qwwqwwq