PySpark实战

本文详细介绍了Spark的基本概念、PySpark的安装与编程模型,以及如何使用RDD进行数据处理,包括map、flatMap、reduceByKey等操作。还涵盖了数据输入、计算和输出的各种方法,以及一个搜索引擎日志分析的实战案例。

一、前言介绍

1、Spark是什么

2、Python On Spark

3、PySpark

4、小结

二、基础准备

1、PySpark库的安装

2、构建PySpark执行环境入口对象

3、PySpark的编程模型

3、小结

三、数据输入

1、RDD对象

(1)Python数据容器转RDD对象

代码演示:

(2)读取文件转RDD对象

代码演示:

2、小结

四、数据计算

1、RDD的map方法

代码演示:

或者使用匿名函数 lambda:

会报错,因为PySpark找不到Python解释器在哪里:

在前面加上如下代码,告诉spark运行时在哪里找到python解释器:

运行成功:

链式调用:每次计算返回的结果都是同一个对象 rdd

运行成功:

小结:

2、RDD的flatMap算子

代码演示:

from pyspark import SparkConf,SparkContext

import  os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_flatMap")

sc=SparkContext(conf=conf)

rdd=sc.parallelize(["app opp python","xiaohong min ass","asd hhj sd"])

print(rdd.flatMap(lambda x:x.split(" ")).collect())

sc.stop()

运行结果:

小结:

3、RDD的reduceByKey算子

代码演示:

from pyspark import SparkConf,SparkContext

import os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_reduceByKey")
sc=SparkContext(conf=conf)

rdd=sc.parallelize([("a",1),("a",2),("b",1),("b",1),("b",2)])
print(rdd.reduceByKey(lambda a,b:a+b).collect())

sc.stop()

运行结果:

小结:

4、练习案例

代码演示:

from pyspark import SparkConf,SparkContext

import os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_spark_lianxi")

sc=SparkContext(conf=conf)

rdd=sc.textFile("d:/a.txt")

rdd1=rdd.flatMap(lambda x:x.split(" "))

rdd2=rdd1.map(lambda x:(x,1))

print(rdd2.reduceByKey(lambda x,y:x+y).collect())

sc.stop()

运行结果:

5、RDD的Filter方法

代码演示:

from pyspark import SparkConf,SparkContext

import os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_Filter")

sc=SparkContext(conf=conf)

rdd=sc.parallelize([1,2,3,4,5])
print(rdd.filter(lambda x:x%2==0).collect())

sc.stop()

运行结果:

小结:

6、RDD的distinct算子

代码演示:

from pyspark import SparkConf,SparkContext

import os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_distinct")

sc=SparkContext(conf=conf)

rdd=sc.parallelize([1,2,3,1,3,3,4,5,3,2,4,21])

print(rdd.distinct().collect())

sc.stop()

运行结果:

小结:

7、RDD的sortBy算子

代码演示:

from pyspark import SparkConf,SparkContext

import os
os.environ["PYSPARK_PYTHON"]="D:\SMJ\Documents\下载\Python\python3.11.5\python.exe"

conf=SparkConf().setMaster("local[*]").setAppName("test_spark_lianxi")

sc=SparkContext(conf=conf)

rdd=sc.textFile("d:/a.txt")

rdd1=rdd.flatMap(lambda x:x.split(" "))

rdd2=rdd1.map(lambda x:(x,1))

rdd3=rdd2.reduceByKey(lambda x,y:x+y).sortBy(lambda x:x[1],ascending=False,numPartitions=1)

print(rdd3.collect())

sc.stop()

运行结果:

小结:

8、练习案例

代码演示1:

运行结果1:

代码演示2:

运行结果2:

代码演示3:

运行结果3:

五、数据输出

1、输出为python对象

(1)collect()

代码演示:

运行结果:

(2)reduce()

代码演示:

运行结果:

(3)take()

代码演示:

运行结果:

(4)count()

代码演示:

运行结果:

(5)小结

2、输出到文件中

(1)saveAsTextFile()

报错:

(2)配置依赖

(3)修改RDD分区

运行成功:

或者:

运行成功:

(4)小结

六、综合案例:搜索引擎日志分析

代码演示1:

运行结果1:

代码演示2:

运行结果2:

代码演示3:

运行结果3:

代码演示4:

运行结果4:

About This Book, Learn why and how you can efficiently use Python to process data and build machine learning models in Apache Spark 2.0Develop and deploy efficient, scalable real-time Spark solutionsTake your understanding of using Spark with Python to the next level with this jump start guide, Who This Book Is For, If you are a Python developer who wants to learn about the Apache Spark 2.0 ecosystem, this book is for you. A firm understanding of Python is expected to get the best out of the book. Familiarity with Spark would be useful, but is not mandatory., What You Will Learn, Learn about Apache Spark and the Spark 2.0 architectureBuild and interact with Spark DataFrames using Spark SQLLearn how to solve graph and deep learning problems using GraphFrames and TensorFrames respectivelyRead, transform, and understand data and use it to train machine learning modelsBuild machine learning models with MLlib and MLLearn how to submit your applications programmatically using spark-submitDeploy locally built applications to a cluster, In Detail, Apache Spark is an open source framework for efficient cluster computing with a strong interface for data parallelism and fault tolerance. This book will show you how to leverage the power of Python and put it to use in the Spark ecosystem. You will start by getting a firm understanding of the Spark 2.0 architecture and how to set up a Python environment for Spark., You will get familiar with the modules available in PySpark. You will learn how to abstract data with RDDs and DataFrames and understand the streaming capabilities of PySpark. Also, you will get a thorough overview of machine learning capabilities of PySpark using ML and MLlib, graph processing using GraphFrames, and polyglot persistence using Blaze. Finally, you will learn how to deploy your applications to the cloud using the spark-submit command., By the end of this book, you will have established a firm understanding of the Spark Python API and how it can be used t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python_198

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值