Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pyspark Pypark连接表_Pyspark_Pyspark Sql - Fatal编程技术网

Pyspark Pypark连接表

Pyspark Pypark连接表,pyspark,pyspark-sql,Pyspark,Pyspark Sql,我是Pypark的新手。我有'Table A'和'Table B',我需要将两者连接起来才能得到'Table C'。有人能帮我吗 我正在使用数据帧 我不知道如何以正确的方式将这些表连接在一起 表A: +--+----------+-----+ |id|year_month| qt | +--+----------+-----+ | 1| 2015-05| 190 | | 2| 2015-06| 390 | +--+----------+-----+ +---------+--

我是Pypark的新手。我有'Table A'和'Table B',我需要将两者连接起来才能得到'Table C'。有人能帮我吗

我正在使用数据帧

我不知道如何以正确的方式将这些表连接在一起

表A

+--+----------+-----+    
|id|year_month| qt  |
+--+----------+-----+
| 1|   2015-05| 190 |
| 2|   2015-06| 390 |
+--+----------+-----+
+---------+-----+
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   |
+---------+-----+
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()
表B

+--+----------+-----+    
|id|year_month| qt  |
+--+----------+-----+
| 1|   2015-05| 190 |
| 2|   2015-06| 390 |
+--+----------+-----+
+---------+-----+
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   |
+---------+-----+
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()
表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   |
+--+----------+-----+-----+
代码

+--+----------+-----+    
|id|year_month| qt  |
+--+----------+-----+
| 1|   2015-05| 190 |
| 2|   2015-06| 390 |
+--+----------+-----+
+---------+-----+
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   |
+---------+-----+
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()

它不是真正的
连接
而是笛卡尔积(
交叉连接

Spark 2

import pyspark.sql.函数作为psf
表A.交叉连接(表B)\
.withColumn(
“qt”,
psf.何时(表B.年\月==表A.年\月,psf.列(“qt”))。否则(0))\
.下降(表A.年\月)
Spark 1.6

tableA.join(tableB)\
.withColumn(
“qt”,
psf.何时(表B.年\月==表A.年\月,psf.列(“qt”))。否则(0))\
.下降(表A.年\月)
+---+---+----------+---+
|id | qt |年|月| 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|
+---+---+----------+---+