Summary of Spark knowledge points

1. Spark ecology:

Spark Core

spark 的核心计算  ,用于通用分布式数据处理的引擎。不依赖于任何其他组件,可以运行在任何商用服务器集群上。实现饿了 Spark 的基本功能,包含任务调度、内存管理、错误恢复,与存储系统交互等模块。还包含了对弹性分布式数据集(Resilient Distributed Dataset,简称RDD)的API 定义。

Spark SQL

是Spark用来操作结构化数据的程序包,可以使用SQL或者HQL来对历史数据做交互式查询(即席查询:用户根据自己的需求 自定义查询)。Spark SQL 支持多种数据源,比如Hive表,Parquet 以及 JSON 等。

Spark Streaming

是Spark提供的对实时数据进行流式计算的组件,基于Spark的微批处理引擎,支持各种各样数据源的导入。提供了用来操作数据流的API,并且与Spark Core 中的RDD API 高度对应,唯一依赖的是 Spark Core引擎。

Spark MLib

构建在 Spark 之上的提供常见的机器学习(ML)功能的程序库,支持一系列数据挖掘算法。包括分类、回归、聚类、协同过滤,还提供了模型评估、数据导入等额外的支持功能。

Spark GraphX

图计算(关注事物本身而且关注事物之间的联系)

2. Briefly describe the difference between the real-time computing framework Storm, Spark Streaming and flink?

Common points:

   ① 都是开源的分布式系统,具有低延迟、可扩展和容错性诸多优点。

   ② 允许在运行数据流代码时,将任务分配到一列具有容错能力的计算机并行运行。

   ③ 都提供了简单的API来简化底层实现的复杂程度。

difference:

   ① Apache Storm中,先要设计一个用于实时计算的图状结构(topology,拓扑)。这个拓扑将会被提交到集群,由集群中的主控节点(master node)分发代码,将任务分配给工作节点(worker node)执行。一个拓扑中包括 spout 和 bolt 两种角色,其中 spout 发送消息,负责将数据流以 tuple(元组)的形式发送出去;而 bolt 则负责转换这些数据流,在 bolt 中可以完成计算、过滤等操作,bolt 自身也可以随机将数据发送给其他 bolt。由 spout 发射出的 tuple 是不可变数组,对应着固定的键值对。

   ② Spark Streaming 是核心Spark API 的扩展,不会像 Storm那样一次一个地处理数据流,而是在处理前按时间间隔预先将其切分为一段一段的批处理作业。Spark 针对持续性数据流的抽象(DStream,DiscretizedStream),一个 DStream 是一个微批处理(micro-batching)的RDD(弹性分布式数据集);而 RDD 则是一种分布式数据集,能够以两种方式并行运作,分别是任意函数和滑动窗口数据的转换。相对于 Flink,Spark将内存完全交给应用层,更容易出现 OOM(Out Of Memory)内存溢出。

   ③ Apache Flink 是一个针对流数据和批数据的分布式处理引擎。主要是由 Java 代码实现。对 Flink 而言,其所要处理的主要场景就是流数据,批数据知识流数据的一个极限特例而已。再换句话说,Flink 会把所有任务当成流来处理,这也是其最大的特点。Flink 可以支持本地的快速迭代,以及一些环形的迭代任务。并且 Flink 可以定制化内存管理。相对于 Spark ,Flink 并没有将内存完全交给应用层。就框架本身与应用场景来说,Flink 更相似于 Storm。

3. Spark resource scheduling

Local (local mode): A mode running on a computer, usually used for training and testing on the local computer.
Standalone mode: Build a Spark cluster composed of Master+Slave, and Spark runs in the cluster.
Yarn mode: The Spark client directly connects to Yarn without the need to build a Spark cluster.
Mesos mode: Spark client directly connects to Mesos; no additional Spark cluster is required. There are fewer domestic applications, and more use yarn scheduling.

4. What are the characteristics of spark?

运行快:基于内存,Spark 实现了高效的DAG执行引擎,可以通过基于内存来高效处理数据流,计算的中间结果是存在于内存中的。

