我的数据格式是用apache节俭定义的,代码由scrooge生成。我使用镶木地板将其存储在火花中,非常像本博客中解释的内容。
我可以很容易地将数据读回数据帧,只需执行以下操作:
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
似乎无形与 Thrift 生成的代码一起工作得很好,我想知道我是否可以使用它来生成当前编码器 api 将接受的东西。
有什么提示吗?
应该可以通过将Encoders.bean(My.getClass)
作为显式隐式传递来解决此问题。
示例:df.as[MyJavaThriftClass](Encoders.bean(MyJavaThriftClass.getClass))