在 Spark / Scala 中减少时折叠错误
Error on fold when reducing in Spark / Scala
假设我有一个包含一些列的数据框:
为什么这不起作用?
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|
+---+----+----+----+----+----+------+
先决条件:
根据编译器消息和 API 用法,我们可以推断出 columnsToConcat
是 Seq[o.a.s.sql.Column]
或等价物。
按照约定 foldLeft
方法需要映射到累加器(初始值)的函数。这里是Seq.foldLeft
signature
def foldLeft[B](z: B)(op: (B, A) ⇒ B): B
+
在Scala中是一种方法,具体来说是.+
调用的语法糖。
这意味着在以下情况下:
columnsToConcat.foldLeft(0)((x,y)=>(x+y))
是
columnsToConcat.foldLeft(0)((x: Int, y: Column) => x + y)
并且您要求 Int
的 +
方法(累加器的推断类型 - 0
),并且由于 Int
- 并且没有 +
(org.apache.spark.sql.Column) => Int
Int
的方法(错误已经列出了可用的方法,并且这种方法不存在并不意外),在当前范围内,也不存在隐式转换从 Int
到任何提供这种方法的类型。
第二种情况你问的是
columnsToConcat.foldLeft(lit(0))((x,y)=>(x+y))
是
columnsToConcat.foldLeft(lit(0))((x: Column, y: Column) => x + y)
和 +
指的是 Column.+
(因为 type of lit(0)
is Column
) and such method, which acceptsAny
和 returns Column
存在。因为 Column <: Any
类型约束得到满足
假设我有一个包含一些列的数据框:
为什么这不起作用?
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|
+---+----+----+----+----+----+------+
先决条件:
根据编译器消息和 API 用法,我们可以推断出
columnsToConcat
是Seq[o.a.s.sql.Column]
或等价物。按照约定
foldLeft
方法需要映射到累加器(初始值)的函数。这里是Seq.foldLeft
signaturedef foldLeft[B](z: B)(op: (B, A) ⇒ B): B
+
在Scala中是一种方法,具体来说是.+
调用的语法糖。
这意味着在以下情况下:
columnsToConcat.foldLeft(0)((x,y)=>(x+y))
是
columnsToConcat.foldLeft(0)((x: Int, y: Column) => x + y)
并且您要求 Int
的 +
方法(累加器的推断类型 - 0
),并且由于 Int
- 并且没有 +
(org.apache.spark.sql.Column) => Int
Int
的方法(错误已经列出了可用的方法,并且这种方法不存在并不意外),在当前范围内,也不存在隐式转换从 Int
到任何提供这种方法的类型。
第二种情况你问的是
columnsToConcat.foldLeft(lit(0))((x,y)=>(x+y))
是
columnsToConcat.foldLeft(lit(0))((x: Column, y: Column) => x + y)
和 +
指的是 Column.+
(因为 type of lit(0)
is Column
) and such method, which acceptsAny
和 returns Column
存在。因为 Column <: Any
类型约束得到满足