[Python] PySpark data calculation② ( RDD#flatMap method | RDD#flatMap syntax | code example )





1. RDD#flatMap method




1. Introduction of RDD#flatMap method


The RDD#map method can process the data elements in the RDD one by one, and the processing logic needs to be passed to the map function through external parameters;

The RDD#flatMap method is based on the RDD#map method, adding the function of "unnesting";

The RDD#flatMap method also receives a function as a parameter, which is applied to each element in the RDD and its nested sub-elements, and returns a new RDD object;


2. Unnesting


Unnested meaning: In the list below, each element is a list;

lst = [[1, 2], [3, 4, 5], [6, 7, 8]]

If the above list is unnested , the new list is as follows:

lst = [1, 2, 3, 4, 5, 6, 7, 8]

The RDD#flatMap method first processes each element in the RDD, and then flattens the calculation result into a new RDD object, that is, unnesting;

In this way, each element in the original RDD object corresponds to several elements in the new RDD object;


3. RDD#flatMap syntax description


RDD#flatMap syntax description:

newRDD = oldRDD.flatMap(lambda x: [element1, element2, ...])

In the old RDD object oldRDD, each element applies a lambda function, which returns multiple elements, and the returned multiple elements will be flattened into the new RDD object newRDD;


Code example:

# 将 字符串列表 转为 RDD 对象
rdd = sparkContext.parallelize(["Tom 18", "Jerry 12", "Jack 21"])

# 应用 map 操作,将每个元素 按照空格 拆分
rdd2 = rdd.flatMap(lambda element: element.split(" "))




2. Code example - RDD#flatMap method



Code example:

"""
PySpark 数据处理
"""

# 导入 PySpark 相关包
from pyspark import SparkConf, SparkContext
# 为 PySpark 配置 Python 解释器
import os
os.environ['PYSPARK_PYTHON'] = "Y:/002_WorkSpace/PycharmProjects/pythonProject/venv/Scripts/python.exe"

# 创建 SparkConf 实例对象 , 该对象用于配置 Spark 任务
# setMaster("local[*]") 表示在单机模式下 本机运行
# setAppName("hello_spark") 是给 Spark 程序起一个名字
sparkConf = SparkConf() \
    .setMaster("local[*]") \
    .setAppName("hello_spark")

# 创建 PySpark 执行环境 入口对象
sparkContext = SparkContext(conf=sparkConf)

# 打印 PySpark 版本号
print("PySpark 版本号 : ", sparkContext.version)

# 将 字符串列表 转为 RDD 对象
rdd = sparkContext.parallelize(["Tom 18", "Jerry 12", "Jack 21"])

# 应用 map 操作,将每个元素 按照空格 拆分
rdd2 = rdd.flatMap(lambda element: element.split(" "))

# 打印新的 RDD 中的内容
print(rdd2.collect())

# 停止 PySpark 程序
sparkContext.stop()

Results of the :

Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
23/07/31 23:02:58 WARN Shell: Did not find winutils.exe: java.io.FileNotFoundException: java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset. -see https://wiki.apache.org/hadoop/WindowsProblems
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
23/07/31 23:02:59 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
PySpark 版本号 :  3.4.1
['Tom', '18', 'Jerry', '12', 'Jack', '21']

Process finished with exit code 0

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132030548