易于使用:Spark支持Java、Python和Scala的API,还支持超过80种高级算法,使用户可以快速构建不同的应用。而Spark支持交互式的Python和Scala的Shell,可以非常方便地在这些Shell中使用Spark集群来验证解决问题的方法。

通用:Spark提供了统一的解决方案,可用于批处理,交互式查询,实时流处理,机器学习和图计算,都可以在同一个应用中无缝使用

兼容性:Spark可以很方便的与其他的开源产品进行融合。

5. Can .spark replace hadoop?

No, it is just an alternative to MapReduce, and it cannot store distributed data, that is, it cannot replace HDFS in Hadoop.

Spark is just a distributed computing platform, while Hadoop is already an ecosystem of distributed computing, storage, and management.
In the Hadoop architecture: HDFS (storage system), MapReduce (computing framework), Hive (query framework), HBase (real-time, quasi-real-time), YARN (resource scheduling)
Spark (a new type of big data computing framework, based on Hadoop The big data stored on the computer is used for calculation.)
Spark is only a tool specially used to process the big data that is stored in a distributed manner. It does not store distributed data.

6. What is the cache of spark Rdd?

Cache and persist In-memory cache, internal optimization mechanism. When Rdd is used repeatedly, it does not need to be recalculated, it is used directly from the memory

RDD通过 persist或cache 方法可以将前面的计算结果缓存,默认情况下 persist( ) 会把数据以序列化的形式缓存在 JVM 的堆(Heap)空间中。

缓存有可能丢失,或者存储于内存的数据由于内存不足而被删除,RDD的缓存容错机制保证了即使缓存丢失也能保证计算的正确执行。通过基于 RDD 的一系列转换,丢失的数据会被重算,由于 RDD 的各个 Partition 是相对独立的,因此只需要计算丢失的部分即可,并不需要重算全部 Partition。

7 There are two ways to submit .spark?

client:

  Driver 程序运行在客户端,适用于交互、调试,希望立即看到 app 的输出

cluster:

  Driver程序运行在由 RM(ResourceManager)启动的 AP(APPMaster)适用于生产环境。

8. Spark fault tolerance?

Lineage : blood relationship, recalculate based on blood relationship for fault tolerance.
RDD only supports coarse-grained conversion, that is, a single operation performed on a large number of records. Record a series of Lineage (blood relations) that created the RDD in order to recover the lost partition. Lineage of RDD records the metadata information and conversion behavior of RDD. When part of the partition data of the RDD is lost, it can recalculate and restore the lost data partition based on this information.
CheckPoint : Set checkpoint, usually file system, disk IO

9. Reasons for data skew in Spark:

- key本身分布不均衡
- 计算方式有误
- 过多的数据在一个 task 里面
- shuffle 并行度不够

10. What is executor memory allocation? (226)

  20%执行编写的代码,默认占Executor总内存的20%
  20%接收上一个stage的结果
  60%进行数据持久化所占用内存大小

11. The purpose of Spark design

Can quickly handle big data problems in a variety of scenarios, and efficiently mine the value of big data, thereby providing decision support for business development

12. What is Rdd? Features of RDD

RDD (Reslilent Distributed Dataset) is the most basic data abstraction in Spark. The code is an abstract class, which represents an immutable, partitionable collection of elements that can be calculated in parallel.
Features: RDD represents a read-only partitioned data set. To change the RDD, a new RDD can be obtained from an RDD only through the conversion operation of the RDD. The new RDD contains the necessary information derived from other RDDs. There are dependencies among RDDs, and the execution of RDDs is calculated according to the kinship delay. If the blood relationship is long, you can cut off the blood relationship by persisting the RDD.

① Partition

RDD is logically partitioned, the data of each partition exists abstractly, and the data of each partition is obtained through a compute function during calculation. If the RDD is built through an existing file system, the compute function is to read the data in the specified file system. If the RDD is converted from other RDDs, the compute function is to perform conversion logic to convert the data of other RDDs.

② Read only

RDD是只读的,要想改变RDD中的数据,只能在现有的RDD基础上创建新的RDD。

③ Dependence

④ Cache

⑤ CheckPoint

