2017-07-26 21 views
2

以下代码正常工作,直到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") 
        ^

这是为什么表现如此?

回答

3

.show()是一个函数,我们在Scala中称之为副作用。它打印到标准输出,并返回Unit(),就像println

例子:

val a = Array(1,2,3).foreach(println) 
a: Unit =() 

在Scala中,你可以假设这一切都是一个函数,将返回的东西。在你的情况下,Unit()正在退回,这就是存储在tempTableB

2

由于@philantrovert已经回答了很多详细的解释。所以我不会解释。

如果您想查看tempTableB中的内容,您可以执行哪些操作,然后您可以在按如下方式分配后执行此操作。

val tempTableB = tableB.groupBy("idB") 
    .agg(first("numB").as("numB")) 

tempTableB.show 

tableA.join(tempTableB, $"idA" === $"idB", "inner") 
.drop("idA", "numA").show 

它应该工作,然后