Scala Spark无框WithColumnRename嵌套字段
假设我有以下代码Scala Spark无框WithColumnRename嵌套字段,scala,apache-spark,frameless,Scala,Apache Spark,Frameless,假设我有以下代码 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.withColumnR
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]
当我尝试转换的字段是嵌套的时,如何实现此转换?WithColumnRename的签名要求在第一个参数中使用符号,因此我不知道如何执行此操作…
WithColumnRename
不允许您转换列。为此,应使用with column
。一种方法是强制转换列并重新创建结构
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)) }
你能解释一下你想做什么转换吗?我只是想把
b.v
列从Int
转换成Long
,但问题是如何转换嵌套为b.v
的列,我想用无框架API而不是数据框架API来解决这个问题,无论如何,谢谢