以下代码工作正常,直到我在 agg
之后添加 show
为止。为什么show
不可能?
val tempTableB = tableB.groupBy("idB")
.agg(first("numB").as("numB")) //when I add a .show here, it doesn't work
tableA.join(tempTableB, $"idA" === $"idB", "inner")
.drop("idA", "numA").show
错误提示:
error: overloaded method value join with alternatives:
(right: org.apache.spark.sql.Dataset[_],joinExprs: org.apache.spark.sql.Column,joinType: String)org.apache.spark.sql.DataFrame <and>
(right: org.apache.spark.sql.Dataset[_],usingColumns: Seq[String],joinType: String)org.apache.spark.sql.DataFrame
cannot be applied to (Unit, org.apache.spark.sql.Column, String)
tableA.join(tempTableB, $"idA" === $"idB", "inner")
^
为什么会这样?
最佳答案
.show()
是一个带有副作用的函数,我们在 Scala 中称之为副作用。它打印到 stdout 并返回 Unit()
,就像 println
示例:
val a = Array(1,2,3).foreach(println)
a: Unit = ()
在 scala 中,你可以假设一切都是函数并且会返回一些东西。在您的情况下,将返回 Unit()
,这就是存储在 tempTableB
中的内容。
关于scala - 为什么在显示运算符之后无法加入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322005/