繁体   English   中英

如何在Spark Scala数据帧中将map(key,struct)转换为map(key,caseclass)

[英]How to convert map(key,struct) to map(key,caseclass) in spark scala dataframe

我有数据框架构-

resultDF.printSchema

 |-- SKU_ID_MAP: string (nullable = true)
 |-- SKU_IMAGE_MAP: map (nullable = true)
 |    |-- key: string
 |    |-- value: struct (valueContainsNull = true)
 |    |    |-- image_id: string (nullable = true)
 |    |    |-- image_name: string (nullable = true)
 |    |    |-- image_path: string (nullable = true)

我想从DF上方创建这样的最终数据框。

   case class Devicesku2 (
     sku_id : String,
     sku_images: Map[String, ImageInfo2]
   )

   resultDF.map(
      row => Devicesku2(
                row.getAs[String]("SKU_ID"),
                row.getAs[Map]("SKU_IMAGE_MAP")
   ).toDF

在上面的row.getAs [Map]给出编译时错误,因为值是结构类型。

有人可以帮忙吗? 谢谢,

`

如果将case class的元素重命名为:

case class Devicesku2 (
  sku_id_map: String,
  sku_image_map: Map[String, ImageInfo2]
)

你可以用

resultDF.as[Devicesku2]

否则,如Aluan Haddad的评论中所述,您将需要

row.getAs[Map[String, ImageInfo2]]("SKU_IMAGE_MAP")

暂无
暂无

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

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