13. RDD attributes

   ① 一组分区(Partition),即数据集的基本组成单位。
    
    ② 一个计算每个分区的函数。

    ③ RDD之间的依赖关系。

    ④ 一个Partitioner,即RDD的分片函数。

    ⑤ 一个列表,存储存取每个Partition的优先位置。

14. Why is spark faster than hadoop's mr?

  1. Memory based
  2. Spark implements the DAG engine
  3. Spark fault tolerance

15.spark on hive 和 hive on spark?

spark on hive:hive作为数据源,spark计算
hive on spark:spark 作为hive 底层的计算引擎

16. What is the role of driver?

① 是执行开发程序中的main方法的进程。

② 负责开发人员编写的用来创建sc,创建rdd,以及进行rdd的转化操作和行动操作代码的执行。

③ 把用户程序转为作业

④ 跟踪Executor的运行状况

⑤ 为执行器节点调度任务

⑥ UI展示应用运行状况

17. What is the role of Executor?

① 是一个工作进程,负责在 Spark 作业中运行任务,任务间相互独立。

② Spark 应用启动时,Executor 节点被同时启动,并且始终伴随着整个 Spark 应用的生命周期而存在。如果Executor节点发生了故障或崩溃,Spark应用也可以继续执行,会将出错节点上的任务调度到其他 Executor 节点上继续运行。

③ 负责运行组成 Spark 应用的任务,并将结果返回给驱动器进程。

④ 通过自身的块管理器(Block Manager)为用户程序中要求缓存的RDD提供内存式存储。RDD是直接存在 Executor 进程内的,因此任务可以在运行时充分利用缓存数据加速运算。

18. How parallel is RDD?

An RDD can have multiple shards, one shard corresponds to one task, and the number of shards determines the degree of parallelism

The degree of parallelism is not as high as possible, and resources should also be considered

19. There are two kinds of operators in Spark?

Conversion operator (transformation):

   用来将RDD进行转换,构建RDD的血缘关系,由一个 RDD 生成一个新的RDD 不会被立即执行,记录的都是一系列的操作

Action operator (action):

   用来触发RDD的计算,得到RDD的相关计算结果或者将RDD保存的文件系统中。(立即执行,返回都是结果。)

20. Wide dependence and narrow dependence

Wide dependence:

(The relationship between parent RDD and child RDD and Partition is one-to-many and many-to-many)

ReduceByKey、Join

Wide dependency means that the Partition of multiple child RDDs will depend on the Partition of the same parent RDD, which will cause Shuffle.

Narrow dependence:

(There is a one-to-one relationship between parent RDD and child Rdd, Partition, or many-to-one relationship)

Map、Filter、Union

Narrow dependency means that each Partition of the parent RDD is used by at most one Partition of the child RDD.

21. The way to create RDD

① Create from the collection

  • Use parallelize () created from the set: val rdd = sc.parallelize(Array(1,2,3,5,3,4)),
  • Use makeRDD() to create from the collection:val rdd1 = sc.makeRDD(Array(1,2,3,5,3,4))

② Create an RDD from the data set of the external storage system:val rdd2=sc.textFile("hdfs://jh/RELEASE")

   包括本地的文件系统,还有所有 Hadoop 支持的数据集,比如:HDFS、Cassandra、HBase等

③ Create from other RDD

   根据原RDD创建新的RDD

22. Summarize the Rdd operator

Value type (13)

map、mapPartitions、mapPartitionsWithIndex、flatMap、glom、groupBy、filter
sample、distinct、coalesce、repartition、sortBy、pipe、

Double Value type interaction (5)

union、subtract、intersection、cartesian、zip、

key-value type(10)

partitionBy、groupByKey、reduceByKey、aggregateByKey、foldByKey、combineByKey、
sortByKey、mapValues、join、cogroup、

Action type (13)

reduce、collect、count、first、take、takeOrdered、aggregate、fold、saveAsTextFile、saveAsSequenceFile、saveAsObjectFile、countByKey、foreach、

23. What is the difference between Map and MapPartition?

map( ):每次处理一条数据。
mapPartition( ):每次处理一个分区的数据,这个分区的数据处理完后,原 RDD中分区的数据才能释放,可能导致OOM(Out Of Memory,内存溢出)当内存空间较大的时候建议使用 mapPartition( ),以提高处理效率。

