简体   繁体   English

如何创建地图数据集?

[英]How to create a Dataset of Maps?

I'm using Spark 2.2 and am running into troubles when attempting to call spark.createDataset on a Seq of Map .我正在使用 Spark 2.2 并且在尝试在MapSeq上调用spark.createDataset时遇到了麻烦。

Code and output from my Spark Shell session follow:我的 Spark Shell 会话的代码和输出如下:

// createDataSet on Seq[T] where T = Int works
scala> spark.createDataset(Seq(1, 2, 3)).collect
res0: Array[Int] = Array(1, 2, 3)

scala> spark.createDataset(Seq(Map(1 -> 2))).collect
<console>:24: 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 spark.implicits._
Support for serializing other types will be added in future releases.
       spark.createDataset(Seq(Map(1 -> 2))).collect
                          ^

// createDataSet on a custom case class containing Map works
scala> case class MapHolder(m: Map[Int, Int])
defined class MapHolder

scala> spark.createDataset(Seq(MapHolder(Map(1 -> 2)))).collect
res2: Array[MapHolder] = Array(MapHolder(Map(1 -> 2)))

I've tried import spark.implicits._ , though I'm fairly certain that's implicitly imported by the Spark shell session.我试过import spark.implicits._ ,但我相当确定它是由 Spark shell 会话隐式导入的。

Is this is a case not covered by current encoders?这是当前编码器未涵盖的情况吗?

It is not covered in 2.2, but can be easily addressed.它没有在 2.2 中涵盖,但可以很容易地解决。 You can add required Encoder using ExpressionEncoder , either explicitly:您可以使用ExpressionEncoder添加所需的Encoder ,或者明确地:

import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder  
import org.apache.spark.sql.Encoder

spark
  .createDataset(Seq(Map(1 -> 2)))(ExpressionEncoder(): Encoder[Map[Int, Int]])

or implicitly :implicitly

implicit def mapIntIntEncoder: Encoder[Map[Int, Int]] = ExpressionEncoder()
spark.createDataset(Seq(Map(1 -> 2)))

Just FYI that the above expression just works in Spark 2.3 (as of this commit if I'm not mistaken).仅供参考,上述表达式仅适用于 Spark 2.3(如果我没记错的话,从这次提交开始)。

scala> spark.version
res0: String = 2.3.0

scala> spark.createDataset(Seq(Map(1 -> 2))).collect
res1: Array[scala.collection.immutable.Map[Int,Int]] = Array(Map(1 -> 2))

I think it's because newMapEncoder is now part of spark.implicits .我认为这是因为newMapEncoder现在是spark.implicits一部分。

scala> :implicits
...
  implicit def newMapEncoder[T <: scala.collection.Map[_, _]](implicit evidence$3: reflect.runtime.universe.TypeTag[T]): org.apache.spark.sql.Encoder[T]

You could "disable" the implicit by using the following trick and give the above expression a try (that will lead to an error).您可以使用以下技巧“禁用”隐式并尝试上述表达式(这将导致错误)。

trait ThatWasABadIdea
implicit def newMapEncoder(ack: ThatWasABadIdea) = ack

scala> spark.createDataset(Seq(Map(1 -> 2))).collect
<console>:26: 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 spark.implicits._  Support for serializing other types will be added in future releases.
       spark.createDataset(Seq(Map(1 -> 2))).collect
                          ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM