2015-11-16 10 views
7

Sto usando Spark 1.3 e desidero unirmi su più colonne utilizzando l'interfaccia python (SparkSQL)SparkSQL uniscono sulla colonna multipla

le seguenti opere:

ho registrarli come tabelle temporanee.

numeric.registerTempTable("numeric") 
Ref.registerTempTable("Ref") 

test = numeric.join(Ref, numeric.ID == Ref.ID, joinType='inner') 

Vorrei ora unirmi in base a più colonne.

ottengo SyntaxError: sintassi non valida con questo:

test = numeric.join(Ref, 
    numeric.ID == Ref.ID AND numeric.TYPE == Ref.TYPE AND 
    numeric.STATUS == Ref.STATUS , joinType='inner') 

risposta

16

Si dovrebbe usare &/| operatori e stare attenti a precedenza degli operatori:

df1 = sqlContext.createDataFrame(
    [(1, "a", 2.0), (2, "b", 3.0), (3, "c", 3.0)], 
    ("x1", "x2", "x3")) 

df2 = sqlContext.createDataFrame(
    [(1, "f", -1.0), (2, "b", 0.0)], ("x1", "x2", "x3")) 

df = df1.join(df2, (df1.x1 == df2.x1) & (df1.x2 == df2.x2)) 
df.show() 

## +---+---+---+---+---+---+ 
## | x1| x2| x3| x1| x2| x3| 
## +---+---+---+---+---+---+ 
## | 2| b|3.0| 2| b|0.0| 
## +---+---+---+---+---+---+