我正在使用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])') –