Pyspark加入表

时间:2017-09-29 14:16:31

标签: pyspark pyspark-sql

我是Pyspark的新人。我有表A'和表B'表B'我需要加入两个以获得表C'有人可以帮我吗?

我正在使用DataFrames ......

我不知道如何以正确的方式将这些表格加在一起......

表A

+--+----------+-----+    
|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()

1 个答案:

答案 0 :(得分:1)

它不是join更多的笛卡尔积(cross join

Spark 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)

Spark 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|
+---+---+----------+---+
相关问题