24. What is the difference between coalesce and repartition?

coalesce 重新分区,可以选择是否进行 Shuffle 过程。由参数 shuffle:Boolean=false/true 决定。

repartition 实际上是调用的 coalesce,默认是进行 shuffle 的。

25. The difference between reduceByKey and groupByKey?

reduceByKey:按照 key 进行聚合,在 Shuffle 之前有 combine(预聚合)操作,返回结果是 RDD[k,v]。

groupByKey:按照 key 进行分组,直接进行 shuffle。

26. The difference between union and intersection?

union算子是将两个rdd的元素求并集

intersection 算子是将求两个 rdd 的元素的交集。

27. What are the commonly used wide dependence operators and narrow dependence operators?

Wide dependency operator:

  ① 所有byKey算子: partitionBy、groupByKey、reduceByKey、aggregateByKey、foldByKey、combineByKey、    sortByKey
    
  ② repartition、cartesian算子

  ③ 部分 join 算子

Narrow dependency operator:

map、flatMap、filter

28. What method can be used instead of the join operator?

Broadcast variable + map + filter

29. How does DAG divide the stage?

When encountering a Shuffle operator, it will be divided into a stage

30. How to divide the job?

When encountering an action operator, it will divide a job

31. Master resource scheduling algorithm?

尽量打散(默认的资源调度算法):

> 尽量让需要的资源平均的在不同机器上启动。

尽量集中:

> 尽量在某一台或者某几台机器上启动。

32. Serialization of Spark?

Spark creates serializers in two ways.

Java序列化

> 在默认情况下,Spark采用Java的ObjectOutputStream序列化一个对象。该方式适用于所有实现了java.io.Serializable的类。通过继承 java.io.Externalizable,你能进一步控制序列化的性能。Java序列化非常灵活,但是速度较慢,在某些情况下序列化的结果也比较大。
>
> 使用:
>
>     实现 Serializable 接口:`implements Serializable`



Kryo序列化
> Spark 也能使用 Kryo(版本2)序列化对象。Kryo不但速度极快,而且产生的结果更加紧凑(通常能提高10倍)。Kryo的缺点是不支持所有类型,为了更好的性能,你需要提前注册程序中所使用的类。
>
> 使用:
>
>     在创建 SparkContext 之前,通过调用 `System.setProperty("spark.serializer","org.apache.spark.serializer.KryoSerializer")`,将序列化方式切换成 Kryo。
>
> 但是 Kryo 需要用户进行注册。`conf.registerKryoClasses(Array(classof[MyClass],classof[MyClass2]))`
>
>     如果对象非常大,你还需要增加属性 spark.kryoserializer.buffer.mb 的值。该属性的默认值是32,但是该属性需要足够大以便能够容纳需要序列化的最大对象。

33. What is an accumulator?

The accumulator is a shared variable that provides a simple syntax for aggregating the values ​​in the work node into the driver program. A common use of accumulators is to count events during job execution during debugging. The only thing in the world is only increasing

    累加器用来对信息进行聚合,通常在向 Spark 传递函数时,比如使用 map( ) 函数或者用filter( ) 传条件时,可以使用驱动器程序中定义的变量,但是集群中运行的每个任务都会得到这些变量的一份新的副本,更新这些副本的值也不会影响驱动器中的对应变量。如果我们想实现所有分片处理时更新共享变量的功能,那么累加器可以实现我们想要的效果。

> 只有在 运行动作操作(action算子)之后累加器中才会有计数值,因为行动操作之前的转换操作(transformation算子)是惰性的。

34. What is a broadcast variable?

Broadcast variables are used to efficiently distribute larger objects. Send a larger read-only value to all worker nodes for one or more Spark operations. For example, if your application needs to send a large read-only lookup table to all nodes, or even a large feature vector in a machine learning algorithm, broadcast variables are easy to use. The same variable is used in multiple parallel operations, but Spark will send it separately for each task.

    使用广播变量的过程如下:

    ① 通过对一个类型T的对象调用 SparkContext.broadcast 创建出一个 Broadcast[T] 对象。任何可序列化的类型都可以这么实现。

    ② 通过 value 属性访问该对象的值(在 Java 中为 value( ) 方法)。

    ③ 变量只会被发到各个节点一次,应作为只读值处理(修改这个值不会影响到别的节点)。

