0

我正在将一些 Spark 1.6 代码更新为 2.0.1,并且在使用 map 时遇到了一些问题。

我在 SO 问题上看到了其他问题,例如encoder-error-while-trying-to-map-dataframe-row-to-updated-row但我无法让这些技术发挥作用,对于下面的这种情况来说它们似乎很荒谬。

val df = spark.sqlContext.read.parquet(inputFile)
df: org.apache.spark.sql.DataFrame = [device_id: string, hour: string ... 9 more fields]

val deviceAggDF = df.select("device_id").distinct
deviceAggDF: org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] = [device_id: string]

deviceAggDF.map( x =>
  (
    Map("ID" -> x.getAs[String](0)),
    Map()
  )
)
scala.MatchError: Nothing (of class scala.reflect.internal.Types$ClassNoArgsTypeRef)
  at org.apache.spark.sql.catalyst.ScalaReflection$.schemaFor(ScalaReflection.scala:667)
  at org.apache.spark.sql.catalyst.ScalaReflection$.toCatalystArray$1(ScalaReflection.scala:448)
  at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor(ScalaReflection.scala:482)
  at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$9.apply(ScalaReflection.scala:592)
  at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$9.apply(ScalaReflection.scala:583)
  at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
  at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
  at scala.collection.immutable.List.foreach(List.scala:381)
  at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:241)
  at scala.collection.immutable.List.flatMap(List.scala:344)
  at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor(ScalaReflection.scala:583)
  at org.apache.spark.sql.catalyst.ScalaReflection$.serializerFor(ScalaReflection.scala:425)
  at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:61)
  at org.apache.spark.sql.Encoders$.product(Encoders.scala:274)
  at org.apache.spark.sql.SQLImplicits.newProductEncoder(SQLImplicits.scala:47)
4

1 回答 1

1

要返回空Map,您应该指定可以 ecnoded 的类型,例如:

deviceAggDF.map( x =>
  (
    Map("ID" -> x.getAs[String](0)),
    Map[String, String]()
  )
)

Map()Map[Nothing,Nothing]并且不能用于Dataset.

于 2016-11-04T16:36:01.127 回答