或者连接条件导致交叉连接

时间:2017-09-06 05:15:30

标签: sql apache-spark pyspark apache-spark-sql pyspark-sql

我正在尝试加入关于spark的两个数据集,我正在使用spark版本2.1,

ffplay -vf setpts=PTS/2 tcp://192.168.1.1:5555

但是它会导致交叉连接,如何连接两个表并获得匹配的记录?

我也尝试过左外连接,但它也迫使我改为交叉连接而不是??

2 个答案:

答案 0 :(得分:2)

试试这个方法

from pyspark.sql import SQLContext as SQC

sqc = SQC(sc)
x = [(1,2,3), (4,5,6), (7,8,9), (10,11,12), (13,14,15)]
y = [(1,4,5), (4,5,6), (10,11,16),(34,23,31), (56,14,89)]
x_df = sqc.createDataFrame(x,["x","y","z"])
y_df = sqc.createDataFrame(y,["x","y","z"])

cond = [(x_df.x == y_df.x) | ( x_df.y == y_df.y)]

x_df.join(y_df,cond, "inner").show()

输出

+---+---+---+---+---+---+
|  x|  y|  z|  x|  y|  z|
+---+---+---+---+---+---+
|  1|  2|  3|  1|  4|  5|
|  4|  5|  6|  4|  5|  6|
| 10| 11| 12| 10| 11| 16|
| 13| 14| 15| 56| 14| 89|
+---+---+---+---+---+---+

答案 1 :(得分:1)

加入两次:

 select * 
   from Tb1 
      inner join Tb2 
         on Tb1.key1=Tb2.key1 
      inner join Tb2 as Tb22 
         on Tb1.key2=Tb22.Key2  

或左加入:

 select * 
   from Tb1 
      left join Tb2 
         on Tb1.key1=Tb2.key1 
      left join Tb2 as Tb22 
         on Tb1.key2=Tb22.Key2