PySpark RDD 的使用
文章目录
- PySpark RDD 的使用
-
- 1.1 RDD的创建
- 1.2 RDD算子
- 1.3 常用Transformation算子
-
- m a p 算子 \textcolor{CornflowerBlue}{map算子} map算子
- f l a t M a p 算子 \textcolor{CornflowerBlue}{flatMap算子} flatMap算子
- r e d u c e B y K e y 算子 \textcolor{CornflowerBlue}{reduceByKey算子} reduceByKey算子
- m a p V a l u e s 算子 \textcolor{CornflowerBlue}{mapValues算子} mapValues算子
- g r o u p B y 算子 \textcolor{CornflowerBlue}{groupBy算子} groupBy算子
- F i l t e r 算子 \textcolor{CornflowerBlue}{Filter算子} Filter算子
- d i s t i n c t 算子 \textcolor{CornflowerBlue}{distinct算子} distinct算子
- u n i o n 算子 \textcolor{CornflowerBlue}{union算子} union算子
- j o i n 算子 \textcolor{CornflowerBlue}{join算子} join算子
- i n t e r s e c t i o n 算子 \textcolor{CornflowerBlue}{intersection算子} intersection算子
- g l o m 算子 \textcolor{CornflowerBlue}{glom算子} glom算子
- g r o u p B y K e y 算子 \textcolor{CornflowerBlue}{groupByKey算子} groupByKey算子
- s o r t B y 算子 \textcolor{CornflowerBlue}{sortBy算子} sortBy算子
- s o r t B y K e y 算子 \textcolor{CornflowerBlue}{sortByKey算子} sortByKey算子
- 1.4 常用Action算子
-
- c o u n t B y K e y 算子 \textcolor{CornflowerBlue}{countByKey算子} countByKey算子
- c o l l e c t 算子 \textcolor{CornflowerBlue}{collect算子} collect算子
- r e d u c e 算子 \textcolor{CornflowerBlue}{reduce算子} reduce算子
- f o l d 算子 \textcolor{CornflowerBlue}{fold算子} fold算子
- f i r s t 算子 \textcolor{CornflowerBlue}{first算子} first算子
- t a k e S a m p l e 算子 \textcolor{CornflowerBlue}{takeSample算子} takeSample算子
- t a k e O r d e r d 算子 \textcolor{CornflowerBlue}{takeOrderd算子} takeOrderd算子
- f o r e a c h 算子 \textcolor{CornflowerBlue}{foreach算子} foreach算子
- s a v e A s T e x t F i l e 算子 \textcolor{CornflowerBlue}{saveAsTextFile算子} saveAsTextFile算子
- 1.5 分区操作算子
-
- m a p P a r t i t i o n s 算子 \textcolor{CornflowerBlue}{mapPartitions算子} mapPartitions算子 - Transformation
- f o r e a c h P a r t i t i o n 算子 \textcolor{CornflowerBlue}{foreachPartition算子} foreachPartition算子 - Action
- p a r t i t i o n B y 算子 \textcolor{CornflowerBlue}{partitionBy算子} partitionBy算子 - Transformation
- r e p a r t i t i o n 算子 \textcolor{CornflowerBlue}{repartition算子} repartition算子 - Transformation
- c o a l e c s e 算子 \textcolor{CornflowerBlue}{coalecse算子} coalecse算子 - Transformation
1.1 RDD的创建
Spark RDD的程序入口对象是SparkContext对象,使用RDD需要先创建一个SparkContext对象
from pyspark import SparkConf, SparkContext
import os
if __name__ == '__main__':
# 设定路径(你的路径)
os.environ['SPARK_HOME'] = '/opt/spark-3.2.3'
os.environ["PYSPARK_PYTHON"] = "/usr/bin/python3"
os.environ["PYSPARK_DRIVER_PYTHON"] = "/usr/bin/python3"
os.environ["HADOOP_CONF_DIR"] = "/opt/hadoop-2.7.2"
# 创建SparkContext对象(本地运行)
conf = SparkConf().setAppName("Test").setMaster("local[*]")
# 集群yarn运行
# conf = SparkConf().setAppName("Test").setMaster("yarn")
# 依赖文件同步提交
# conf.set("spark.submit.pyFiles", 依赖文件或多个文件打包成的zip)
sc = SparkContext(conf=conf)
随后我们可以选择两种RDD的创建方式
-
rdd = sc.parallelize(c: Iterable[T], numSlices: int | None = …) 本地对象转分布式RDD即手动创建
-
rdd = sc.textFile(name: str, minPartitions: int | None = …, use_unicode: bool = …) 读取文件或目录,本地、hdfs数据皆可
或 rdd = sc.wholeTextFiles(name: str, minPartitions: int | None = …, use_unicode: bool = …) 读取小文件或目录,本地、hdfs数据皆可
1.2 RDD算子
算子:分布式集合化对象的API叫做算子
- Transformation算子 返回值仍是一个RDD算子的,称之为转换算子
- Action算子 返回值不是RDD的就是Action算子
转换算子没有Action算子无法工作
1.3 常用Transformation算子
m a p 算子 \textcolor{CornflowerBlue}{map算子} map算子
功能:将RDD中的数据一条条按给定函数处理,返回RDD
例:words_with_one_rdd = words_rdd.map(lambda x: (x, 1)) # 将全是单词的RDD转换为(单词,1)的元组形式
f l a t M a p 算子 \textcolor{CornflowerBlue}{flatMap算子} flatMap算子
功能:对RDD先执行map操作,然后进行解除嵌套操作
解除嵌套:[[1, 2, 3], [4, 5, 6]] - > [1, 2, 3, 4, 5, 6]
r e d u c e B y K e y 算子 \textcolor{CornflowerBlue}{reduceByKey算子} reduceByKey算子
功能:针对KV型RDD,自动按照key分组,然后根据给定聚合逻辑。完成组内value的聚合
例:result_rdd = words_with_one_rdd.reduceByKey(lambda a, b: a + b) # 按照key分组,并加和key相同的
m a p V a l u e s 算子 \textcolor{CornflowerBlue}{mapValues算子} mapValues算子
功能:针对二元元组RDD,只对其Value进行map操作
g r o u p B y 算子 \textcolor{CornflowerBlue}{groupBy算子} groupBy算子
功能:将RDD的数据进行分组
groupyBy传入的函数的意思是按照谁来分组(返回谁即可)
例:result.groupBy(lambda t: t[0]) # 按照key来分组
后接rdd.map(lambda x: (x[0], list(x[1]))) 转换成[(key,list[]),]的形式
F i l t e r 算子 \textcolor{CornflowerBlue}{Filter算子} Filter算子
功能:过滤出想要的数据,返回值是true的得以保留,false的被丢弃
d i s t i n c t 算子 \textcolor{CornflowerBlue}{distinct算子} distinct算子
功能:对RDD数据进行去重,返回新RDD,一般当作无参
u n i o n 算子 \textcolor{CornflowerBlue}{union算子} union算子
功能:两个RDD合并成一个RDD返回,不去重
j o i n 算子 \textcolor{CornflowerBlue}{join算子} join算子
功能:对两个RDD执行join操作(可实现SQL的内/外连接),按照二元元组key来连接
用法:
-
rdd.join(other_rdd) # 内连接
-
rdd.leftOuterJoin(other_rdd) # 左外
-
rdd.rightOuterJoin(other_rdd) # 右外
i n t e r s e c t i o n 算子 \textcolor{CornflowerBlue}{intersection算子} intersection算子
功能:求两个RDD的交集,返回一个新的RDD