2017-09-29 10 views
0

私はPysparkで新しいです。私は 'テーブルA'と 'テーブルB'を持っており、両方とも 'テーブルC'を取得する必要があります。誰でも助けてくれるの?私が参加する方法がわからないPyspark Join Table

私はデータフレームを使用してい

...そのすべて一緒に正しい方法でテーブル...

+--+----------+-----+  
|id|year_month| qt | 
+--+----------+-----+ 
| 1| 2015-05| 190 | 
| 2| 2015-06| 390 | 
+--+----------+-----+ 

表B

+---------+-----+ 
year_month| sem | 
+---------+-----+ 
| 2016-01| 1 | 
| 2015-02| 1 | 
| 2015-03| 1 | 
| 2016-04| 1 | 
| 2015-05| 1 | 
| 2015-06| 1 | 
| 2016-07| 2 | 
| 2015-08| 2 | 
| 2015-09| 2 | 
| 2016-10| 2 | 
| 2015-11| 2 | 
| 2015-12| 2 | 
+---------+-----+ 

表C:ザ・列を追加しても、行を追加参加 ...

+--+----------+-----+-----+  
|id|year_month| qt | sem | 
+--+----------+-----+-----+ 
| 1| 2015-05 | 0 | 1 | 
| 1| 2016-01 | 0 | 1 | 
| 1| 2015-02 | 0 | 1 | 
| 1| 2015-03 | 0 | 1 | 
| 1| 2016-04 | 0 | 1 | 
| 1| 2015-05 | 190 | 1 | 
| 1| 2015-06 | 0 | 1 | 
| 1| 2016-07 | 0 | 2 | 
| 1| 2015-08 | 0 | 2 | 
| 1| 2015-09 | 0 | 2 | 
| 1| 2016-10 | 0 | 2 | 
| 1| 2015-11 | 0 | 2 | 
| 1| 2015-12 | 0 | 2 | 
| 2| 2015-05 | 0 | 1 | 
| 2| 2016-01 | 0 | 1 | 
| 2| 2015-02 | 0 | 1 | 
| 2| 2015-03 | 0 | 1 | 
| 2| 2016-04 | 0 | 1 | 
| 2| 2015-05 | 0 | 1 | 
| 2| 2015-06 | 390 | 1 | 
| 2| 2016-07 | 0 | 2 | 
| 2| 2015-08 | 0 | 2 | 
| 2| 2015-09 | 0 | 2 | 
| 2| 2016-10 | 0 | 2 | 
| 2| 2015-11 | 0 | 2 | 
| 2| 2015-12 | 0 | 2 | 
+--+----------+-----+-----+ 

コード

from pyspark import HiveContext 
sqlContext = HiveContext(sc) 

lA = [(1,"2015-05",190),(2,"2015-06",390)] 
tableA = sqlContext.createDataFrame(lA, ["id","year_month","qt"]) 
tableA.show() 

lB = [("2016-01",1),("2015-02",1),("2015-03",1),("2016-04",1), 
     ("2015-05",1),("2015-06",1),("2016-07",2),("2015-08",2), 
     ("2015-09",2),("2016-10",2),("2015-11",2),("2015-12",2)] 
tableB = sqlContext.createDataFrame(lB,["year_month","sem"]) 
tableB.show() 

答えて

0

それは本当に多くのjoinデカルト積(cross join) ではありません

スパーク2

import pyspark.sql.functions as psf 
tableA.crossJoin(tableB)\ 
    .withColumn(
     "qt", 
     psf.when(tableB.year_month == tableA.year_month, psf.col("qt")).otherwise(0))\ 
    .drop(tableA.year_month) 

スパーク1.6

tableA.join(tableB)\ 
    .withColumn(
     "qt", 
     psf.when(tableB.year_month == tableA.year_month, psf.col("qt")).otherwise(0))\ 
    .drop(tableA.year_month) 

+---+---+----------+---+ 
| id| qt|year_month|sem| 
+---+---+----------+---+ 
| 1| 0| 2015-02| 1| 
| 1| 0| 2015-03| 1| 
| 1|190| 2015-05| 1| 
| 1| 0| 2015-06| 1| 
| 1| 0| 2016-01| 1| 
| 1| 0| 2016-04| 1| 
| 1| 0| 2015-08| 2| 
| 1| 0| 2015-09| 2| 
| 1| 0| 2015-11| 2| 
| 1| 0| 2015-12| 2| 
| 1| 0| 2016-07| 2| 
| 1| 0| 2016-10| 2| 
| 2| 0| 2015-02| 1| 
| 2| 0| 2015-03| 1| 
| 2| 0| 2015-05| 1| 
| 2|390| 2015-06| 1| 
| 2| 0| 2016-01| 1| 
| 2| 0| 2016-04| 1| 
| 2| 0| 2015-08| 2| 
| 2| 0| 2015-09| 2| 
| 2| 0| 2015-11| 2| 
| 2| 0| 2015-12| 2| 
| 2| 0| 2016-07| 2| 
| 2| 0| 2016-10| 2| 
+---+---+----------+---+