我正在创建像
这样的列表var transactionList = result.select(col("transaction_id")).distinct().collect().map(_(0)).toList
我想将“transactionList”插入到Dataframe中,然后将其展开
我尝试过像
df.withColumn("transactionList" , ArrayType(for (id <- transactionList) lit(id))
但它不起作用
答案 0 :(得分:1)
您还应该将.map(_(0))
替换为.map(_.getString(0))
result.select(col("transaction_id")).distinct().collect().map(.getString(0))
您可以使用lit
将literal
值转换为Column
df.withColumn("transactionList", lit(transactionList))
如果您有transactionList = List("a", "b")
这将在所有行中添加一个新的列transactionList
作为数组,其值为(a, b)
。
/** * Creates a [[Column]] of literal value. * * The passed in object is returned directly if it is already a [[Column]]. * If the object is a Scala Symbol, it is converted into a [[Column]] also. * Otherwise, a new [[Column]] is created to represent the literal value. * * @group normal_funcs * @since 1.3.0 */