我正在使用Spark 1.5.2從scala對象使用以下語法創建數據框。我的目的是爲單元測試創建一個數據。Spark:SQL上下文:從Scala對象創建數據框
class Address (first:String = null, second: String = null, zip: String = null){}
class Person (id: String = null, name: String = null, address: Seq[Address] = null){}
def test() = {
val sqlContext = new SQLContext(sc)
import sqlContext.implicits._
val persons = Seq(
new Person(id = "1", name = "Salim",
address = Seq(new Address(first = "1st street"))),
new Person(name = "Sana",
address = Seq(new Address(zip = "60088")))
)
// The code can't infer schema automatically
val claimDF = sqlContext.createDataFrame(sc.parallelize(persons, 2),classOf[Person])
claimDF.printSchema() // This prints "root" not the schema of Person.
}
相反,如果我轉換人事和地址,以案例類,然後星火可以自動使用上述語法或使用sc.parallelize(persons, 2).toDF
或使用sqlContext.createDataFrame(sc.parallelize(persons, 2),StructType)
我可以繼承模式因爲它不能容納20個以上的字段,所以我們有很多字段。使用StructType會帶來很多不便。案例類最方便,但不能容納太多的屬性。
請幫助,在此先感謝。
我認爲,如果你的類擴展[產品特點](http://www.scala-lang.org/api/2.10.6/#scala.Product),並實施其抽象方法它可能工作。 (由於這個簽名:'createDataFrame [A <:Product](data:Seq [A])') –