【发布时间】:2019-02-04 18:38:42
【问题描述】:
鉴于我有一个包含一些列的数据框:
为什么这不起作用?
val output3b = input.withColumn("sum", columnsToConcat.foldLeft(0)((x,y)=>(x+y)))
notebook:16: error: overloaded method value + with alternatives:
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (org.apache.spark.sql.Column)
val output3b = input.withColumn("sum", columnsToConcat.foldLeft(0)((x,y)=>(x+y))) // does work
^
notebook:16: error: type mismatch;
found : Int
required: org.apache.spark.sql.Column
val output3b = input.withColumn("sum", columnsToConcat.foldLeft(0)((x,y)=>(x+y)))
但这有吗?
val output3a = input.withColumn("concat", columnsToConcat.foldLeft(lit(0))((x,y)=>(x+y)))
使用著名的 lit 函数似乎可以平滑一些事情,但我不知道为什么。
+---+----+----+----+----+----+------+
| ID|var1|var2|var3|var4|var5|concat|
+---+----+----+----+----+----+------+
| a| 5| 7| 9| 12| 13| 46.0|
+---+----+----+----+----+----+------+
【问题讨论】:
标签: scala apache-spark fold