35. The relationship between node and task execution?

  ① 每个节点可以起一个或多个 Executor。
 
  ② 每个Executor由若干 core组成,每个 Executor 的每个 core 一次只能执行一个 Task。
  
  ③ 每个Task执行的结果就是生成了目标RDD的一个partition。


注意:这里的core是虚拟的core而不是机器的物理CPU核,可以理解为就是 Executor的一个工作线程。而 Task被执行的并发度 = Executor数目 * 每个Executor 核数。

36. How to recover if the driver fails?

- --supervise 在集群模式下,driver失败了自动重启
- 对 driver 的元数据做 CK(CheckPoint)

37. How to view logs in cluster mode

① Use the aggregate log method (recommended, more commonly used) to aggregate the logs scattered on each machine in the cluster.

打开日志聚合的选项:`yarn.log-aggregation-enable` Container的日志会拷贝到HDFS上,并从机器上删除。对于这种情况,可以使用 `yarn logs -applicationId <app ID>` 命令,来查看日志。

yarn logs 命令,会打印出 application 对应的所有 Container 的日志出来,当然,因为日志是在 HDFS 上的,我们自然也可以通过 HDFS 的命令行来直接从 HDFS 中查看日志。

日志在HDFS中的目录,可以通过查看 yarn.nodemanager.remote-app-log-dir 和 yarn.nodemanager.remote-app-log-dir-suffix 属性来获知。

② WebUI

日志也可以通过 Spark web ui 来查看 executor 的输出日志

但是此时需要启动 History Server,需要让 spark History server 和 MapReduce History server 运行着,并且在 yarn-site.xml 文件中,配置 yarn.log.server.url 属性

spark History server web ui 中的 log url,会将你重定向到 mapreduce history server 上,去查看日志。

③ Scattered viewing (usually not recommended)

    如果没有打开聚合日志选项,那么日志默认就是散落在各个机器上的本地磁盘目录中的,在YARN_APP_LOGS_DIR 目录下,根据hadoop版本的不同,通常在 /tmp/logs 目录下,或者 $HADOOP_HOME/logs/userlogs 目录下,如果你要查看某个 Container 的日志,那么就得登录到那台机器上去,然后到指定的目录下去,找到那个日志文件,然后查看。

38. Spark optimization?

开发优化

    ① 避免创建重复的RDD

        `.persist()`和 `cache()`

    ② 尽可能使用同一个 RDD

    ③ 对多次使用的RDD进行持久化

        `new StorageLevel()`

    ④ 尽量避免使用 shuffle 类算子

        减少分区:broadcast + map + filter 代替 join

        大表join小表,将小表的数据广播到 executor中,使用 map+filter 完成join的功能

    ⑤ 使用map-side 预聚合的shuffle操作

    ⑥ 使用高性能的算子

    使用 reduceByKey/aggregateByKey代替groupByKey

    ⑦ 广播大变量

    ⑧ 使用 Kryo 优化序列化性能

    ⑨ 优化数据结构

资源调优

    ① num-executors

    ② executor-memory,1/3-1/2

    ③ executor-cores,建议设置为2~4个,不要超过总cores的1/3~1/2

    ④ task的总数一般是cpu core 的2-3倍。

39. What determines the number of RDD Partitions?

① 默认的,两个

② 指定的,numPartitions

③ 从HDFS读取数据,由块的个数决定

④ 从 Kafka 读取数据,由 topic 的 Partition 个数决定的

40. Spark persistence level?

