Scala 为什么在show操作符之后不能连接?
在我在Scala 为什么在show操作符之后不能连接?,scala,apache-spark,join,apache-spark-sql,Scala,Apache Spark,Join,Apache Spark Sql,在我在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: ov
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")
^
错误:重载方法值与可选项联接:
(右:org.apache.spark.sql.Dataset[\ux],joinExprs:org.apache.spark.sql.Column,joinType:String)org.apache.spark.sql.DataFrame
(右:org.apache.spark.sql.Dataset[173],使用columns:Seq[String],joinType:String)org.apache.spark.sql.DataFrame
无法应用于(单位,org.apache.spark.sql.Column,字符串)
表A.join(表B,$“idA”==$“idB”,“内部”)
^
为什么会这样?.show()
是一个函数,我们在Scala中称之为副作用。它打印到标准输出并返回Unit()
,就像println
例如:
val a = Array(1,2,3).foreach(println)
a: Unit = ()
在scala中,您可以假设所有内容都是函数,并将返回某些内容。在您的情况下,
Unit()
将被返回,这就是存储在atterableb
中的内容,因为@philantrover已经给出了详细的解释。因此,我将不作解释
如果您想查看诱惑B中的内容,那么您可以在如下所示分配它之后执行此操作
val tempTableB = tableB.groupBy("idB")
.agg(first("numB").as("numB"))
tempTableB.show
tableA.join(tempTableB, $"idA" === $"idB", "inner")
.drop("idA", "numA").show
那就行了