Spark Frameless withColumnRenamed嵌套字段

问题描述 投票:2回答:1

假设我有以下代码

case class MyTypeInt(a: String, b: MyType2)
case class MyType2(v: Int)
case class MyTypeLong(a: String, b: MyType3)
case class MyType3(v: Long)

val typedDataset = TypedDataset.create(Seq(MyTypeInt("v", MyType2(1))))
typedDataset.withColumnRenamed(???, typedDataset.colMany('b, 'v).cast[Long]).as[MyTypeLong]

当我尝试转换的字段嵌套时,如何实现此转换? withColumnRenamed的签名在第一个参数中请求一个符号,所以我不知道怎么做...

scala apache-spark frameless
1个回答
1
投票

withColumnRenamed不允许您转换列。要做到这一点,你应该使用withColumn。然后,一种方法是转换列并重新创建结构。

scala> val new_ds = ds.withColumn("b", struct($"b.v" cast "long" as "v")).as[MyTypeLong]
scala> new_ds.printSchema
root
|-- a: string (nullable = true)
|-- b: struct (nullable = false)
|    |-- v: long (nullable = true) 

另一种方法是使用map并自己构建对象:

ds.map{ case MyTypeInt(a, MyType2(b)) => MyTypeLong(a, MyType3(b)) } 
© www.soinside.com 2019 - 2024. All rights reserved.