Python大数据之PySpark(二)PySpark安装_支持 依赖项 ‘python pyspark‘ 的插件 spark 当前尚未安装。

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Jupyter环境设置

监控页面

  • 4040的端口
  • image-20210907164238934
  • image-20210907164121516

运行圆周率

  • 回顾Hadoop中可以使用
  • hadoop jar xxxx.jar 100
  • yarn jar xxxx.jar 1000
  • 跑的mr的任务
  • Spark中也有对应的提交任务的代码
  • spark-submit 提交圆周率的计算代码 */examples/src/main/python/pi.py*
  • 提交的命令:

bin/spark-submit --master local[2] /export/server/spark/examples/src/main/python/pi.py 10
或者

# 基于蒙特卡洛方法求解的Pi,需要参数10,或100代表的次数
bin/spark-submit \
--master local[2] \
/export/server/spark/examples/src/main/python/pi.py  \
10

  • image-20210907165254363
  • image-20210907165310163
  • 蒙特卡洛方法求解PI
  • image-20210907170654900
  • 采用的扔飞镖的方法,在极限的情况下,可以用落入到圆内的次数除以落入正方形内的次数
  • hadoop jar /export/server/hadoop-3.3.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.0.jar pi 10 10
  • hadoop提交任务中使用 第一个10代表是map任务,第二10代表每个map任务投掷的次数
  • spark-submit的提交的参数10的含义是投掷的次数
  • 简单的py代码

def pi(times): # times的意思是落入到正方形的次数
x_time = 0
for i in range(times):

有多少落入到圆内

x = random.random()
y = random.random()
if x * x + y * y <= 1:
x_time += 1
return x_time / times * 4.0
print(pi(10000000))#3.1410412

环境搭建-Standalone

  • 完成了Spark的local环境搭建
  • 完成了Spark的PySpark的local环境搭建
  • 基于PySpark完成spark-submit的任务提交

Standalone 架构

  • image-20210907173358686
  • 如果修改配置,如何修改?
  • 1-设定谁是主节点,谁是从节点
    • node1是主节点,node1,node2,node3是从节点
  • 2-需要在配置文件中声明,
    • 那个节点是主节点,主节点的主机名和端口号(通信)
    • 那个节点是从节点,从节点的主机名和端口号
  • 3-现象:进入到spark-shell中或pyspark中,会开启4040的端口webui展示,但是一旦交互式命令行退出了,wenui无法访问了,需要具备Spark的历史日志服务器可以查看历史提交的任务

角色分析

Master角色,管理节点, 启动一个名为Master的进程, *Master进程有且仅有1个*(HA模式除外)

Worker角色, 干活节点,启动一个名为 Worker的进程., Worker进程****最少1个, 最多不限制****

Master进程负责资源的管理, 并在有程序运行时, 为当前程序创建管理者Driver

Driver:驱动器,使用SparkCOntext申请资源的称之为Driver,告诉任务需要多少cpu或内存

Worker进程负责干活, 向Master汇报状态, 并听从程序Driver的安排,创建Executor干活

在Worker中有Executor,Executor真正执行干活

集群规划

谁是Master 谁是Worker

node1:master/worker

node2:slave/worker

node3:slave/worker

为每台机器安装Python3

安装过程

  • 1-配置文件概述
  • spark-env.sh 配置主节点和从节点和历史日志服务器
  • workers 从节点列表
  • spark-default.conf spark框架启动默认的配置,这里可以将历史日志服务器是否开启,是否有压缩等写入该配置文件
  • image-20210907174431620
  • 2-安装过程
  • 2-1 修改workers的从节点配置文件
  • image-20210907175314177
  • 2-2 修改spark-env.sh配置文件
  • hdfs dfs -mkdir -p /sparklog/
  • image-20210907175633633
  • 2-3 修改spark-default.conf配置文件
  • image-20210907175738338
  • 2-4 配置日志显示级别(省略)

测试

  • image-20210907180432917
  • WebUi
  • image-20210907181051096
  • (1)Spark-shell
  • bin/spark-shell --master spark://node1:7077
  • image-20210907181018963
  • (2)pyspark
  • 前提:需要在三台机器上都需要安装Anaconda,并且安装PySpark3.1.2的包
  • 步骤:
  • 如果使用crt上传文件一般使用rz命令,yum install -y lrzsz
  • 1-在3台虚拟机上准备anconda
  • 2-安装anaconda,sh anaconda.sh
  • 3-安装pyspark,这里注意环境变量不一定配置,直接进去文件夹也可以
  • 4-测试
  • 调用:bin/pyspark --master spark://node1:7077
  • image-20210908104715004
  • image-20210908104843353
  • (3)spark-submit
  • image-20210908105555461
  • 
    
    





#基于Standalone的脚本
#driver申请作业的资源,会向–master集群资源管理器申请
#执行计算的过程在worker中,一个worker有很多executor(进程),一个executor下面有很多task(线程)
bin/spark-submit
–master spark://node1:7077
–driver-memory 512m
–executor-memory 512m
–conf “spark.pyspark.driver.python=/root/anaconda3/bin/python3”
–conf “spark.pyspark.python=/root/anaconda3/bin/python3”
/export/server/spark/examples/src/main/python/pi.py
10



  • 完毕





Spark 应用架构

  • 两个基础driver和executor
  • image-20210908110103556
  • image-20210908110416482
  • image-20210908110915547
  • 用户程序从最开始的提交到最终的计算执行,需要经历以下几个阶段:

1)、用户程序创建 SparkContext 时,新创建的 SparkContext 实例会连接到 ClusterManager。 Cluster Manager 会根据用户提交时设置的 CPU 和内存等信息为本次提交分配计算资源,启动 Executor。

2)、Driver会将用户程序划分为不同的执行阶段Stage,每个执行阶段Stage由一组完全相同Task组成,这些Task分别作用于待处理数据的不同分区。在阶段划分完成和Task创建后, Driver会向Executor发送 Task;

3)、Executor在接收到Task后,会下载Task的运行时依赖,在准备好Task的执行环境后,会开始执行Task,并且将Task的运行状态汇报给Driver;

4)、Driver会根据收到的Task的运行状态来处理不同的状态更新。 Task分为两种:一种是Shuffle Map Task,它实现数据的重新洗牌,洗牌的结果保存到Executor 所在节点的文件系统中;另外一种是Result Task,它负责生成结果数据;

5)、Driver 会不断地调用Task,将Task发送到Executor执行,在所有的Task 都正确执行或者超过执行次数的限制仍然没有执行成功时停止;

  • image-20210908111501219

环境搭建StandaloneHA

  • 回顾:Spark的Standalone独立部署模式,采用Master和Worker结构进行申请资源和执行计算
  • 问题:如果Master出问题了,整个Spark集群无法工作,如何处理?
  • 解决:涉及主备,需要一个主节点,需要一个备用节点,通过ZK选举,如果主节点宕机备份节点可以接替上主节点继续执行计算

高可用HA

  • 架构图
  • image-20210908111920879

基于Zookeeper实现HA

  • 如何实现HA的配置?
  • 1-需要修改spark-env.sh中的master的ip或host,注释掉,因为依靠zk来选择
  • image-20210908112612289

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

[外链图片转存中…(img-UiYlj7QA-1715682855273)]
[外链图片转存中…(img-KLBbJzl5-1715682855274)]
[外链图片转存中…(img-WejC7sG0-1715682855274)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值