簡體   English   中英

如何使用 scala 中的 withColumn function 添加可變列表作為 dataframe 的列

[英]How to add a mutable list as a column of a dataframe using withColumn function in scala

我實際上想從我現有的 dataframe 中的可變列表中添加一個新列,使用 scala 的列 function,有人可以幫忙嗎

這就是我想做的,但對我不起作用

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

提前致謝!!

你可以在 Spark 2.2 中找到支持 Seq、Map 和 Tuples 的 typedLit

  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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM