Python大数据之PySpark

本文介绍了PySpark的基本概念、开发环境搭建、工作机制,重点讲解了PySpark的批处理和SQL操作,包括数据帧的创建、查看、读写以及与Hive数据仓库的连接。此外,还提到了Pandas On Spark以及Apache Arrow在PySpark中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



1、Spark与PySpark


Apache Spark是一种用于大规模数据处理的多语言分布式引擎,用于在单节点机器或集群上执行数据工程、数据科学和机器学习

Spark官网:https://spark.apache.org/

按照官网描述,Spark关键特征包括:

  • 批/流处理

    Spark支持您使用喜欢的语言:Python、SQL、Scala、Java或R,统一批量和实时流处理数据

  • SQL分析

    执行快速、分布式ANSI SQL查询以进行BI仪表板和临时报告。运行速度比大多数数据仓库更快

  • 大规模数据科学

    对PB级数据执行探索性数据分析(EDA),而无需采用降采样(缩减采样)

  • 机器学习

    在笔记本电脑上训练机器学习算法,并使用相同的代码扩展到数千台机器的容错集群

Apache Spark与多种框架集成,有助于将其扩展到数千台机器:

在这里插入图片描述

Apache Spark支持的存储和基础设施有:

在这里插入图片描述

根据Spark官网,Spark支持Python语言编程

PySpark是Spark为Python开发者提供的API。它不仅允许您使用Python API编写Spark应用程序,还提供PySpark shell用于在分布式环境中交互式分析数据。PySpark支持Spark的大部分功能,例如Spark Core、Spark SQL、Spark Streaming、Spark MLlib

在这里插入图片描述

  • Spark Core是Spark平台的底层通用执行引擎,所有其他功能都构建在其之上。它提供了RDD(弹性分布式数据集)和内存计算能力

  • Spark SQL和DataFrame是用于结构化数据处理的Spark模块。它提供了一个称为DataFrame的编程抽象,也可以充当分布式SQL查询引擎

  • Spark中的流功能运行在Spark之上,支持跨流数据和历史数据的强大交互式和分析应用程序,同时继承了Spark的易用性和容错特性

  • MLlib构建于Spark之上,是一个可扩展的机器学习库,它提供了一组统一的高级API,可帮助用户创建和调整实用的机器学习管道

PySpark依赖于Py4J,Py4J是一个用Python和Java编写的库。通过Py4J,Python程序能够动态访问Java虚拟机中的Java对象,Java程序也能够回调Python对象

PySpark是Spark官方提供的基于Python语言开发的类库,仅支持在本地Local模式环境下供Python用户开发使用

PySpark官网文档:https://spark.apache.org/docs/3.1.2/api/python/getting_started

2、PySpark开发环境搭建


前提:确保已经安装配置了Java和Scala

1)Hadoop的Windows环境配置

由于Hadoop主要基于Linux编写,而Hive、Spark等依赖于Hadoop,因此,Hadoop在Windows上运行需要winutils.exehadoop.dll等环境文件的支持,winutils.exehadoop.dll等文件必须放置在bin目录下,主要用于模拟Linux下的目录环境

官方文档说明:https://cwiki.apache.org/confluence/display/HADOOP2/WindowsProblems

在这里插入图片描述

配置Hadoop的Windows环境变量:

HADOOP_HOME=D:\Software\Hadoop\hadoop-2.7.7
Path=%HADOOP_HOME%\bin

hadoop.dllwinutils.exe可不用拷贝)文件拷贝到C:\Windows\System32目录中,重启电脑

PS:各版本hadoop.dllwinutils.exe下载:https://github.com/cdarlint/winutils/tree/master/hadoop-3.1.2

PySpark开发环境搭建常见问题及解决:传送门

2)PySpark环境搭建

在PyCharm终端安装PySpark模块:

pip install pyspark==3.1.2

配置PySpark的Windows环境变量:

PYSPARK_PYTHON=python
PYSPARK_DRIVER_PYTHON=jupyter
PYSPARK_DRIVER_PYTHON_OPTS=lab

如果您未配置PySpark的Windows环境变量,将出现如下报错:

在这里插入图片描述

解决:至少确保配置PYSPARK_PYTHON=python该Windows系统环境变量(配置后需要重启PyCharm)

3)PySpark环境验证

Windows+R,输入cmd回车执行如下命令:

spark-shell

在这里插入图片描述

成功打印Spark版本说明环境配置成功

3、PySpark的工作机制

PySpark

Spark的主要组件包括Driver、Executor、Cluster Manager、Task等

其中Driver是客户端任务,Executor负责执行具体Task,用户将Spark任务程序Driver提交给资源管理系统(如Yarn、K8s等),Spark会将程序分解为一个个的Task交给Executor执行

为了不影响现有Spark的运行架构,Spark在外围包装了一层Python的API,借助Py4j实现Python和Java的交互,即通过Py4j将PySpark代码“解析”到JVM中去运行

在Driver端,Python通过Py4j来调用Java方法,将用户使用Python写的程序映射到JVM中,比如,用户在PySpark中实例化一个Python的SparkContext对象,最终会在JVM中实例化Scala的SparkContext对象

在Executor端,都启动一个Python守护进程,当Task收到任务请求后,交给底层的Python进程去执行

在这里插入图片描述

4、PySpark批处理


SparkConf是Spark批处理程序的主入口

PySpark批处理使用案例:单词统计

from pyspark import SparkContext, SparkConf

conf = SparkConf().setMaster("local[*]").setAppName("WordCount")
sc = SparkContext(conf=conf)
rdd_lines = sc.textFile(r'C:\Users\cc\Desktop\temp\HarryPotter.txt')
rdd_lines.flatMap(lambda line: re.split("\\s+", re.sub("\\W+", " ", line.lower())))\
    .filter(lambda w: w.strip() != "")\
    .map(lambda w: (w, 1))\
    .reduceByKey(lambda v1, v2: v1+v2)\
    .sortBy(lambda t
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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值