我的数据格式是用apache thrift定义的,代码由scrooge生成。我使用镶木地板将它存储在火花中,非常类似于此blog中所解释的。
我可以非常轻松地将数据读回到Dataframe中,只需执行以下操作:
val df = sqlContext.read.parquet("/path/to/data")
我可以在RDD中阅读更多体操:
def loadRdd[V <: TBase[_, _]](inputDirectory: String, vClass: Class[V]): RDD[V] = {
implicit val ctagV: ClassTag[V] = ClassTag(vClass)
ParquetInputFormat.setReadSupportClass(jobConf, classOf[ThriftReadSupport[V]])
ParquetThriftInputFormat.setThriftClass(jobConf, vClass)
val rdd = sc.newAPIHadoopFile(
inputDirectory, classOf[ParquetThriftInputFormat[V]], classOf[Void], vClass, jobConf)
rdd.asInstanceOf[NewHadoopRDD[Void, V]].values
}
loadRdd("/path/to/data", classOf[MyThriftClass])
我的问题是:如何在使用spark 1.6发布的新数据集api中访问该数据?我想要的原因是数据集api的好处:类型安全与数据帧速度相同。
我理解需要某种类型的编码器,并且已经为原始类型和案例类提供了这些编码器,但我所拥有的是节约代码生成的代码(java或scala代码,任何一个符合条件),看起来确实如此很像一个案例类,但它不是真的。
我尝试了显而易见的选项,但这些选项无效:
val df = sqlContext.read.parquet("/path/to/data")
df.as[MyJavaThriftClass]
<console>:25: error: Unable to find encoder for type stored in a Dataset. Primitive types (Int, String, etc) and Product types (case classes) are supported by importing sqlContext.implicits._ Support for serializing other types will be added in future releases.
df.as[MyScalaThriftClass]
scala.ScalaReflectionException: <none> is not a term
at scala.reflect.api.Symbols$SymbolApi$class.asTerm(Symbols.scala:199)
at scala.reflect.internal.Symbols$SymbolContextApiImpl.asTerm(Symbols.scala:84)
at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor(ScalaReflection.scala:492)
at org.apache.spark.sql.catalyst.ScalaReflection$.extractorsFor(ScalaReflection.scala:394)
at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:54)
at org.apache.spark.sql.SQLImplicits.newProductEncoder(SQLImplicits.scala:41)
... 48 elided
df.as[MyScalaThriftClass.Immutable]
java.lang.UnsupportedOperationException: No Encoder found for org.apache.thrift.protocol.TField
- field (class: "org.apache.thrift.protocol.TField", name: "field")
- array element class: "com.twitter.scrooge.TFieldBlob"
- field (class: "scala.collection.immutable.Map", name: "_passthroughFields")
- root class: "com.worldsense.scalathrift.ThriftRange.Immutable"
at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor(ScalaReflection.scala:597)
at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor$1.apply(ScalaReflection.scala:509)
at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor$1.apply(ScalaReflection.scala:502)
at scala.collection.immutable.List.flatMap(List.scala:327)
at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor(ScalaReflection.scala:502)
at org.apache.spark.sql.catalyst.ScalaReflection$.toCatalystArray$1(ScalaReflection.scala:419)
at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor(ScalaReflection.scala:537)
at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor$1.apply(ScalaReflection.scala:509)
at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor$1.apply(ScalaReflection.scala:502)
at scala.collection.immutable.List.flatMap(List.scala:327)
at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$extractorFor(ScalaReflection.scala:502)
at org.apache.spark.sql.catalyst.ScalaReflection$.extractorsFor(ScalaReflection.scala:394)
at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:54)
at org.apache.spark.sql.SQLImplicits.newProductEncoder(SQLImplicits.scala:41)
... 48 elided
看起来无形的works fine有Thrift生成的代码,我想知道是否可以用它来生成当前编码器api会接受的东西。
任何提示?
答案 0 :(得分:0)
应该有可能通过传递Encoders.bean(My.getClass)
作为显式隐式来解决此问题。
示例:df.as[MyJavaThriftClass](Encoders.bean(MyJavaThriftClass.getClass))