PySpark | PySpark库 | 本机开发环境搭建 | 分布式代码执行分析

本文详细介绍了PySpark库的框架与类库,以及如何在Window系统下搭建本机PySpark开发环境,包括配置PySpark、Pycharm的本地与远程解释器。深入探讨了Spark的分布式代码执行分析,讲解了SparkContext在程序中的重要性,以及Wordcount代码实战。重点解析了PySpark在分布式环境中的执行原理,强调了Driver和Executor的角色分工。

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


传送门:


一、PySpark库

1.框架与类库
  • 类库:—堆别人写好的代码,你可以导入进行使用。PySpark就是一个类库。
  • 框架:可以独立运行,并提供编程结构的—种软件产品。Spark就是一个独立的框架。
2.什么是PySpark

  PySpark是可以在Python代码中:import pyspark来进行调用的。PySpark 是Spark官方提供的一个Python类库,内置了完全的Spark API,可以通过PySpark类库来编写Spark应用程序,并将其提交到Spark集群中运行。
PySpark类库和标准Spark框架对比:
在这里插入图片描述

3. PySpark安装

  在合适虚拟环境下,执行如下命令即可安装:

(shayun)C:\Users\admin>pip install pyspark -i https://pypi.tuna.tsinghua.edu.cn/simple

二、本机开发环境搭建

1.本机PySpark环境配置(Window系统下)
  1. 将课程资料中提供的: hadoop-3.3.0 文件, 复制到一个地方, 比如E:\softs\hadoop-3.0.0
  2. 将文件夹内bin内的hadoop.dll复制到: C:\Windows\System32里面去
  3. 配置HADOOP_HOME环境变量指向 hadoop-3.3.0文件夹的路径, 如下图
    在这里插入图片描述

配置这些的原因是:
hadoop设计用于linux运行, 我们写spark的时候,在windows上开发不可避免的会用到部分hadoop功能。为了避免在windows上报错, 我们给windows打补丁。

2.Pycharm本地与远程解释器配置

配置本地解释器
在这里插入图片描述
在这里插入图片描述
配置远程SSH Linux解释器

  1. 设置远程SSH python pySpark 环境
    在这里插入图片描述
  2. 添加新的远程连接
    在这里插入图片描述
  3. 设置虚拟机Python环境路径
    在这里插入图片描述
  4. 将Windows文件夹与Linux文件夹进行同步
    在这里插入图片描述
3.应用入口:SparkContext

  Spark Application程序入口为:SparkContext,任何一个应用首先需要构建SparkCo

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
### 构建完全分布式环境中的 PySpark #### 准备工作 为了在完全分布式的环境中设置 PySpark,需确保集群内的每台机器都安装并配置好必要的软件组件。这包括 Java、PythonSpark 的安装。 #### 安装依赖项 首先,在所有节点上安装 Python 及其开发工具包。接着,安装 Apache Spark 并将其解压到指定目录下。对于每个节点而言,还需下载 PySpark 并通过 pip 工具完成安装[^1]。 ```bash pip install pyspark ``` #### 配置 Spark 环境变量 编辑 `~/.bashrc` 文件来定义 SPARK_HOME 和 PYTHONPATH 环境变量: ```bash export SPARK_HOME=/path/to/spark export PYTHONPATH=$SPARK_HOME/python:$PYTHONPATH source ~/.bashrc ``` #### 设置 Master 节点 选择一台服务器作为主控节点(Master),启动 Spark Standalone 模式下的 master 实例: ```bash $SPARK_HOME/sbin/start-master.sh ``` 记录生成的 spark://master-url 地址用于后续 worker 注册连接。 #### 启动 Worker 节点 在其他计算资源充足的机器上执行如下命令加入集群成为 worker 成员之一: ```bash $SPARK_HOME/sbin/start-slave.sh <spark://master-url> ``` 此时便形成了一个多机协作处理数据的任务调度框架体系结构。 #### 测试 PySpark Shell 连接 通过 PySpark shell 来验证是否成功接入远程集群: ```python from pyspark.sql import SparkSession spark = ( SparkSession.builder.appName("TestApp") .config('spark.master', 'spark://<master-ip>:7077') .getOrCreate() ) print(spark.sparkContext.getConf().getAll()) ``` 上述代码创建了一个名为 "TestApp" 的应用程序实例,并指定了要使用的 master URL。最后打印出当前会话的所有配置参数以确认一切正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

幼稚的人呐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值