> new StorageLevel(false, false, false, false)
>
> 第一个参数,是否持久化到磁盘
>
> 第二个参数,是否持久化到内存
>
> 第三个参数,是否持久化到JVM的heap中
>
> 第四个参数,是否不使用序列化
>
> ① 不持久化RDD数据
>
> val NONE = new StorageLevel(false, false, false, false)
>
> ② 将RDD数据持久化到磁盘
>
> val DISK_ONLY = new StorageLevel(true, false, false, false)
>
> ③ 在磁盘上保存两个副本
>
> val DISK_ONLY_2 = new StorageLevel(true, false, false, false, 2)
>
> ④ 将RDD的数据持久化到内存
>
> val MEMORY_ONLY = new StorageLevel(false, true, false, true)
>
> ⑤ 在内存中持久化两个副本
>
> val MEMORY_ONLY_2 = new StorageLevel(false, true, false, true, 2)
>
> ⑥ 将序列化后的RDD数据持久化到内存
>
> val MEMORY_ONLY_SER = new StorageLevel(false, true, false, false)
>
> ⑦ 将序列化后的RDD数据在内存中保存两个副本
>
> val MEMORY_ONLY_SER_2 = new StorageLevel(false, true, false, false, 2)
>
> ⑧ 将RDD的数据持久化到内存中,如果内存放不下,就溢写到磁盘上
>
> val MEMORY_AND_DISK = new StorageLevel(true, true, false, true)
>
> ⑨ 将RDD的数据在内存中持久化两个副本,如果放不下,就溢写到磁盘上
>
> val MEMORY_AND_DISK_2 = new StorageLevel(true, true, false, true, 2)
>
> ⑩ 将序列化后的RDD数据持久化到内存中,如果放不下,就溢写到磁盘上
>
> val MEMORY_AND_DISK_SER = new StorageLevel(true, true, false, false)
>
> ⑪ 将序列化后的RDD数据在内存中持久化两个副本,如果存不下,就溢写到磁盘上
>
> val MEMORY_AND_DISK_SER_2 = new StorageLevel(true, true, false, false, 2)
>
> ⑫ 把序列化后的RDD数据存储在内存中,放不下,就存放咋
>
> val OFF_HEAP = new StorageLevel(true, true, true, false, 1)

41. What are the parameters of spark spark-submit script?

-Master: the address of the master, where to submit the task for execution

--Deploy-mode: start the driver locally (Client) or start on the cluster, the default is client

--Class: the main class of the application, only for java or scala applications

--Name: the name of the application

-Jars: local jar packages separated by commas. After setting, these jars will be included in the classpath of the driver and executor.

--Packages: The maven coordinates of the jar contained in the classpath of the driver and executor.

--Execlude-packages: In order to avoid conflicts, specify packages that are not included

-Repositories: remote repository

--Conf PROP=VALUE: Specify the value of Spark configuration properties.

–Properties-file: loaded configuration file

--Driver-memory: driver memory, 1G by default

--Driver-java-options: additional Java options passed to the driver

--Driver-library-path: additional library path passed to the driver

--Driver-class-path: additional class path passed to the driver

--Driver-cores: the number of cores of the driver, the default is 1. Use in yarn or standalone

--Executor-memory: the memory of each executor, the default is 1G

--Total-executor-cores: The total number of cores of all executors, only used under mesos or stangalone.

--Num-executors: The number of executors started. The default is 2. Use under yarn

--Executor-core: the number of cores of each executor. Use under yarn or standalone.

42. How is spark configured?

Code

script

Configuration files, with decreasing priority

43.oom?

out of memory

44. What is the role of spark collect operator?

在驱动程序中,以数组的形式返回数据集的所有元素。

45. What is DAG?

Directed Acyclic Graph,有向无环图

原始的 RDD 通过一系列的转换就形成了 DAG,根据 RDD 之间的依赖关系的不同将DAG划分成不同的 Stage,对于窄依赖,Partition 的转换处理在 Stage 中完成计算。

拓展:

在 图论 中,如果一个有向图无法从某个定点出发经过若干条边回到该点,则这个图是一个 有向无环图(DAG图)。DAG与数组、排列、区块链一样,是一种数据结构。

46. ​​How does spark DAG divide the stage?

  • For narrow dependencies, the conversion processing of partitions is calculated in the stage, without division (put the narrow dependencies in the same stage as much as possible to achieve pipeline calculation).

  • For wide dependencies, due to the existence of shuffle, the next calculation can only be started after the parent RDD processing is completed, that is to say, the stage needs to be divided (the wide dependency is split).

  • The cutting rule of stage: from back to front, cut the stage when encountering wide dependence.

Guess you like

Origin blog.csdn.net/Poolweet_/article/details/108472944