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])') –