The basic principle of understanding Spark of 30 minutes

 

Articles published in several Chilean public No. [Story] (ID: decision_engine), number of public attention not to miss every one dry.

 

 

 

Author | Liang Yun 1991

Reprinted from the United States (ID: Python_Ai_Road) Python and Algorithms

 

0 1 the Spark Benefits Features

 

As the successor of big data computing MapReduce framework, Spark has the following advantages properties.

 

0 . 1 efficiency

 

Unlike MapReduce intermediate calculation result into the disk, using the Spark memory store intermediate calculation results, the iterative computation reduces the IO disk, and the DAG was calculated by optimizing parallel, reducing the dependency between the different tasks, reduced delays for time. The memory is calculated, Spark MapReduce 100 times faster than.

 

 

0 2 ease of use

 

Unlike MapReduce Map and Reduce supports only two programming operator, the Spark offers more than 80 different operators Transformation and Action, such as map, reduce, filter, groupByKey, sortByKey, foreach the like, and the use of functional programming style, to achieve the same amount of code required functionality greatly reduced.

 

 

0 3 -class resistance

 

Spark provides a unified solution. Spark can be used in batch, interactive queries (Spark SQL), real-time stream processing (Spark Streaming), machine learning (Spark MLlib) and calculated (GraphX).

 

These different types of processing can be seamlessly used in the same application. This enterprise applications, you can use a platform to implement different projects, reducing the cost of human development and deployment platform.

 

 

0 . 4 Compatibility

 

Spark 能够跟很多开源工程兼容使用。如 Spark 可以使用 Hadoop 的 YARN 和 Apache Mesos 作为它的资源管理和调度器,并且 Spark 可以读取多种数据源,如 HDFS、HBase、MySQL 等。

 

 

02Spark基本概念

 

RDD:是弹性分布式数据集(Resilient Distributed Dataset)的简称,是分布式内存的一个抽象概念,提供了一种高度受限的共享内存模型。

 

DAG:是 Directed Acyclic Graph(有向无环图)的简称,反映 RDD 之间的依赖关系。

 

Driver Program:控制程序,负责为 Application 构建 DAG 图。

 

Cluster Manager:集群资源管理中心,负责分配计算资源。

 

Worker Node:工作节点,负责完成具体计算。

 

Executor:是运行在工作节点(Worker Node)上的一个进程,负责运行 Task,并为应用程序存储数据。

 

Application:用户编写的 Spark 应用程序,一个 Application 包含多个 Job。

 

Job:作业,一个 Job 包含多个 RDD 及作用于相应 RDD 上的各种操作。

 

Stage:阶段,是作业的基本调度单位,一个作业会分为多组任务,每组任务被称为“阶段”。

 

Task:任务,运行在 Executor 上的工作单元,是 Executor 中的一个线程。

 

总结:Application 由多个 Job 组成,Job 由多个 Stage 组成,Stage 由多个 Task 组成。Stage 是作业调度的基本单位。

 

 

03Spark架构设计

 

Spark 集群由 Driver, Cluster Manager(Standalone, Yarn 或 Mesos),以及 Worker Node 组成。对于每个 Spark 应用程序,Worker Node 上存在一个 Executor 进程,Executor 进程中包括多个 Task 线程。

 

 

04Spark运行流程

 

1,Application 首先被 Driver 构建 DAG 图并分解成 Stage。

 

2,然后 Driver 向 Cluster Manager 申请资源。

 

3,Cluster Manager 向某些 Work Node 发送征召信号。

 

4,被征召的 Work Node 启动 Executor 进程响应征召,并向 Driver 申请任务。

 

5,Driver 分配 Task 给 Work Node。

 

6,Executor 以 Stage 为单位执行 Task,期间 Driver 进行监控。

 

7,Driver 收到 Executor 任务完成的信号后向 Cluster Manager 发送注销信号。

 

8,Cluster Manager 向 Work Node 发送释放资源信号。

 

9,Work Node 对应 Executor 停止运行。

 

 

 

05Spark部署模式

 

Local:本地运行模式,非分布式。

 

Standalone:使用 Spark 自带集群管理器,部署后只能运行 Spark 任务。

 

Yarn:Haoop 集群管理器,部署后可以同时运行 MapReduce,Spark,Storm,Hbase 等各种任务。

 

Mesos:与 Yarn 最大的不同是 Mesos 的资源分配是二次的,Mesos 负责分配一次,计算框架可以选择接受或者拒绝。

 

 

06RDD数据结构

 

RDD 全称 Resilient Distributed Dataset,弹性分布式数据集,它是记录的只读分区集合,是 Spark 的基本数据结构。

 

RDD 代表一个不可变、可分区、里面的元素可并行计算的集合。

 

一般有两种方式可以创建 RDD,第一种是读取文件中的数据生成 RDD,第二种则是通过将内存中的对象并行化得到 RDD。

 

//通过读取文件生成RDD
val  rdd = sc.textFile("hdfs://hans/data_warehouse/test/data")

 

//通过将内存中的对象并行化得到RDD
val num = Array(1,2,3,4,5)
val rdd = sc.parallelize(num)
//或者 val rdd = sc.makeRDD(num)

 

创建 RDD 之后,可以使用各种操作对 RDD 进行编程。

 

RDD 的操作有两种类型,即 Transformation 操作和 Action 操作。转换操作是从已经存在的 RDD 创建一个新的 RDD,而行动操作是在 RDD 上进行计算后返回结果到 Driver。

 

Transformation 操作都具有 Lazy 特性,即 Spark 不会立刻进行实际的计算,只会记录执行的轨迹,只有触发 Action 操作的时候,它才会根据 DAG 图真正执行。

 

 

操作确定了 RDD 之间的依赖关系。

 

RDD 之间的依赖关系有两种类型,即窄依赖和宽依赖。窄依赖时,父 RDD 的分区和子 RDD 的分区的关系是一对一或者多对一的关系。而宽依赖时,父 RDD 的分区和子 RDD 的分区是一对多或者多对多的关系。

 

宽依赖关系相关的操作一般具有 shuffle 过程,即通过一个 Patitioner 函数将父 RDD 中每个分区上 key 不同的记录分发到不同的子 RDD 分区。

 

 

依赖关系确定了 DAG 切分成 Stage 的方式。

 

切割规则:从后往前,遇到宽依赖就切割 Stage。

 

RDD 之间的依赖关系形成一个 DAG 有向无环图,DAG 会提交给 DAGScheduler,DAGScheduler 会把 DAG 划分成相互依赖的多个 stage,划分 stage 的依据就是 RDD 之间的宽窄依赖。遇到宽依赖就划分 stage,每个 stage 包含一个或多个 task 任务。然后将这些 task 以 taskSet 的形式提交给 TaskScheduler 运行。

 

 

07WordCount范例

 

只需要四行代码就可以完成 WordCount 词频统计。

 

val file = sc.textFile("hello.txt")
val word = file.flatMap(_.split(","))
val wordOne = word.map((_,1))
wordOne.reduceByKey(_+_)

 

 

 

数智物语征稿启事0613.png

 

星标我,每天多一点智慧

 

Guess you like

Origin www.cnblogs.com/shuzhiwuyu/p/11202533.html