具有部分相同、部分不同列名的两个Dataset按照部分相同、部分不同列相等进行join操作,有以下几种方式:
val df1 = Seq((1, 2, 3),(1, 1, 1)).toDF("a", "b", "c")
val df2 = Seq((1, 2, 4),(2, 2, 2)).toDF("a", "b1", "d")
df1.show
+---+---+---+
| a| b| c|
+---+---+---+
| 1| 2| 3|
| 1| 1| 1|
+---+---+---+
df2.show
+---+---+---+
| a| b1| d|
+---+---+---+
| 1| 2| 4|
| 2| 2| 2|
+---+---+---+
//join条件:df1("a") == df2("a") && df1("b") == df2("b1")
//若是直接join会报错:org.apache.spark.sql.AnalysisException: Reference 'a' is ambiguous, could be:...
df1.join(df2, col("a") === col("a") && col("b") === col("b1"), "outer").show
//可以改为这样:
df1.join(df2, df1("a") === df2("a") && col("b") === col("b1"), "outer").show
+----+----+----+----+----+----+
| a| b| c| a| b1| d|
+----+----+----+----+----+----+
|null|null|null| 2| 2| 2|
| 1| 2| 3| 1| 2| 4|
| 1| 1| 1|null|null|null|
+----+----+----+----+----+----+
//当然也可以将其中一个Dataset的列改名,改为都相同或都不同,再用上面的方法join
df1.join(df2.withColumnRenamed("b1", "b"), Seq("a", "b"), "outer").show
+---+---+----+----+
| a| b| c| d|
+---+---+----+----+
| 2| 2|null| 2|
| 1| 2| 3| 4|
| 1| 1| 1|null|
+---+---+----+----+
//还可以用Dataset的as方法(与alias方法等效),给Dataset命名,然后消除歧义。(Dataset的别名类似SQL中表的别名)
df1.alias("df1")
.join(df2.as("df2"), col("df1.a") === col("df2.a") && col("b") === col("b1"), "outer")
.show
+----+----+----+----+----+----+
| a| b| c| a| b1| d|
+----+----+----+----+----+----+
|null|null|null| 2| 2| 2|
| 1| 2| 3| 1| 2| 4|
| 1| 1| 1|null|null|null|
+----+----+----+----+----+----+
//如果只想保留df2的a列:
val t = df1.alias("df1")
.join(df2.as("df2"), col("df1.a") === col("df2.a") && col("b") === col("b1"), "outer")
.drop(col("df1.a")).show
+----+----+----+----+----+
| b| c| a| b1| d|
+----+----+----+----+----+
|null|null| 2| 2| 2|
| 2| 3| 1| 2| 4|
| 1| 1|null|null|null|
+----+----+----+----+----+
补充:
Dataset的as方法(与alias方法等效):为Dataset对象起别名,Dataset的别名类似SQL中表的别名。
val df = Seq((1, 2),(1, 1)).toDF("a", "b")
df.select("a").show
+---+
| a|
+---+
| 1|
| 1|
+---+
df.select("df.a").show
//报错:org.apache.spark.sql.AnalysisException: cannot resolve 'df.a' given input columns: [a, b];
df.as("df").select("df.a").show
+---+
| a|
+---+
| 1|
| 1|
+---+
本文由【张】发布于开源中国,原文链接:https://my.oschina.net/u/2000675/blog/3106422