如何使用scala中的withcolumn函数将可变列表添加为Dataframe的一列

blmhpbnm  于 2021-05-17  发布在  Spark
关注(0)|答案(1)|浏览(828)

实际上,我想使用scala的with column函数从现有Dataframe中的可变列表中添加一个新列,有人能帮忙吗
这就是我想做的,但对我没用

val list1=mutable.MutableList[String]()
val list2=mutable.MutableList[String]()
list1+=convertIntToStringBase(30,account,9)  // Applying some transformation to list1 
DF.withColumn("New_col",lit(list1))   // Adding the elements of the list as a new column in a dataframe

错误消息:

Exception in thread "main" java.lang.RuntimeException: Unsupported literal type class scala.collection.mutable.MutableList MutableList

提前谢谢!!

d7v8vwbk

d7v8vwbk1#

您可以在spark 2.2 typedlit中找到支持seq、map和tuple的代码

import org.apache.spark.sql.functions.typedLit

    df.withColumn("some_array", typedLit(Seq(1, 2, 3)))
    df.withColumn("some_struct", typedLit(("foo", 1, 0.3)))
    df.withColumn("some_map", typedLit(Map("key1" -> 1, "key2" -> 2)))

相关问题