Spark之join、leftOuterJoin、rightOuterJoin及fullOuterJoin

Spark的join與mysql的join相似,mysql的join是將表與表之間鏈接查詢,spark中join是將RDD數據集進行鏈接,Spark主要有join、leftOuterJoin、rightOuterJoin及fullOuterJoin這4種鏈接mysql

join:至關於mysql的INNER JOIN,當join左右兩邊的數據集都存在時才返回sql

leftOuterJoin:至關於mysql的LEFT JOIN,leftOuterJoin返回數據集左邊的所有數據和數據集左邊與右邊有交集的數據
spa

rightOuterJoin:至關於mysql的RIGHT JOIN,rightOuterJoin返回數據集右邊的所有數據和數據集右邊與左邊有交集的數據code

fullOuterJoin:返回左右數據集的所有數據,左右有一邊不存在的數據以None填充orm

下面以代碼看個例子:blog

from pyspark import SparkConf, SparkContext conf = SparkConf() sc = SparkContext(conf=conf) def func_join(): a = sc.parallelize([("name", "Alice"), ("age", 20), ("job", "student"), ("fav", "basket")]) b = sc.parallelize([("name", "Bob"), ("age", 22), ("address", "WuHan")]) print("join:{}".format(a.join(b).collect())) print("leftOuterJoin:{}".format(a.leftOuterJoin(b).collect())) print("rightOuterJoin:{}".format(a.rightOuterJoin(b).collect())) print("fullOuterJoin:{}".format(a.fullOuterJoin(b).collect())) func_join() sc.stop() """ result: join:[('name', ('Alice', 'Bob')), ('age', (20, 22))] leftOuterJoin:[('fav', ('basket', None)), ('name', ('Alice', 'Bob')), ('job', ('student', None)), ('age', (20, 22))] rightOuterJoin:[('name', ('Alice', 'Bob')), ('age', (20, 22)), ('address', (None, 'WuHan'))] fullOuterJoin:[('fav', ('basket', None)), ('name', ('Alice', 'Bob')), ('job', ('student', None)), ('age', (20, 22)), ('address', (None, 'WuHan'))]
"""
相關文章
相關標籤/搜索