Spark Chapter 6 Spark Core进阶

1 spark核心概念

1 Application

基于Spark的应用程序 =1driver+n executors

User program built on Spark.

Consists of a driver program and executors on the cluster.

例如:一个py脚本,或者pyshark/spark-shell

2 Application jar

在java或者scala开发中会有,python中较为少见

A jar containing the user's Spark application. In some cases users will want to create an "uber jar" containing their application along with its dependencies. The user's jar should never include Hadoop or Spark libraries, however, these will be added at runtime.

3 Driver program

运行一个main方法并创建一个sc

The process running the main() function of the application and creating the SparkContext

4 Cluster manager

获取外部资源的,可以指定需要的资源量

An external service for acquiring resources on the cluster (e.g. standalone manager, Mesos, YARN)

一个app需要申请,driver的内存,executors的内存,

5 Deploy mode

部署模式——driver 运行的位置

Distinguishes where the driver process runs. In "cluster" mode, the framework launches the driver inside of the cluster. In "client" mode, the submitter launches the driver outside of the cluster.

6 Worker node

standalone 相当于slave节点,yarn相当于node manager

Any node that can run application code in the cluster

7 Executor

A process launched for an application on a worker node, that runs tasks and keeps data in memory or disk storage across them. Each application has its own executors.

在不同的app中是不共用的。

8 Task

A unit of work that will be sent to one executor

map、shuffle

9 Job

一个action对应一个job

A parallel computation consisting of multiple tasks that gets spawned in response to a Spark action (e.g. save, collect); you'll see this term used in the driver's logs.


10 stage

一个stage的边界是从某个地方取数据开始到suffle结束

Each job gets divided into smaller sets of tasks called stagesthat depend on each other (similar to the map and reduce stages in MapReduce); you'll see this term used in the driver's logs.

2 spark 运行架构以及注意事项

【重要】

Spark applications run as independent sets of processes on a cluster, coordinated by the SparkContext object in your main program (called the driver program).

Specifically, to run on a cluster, the SparkContext can connect to several types ofcluster managers (either Spark’s own standalone cluster manager, Mesos or YARN), which allocate resources(分配资源) across applications.

Step1 Once connected, Spark acquires executors on nodes in the cluster, which are processes that run computations and store data for your application.

完成链接后,spark的任务就会链接到executorsexecutors会负责处理任务和数据存储

Step2 Next, it sends your application code (defined by JAR or Python files passed to SparkContext) to the executors.

接下来,SC会把代码发送到executors下

Step3  Finally, SparkContext sends tasks to the executors to run.

SparkContext 会把 tasks发送到executors 去执行。相当于是具体的任务执行的命令。





Each application gets its own executor processes, which stay up for the duration of the whole application and run tasks in multiple threads. This has the benefit of isolating applications from each other, on both the scheduling side (each driver schedules its own tasks) and executor side (tasks from different applications run in different JVMs). However, it also means that data cannot be shared across different Spark applications (instances of SparkContext) without writing it to an external storage system.

每一个都是独立的线程,应用程序之间是隔离的。数据不能跨应用程序共享。

Spark is agnostic to the underlying cluster manager. As long as it can acquire executor processes, and these communicate with each other, it is relatively easy to run it even on a cluster manager that also supports other applications (e.g. Mesos/YARN).

spark不关心底层运行的节点 

The driver program must listen for and accept incoming connections from its executors throughout its lifetime (e.g., see spark.driver.port in the network config section). As such, the driver program must be network addressable from the worker nodes.

有监听机制,防止运行错误

Because the driver schedules tasks on the cluster, it should be run close to the worker nodes, preferably on the same local area network. If you’d like to send requests to the cluster remotely, it’s better to open an RPC to the driver and have it submit operations from nearby than to run a driver far away from the worker nodes.

driver要尽可能近的靠近节点,想要发送远程请求需要开一个RPC



3 spark和hadoop重要概念区分

hadoop

1 一个MR程序-一个Job

2 一个Job =1个N个Task(Map/Reduce)

3 一个Task对对应于一个进程

4 Task运行时开启进程,Task执行完毕后销毁进程,对于多个Task,开销较大  (JVM共享对其无效)


Spark

1 Application= Driver(main方法创建SparkContext)+Executor

2 一个Application = 0~n个Job

3 一个Job = 一个Action

4 一个Job =1~n个Stage

5 一个Stage = 1~n个Task

6 一个Task 有一个线程,多个Task可以并行的方式运行在一个Executor进程中

4 Spark Cache详情

调用方法:rdd.cache()/persist()

如果启动这个机制,会把storageLevel改正memory only

使用lazy机制:没有遇到action 不提交作业

如果一个RDD在后续的计算中会被使用,建议Cache

from pyspark import StorageLevel

lines= sc.textFile("file:///home/hadoop/data/page_views.dat")

lines.count()

lines.cache()

lines.count()

lines.persist(StorageLevel.MEMORY_ONLY_2)

如果做了持久化,这样读取只有一次


rdd.unprisist()是非lazy的


RDD Persistence

One of the most important capabilities in Spark is persisting (or caching) a dataset in memory across operations. When you persist an RDD, each node stores any partitions of it that it computes in memory and reuses them in other actions on that dataset (or datasets derived from it). This allows future actions to be much faster (often by more than 10x). Caching is a key tool for iterative algorithms and fast interactive use.


cache方法底层是persist,缓存是有容错机制的,支持多副本。

如果内存较小可以用序列化的方式,较为节省内存。

persist的StorageLevel参数:MEMORY_ONLY, MEMORY_ONLY_2, MEMORY_AND_DISK, MEMORY_AND_DISK_2, DISK_ONLY, and DISK_ONLY_2.


持久化策略:内存,单副本,序列化


ps 查看存储情况:// 192.168.199.102:4040

5 Spark Lineage详情

Lineage:RDD的依赖关系

在容错机制中,按照partition计算,无需全部计算。

问1:如果没有持久化,是不是如果第二个partition丢失了,还是的从磁盘里读取后再计算?

问2:持久化的计算消耗大么?


6 Spark Dependancy

narrow窄依赖-pipline-abe

一个父RDD的分区最多被一个字RDD使用

——map,filiter,union,join with inputs co-partitioned

wide宽依赖-shuffle

一个父RDD会被子RDD使用多次

——groupbykey on non-partitioned data,join with inputs not co-partitioned,repartition,coclesce,cogroup,reducebykey

The reduceByKey operation generates a new RDD where all values for a single key are combined into a tuple - the key and the result of executing a reduce function against all values associated with that key. 

The Shuffle is an expensive operation since it involves disk I/O, data serialization, and network I/O. T

区别

如果是窄依赖,如果错了只需要重算部分父RDD,宽依赖要重新计算所有RDD。

问:reducebykey之后会重新分配数据,那么partition输出的partition应该分几块?


写代码作业

lines = sc.textFile

words = flatMap

pairs = map(word,1)

reduceByKey



sc.textFile().flatmap().map().reduceByKey()

可以看到两个stage

【作业】:官网suffle内容阅读


tips

进程——process

线程——threads

血源——lineage

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容