rdd

Spark性能优化

[亡魂溺海] 提交于 2019-12-06 03:09:44
Spark性能优化 1)避免创建重复RDD 2)尽可能复用同一个RDD 3)对多次使用的RDD进行持久化 4)尽量避免使用shuffle类算子 5)使用map-side预聚合的shuffle操作 6)使用高性能的算子 7)广播大变量 8)使用Kryo优化序列化性能 9)优化数据结构 10)资源参数调优 1)避免创建重复RDD ​ 对于同一份数据,只应该创建一个RDD,不能创建多个RDD来代表同一份数据。 2)尽可能复用同一个RDD ​ 除了要避免在开发过程中对一份完全相同的数据创建多个RDD之外,在对不同的数据执行算子 操作时还要尽可能地复用一个RDD。比如说,有一个RDD的数据格式是key-value类型的,另 一个是单value类型的,这两个RDD的value数据是完全一样的。那么此时我们可以只使用key-value类型的那个RDD,因为其中已经包含了另一个的数据。对于类似这种多个RDD的数据有重叠或者包含的情况,我们应该尽量复用一个RDD,这样可以尽可能地减少RDD的数量,从而尽可能减少算子执行的次数。 3)对多次使用的RDD进行持久化 Spark中对于一个RDD执行多次算子的默认原理是这样的:每次你对一个RDD执行一个算子操作时,都会重新从源头处计算一遍,计算出那个RDD来,然后再对这个RDD执行你的算子操作。因此对于这种情况,建议是:对多次使用的RDD进行持久化

spark调优——数据倾斜

末鹿安然 提交于 2019-12-06 03:04:45
Spark中的数据倾斜问题主要指shuffle过程中出现的数据倾斜问题,是由于不同的key对应的数据量不同导致的不同task所处理的数据量不同的问题。 例如,reduce点一共要处理100万条数据,第一个和第二个task分别被分配到了1万条数据,计算5分钟内完成,第三个task分配到了98万数据,此时第三个task可能需要10个小时完成,这使得整个Spark作业需要10个小时才能运行完成,这就是数据倾斜所带来的后果。 注意,要区分开数据倾斜与数据量过量这两种情况,数据倾斜是指少数task被分配了绝大多数的数据,因此少数task运行缓慢;数据过量是指所有task被分配的数据量都很大,相差不多,所有task都运行缓慢。 数据倾斜的表现: 1. Spark作业的大部分task都执行迅速,只有有限的几个task执行的非常慢,此时可能出现了数据倾斜,作业可以运行,但是运行得非常慢; 2. Spark作业的大部分task都执行迅速,但是有的task在运行过程中会突然报出OOM,反复执行几次都在某一个task报出OOM错误,此时可能出现了数据倾斜,作业无法正常运行。 定位数据倾斜问题: 1. 查阅代码中的shuffle算子,例如reduceByKey、countByKey、groupByKey、join等算子,根据代码逻辑判断此处是否会出现数据倾斜; 2. 查看Spark作业的log文件

spark调优——算子调优

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 03:02:16
算子调优一:mapPartitions 普通的map算子对RDD中的每一个元素进行操作,而mapPartitions算子对RDD中每一个分区进行操作。如果是普通的map算子,假设一个partition有1万条数据,那么map算子中的function要执行1万次,也就是对每个元素进行操作。 如果是mapPartition算子,由于一个task处理一个RDD的partition,那么一个task只会执行一次function,function一次接收所有的partition数据,效率比较高。 比如,当要把RDD中的所有数据通过JDBC写入数据,如果使用map算子,那么需要对RDD中的每一个元素都创建一个数据库连接,这样对资源的消耗很大,如果使用mapPartitions算子,那么针对一个分区的数据,只需要建立一个数据库连接。 mapPartitions算子也存在一些缺点:对于普通的map操作,一次处理一条数据,如果在处理了2000条数据后内存不足,那么可以将已经处理完的2000条数据从内存中垃圾回收掉;但是如果使用mapPartitions算子,但数据量非常大时,function一次处理一个分区的数据,如果一旦内存不足,此时无法回收内存,就可能会OOM,即内存溢出。 因此,mapPartitions算子适用于数据量不是特别大的时候

Spark常规性能调优

妖精的绣舞 提交于 2019-12-06 03:00:56
1.1.1 常规性能调优一:最优资源配置 Spark性能调优的第一步,就是为任务分配更多的资源,在一定范围内,增加资源的分配与性能的提升是成正比的,实现了最优的资源配置后,在此基础上再考虑进行后面论述的性能调优策略。 资源的分配在使用脚本提交Spark任务时进行指定,标准的Spark任务提交脚本如代码清单2-1所示:、 /usr/opt/modules/spark/bin/spark-submit \ --class com.atguigu.spark.Analysis \ --num-executors 80 \ --driver-memory 6g \ --executor-memory 6g \ --executor-cores 3 \ /usr/opt/modules/spark/jar/spark.jar \ 可以进行分配的资源如表2-1所示: 表2-1 可分配资源表 名称 说明 --num-executors 配置Executor的数量 --driver-memory 配置Driver内存(影响不大) --executor-memory 配置每个Executor的内存大小 --executor-cores 配置每个Executor的CPU core数量 调节原则:尽量将任务分配的资源调节到可以使用的资源的最大限度。 对于具体资源的分配

mapPartitions returns empty array

六眼飞鱼酱① 提交于 2019-12-06 02:49:11
问题 I have the following RDD which has 4 partitions:- val rdd=sc.parallelize(1 to 20,4) Now I try to call mapPartitions on this:- scala> rdd.mapPartitions(x=> { println(x.size); x }).collect 5 5 5 5 res98: Array[Int] = Array() Why does it return empty array? The anonymoys function is simply returning the same iterator it received, then how is it returning empty array? The interesting part is that if I remove println statement, it indeed returns non empty array:- scala> rdd.mapPartitions(x=> { x }

how to divide rdd data into two in spark?

一世执手 提交于 2019-12-06 02:27:49
问题 I have a data in Spark RDD and I want to divide it into two part with a scale such as 0.7. For example if the RDD looks like this: [1,2,3,4,5,6,7,8,9,10] I want to divide it into rdd1 : [1,2,3,4,5,6,7] and rdd2 : [8,9,10] with the scale 0.7. The rdd1 and rdd2 should be random every time. I tried this way: seed = random.randint(0,10000) rdd1 = data.sample(False,scale,seed) rdd2 = data.subtract(rdd1) and it works sometimes but when my data contains dict I experienced some problems. For example

Does spark keep all elements of an RDD[K,V] for a particular key in a single partition after “groupByKey” even if the data for a key is very huge?

假装没事ソ 提交于 2019-12-06 02:17:40
问题 Consider I have a PairedRDD of,say 10 partitions. But the keys are not evenly distributed, i.e, all the 9 partitions having data belongs to a single key say a and rest of the keys say b,c are there in last partition only.This is represented by the below figure: Now if I do a groupByKey on this rdd , from my understanding all data for same key will eventually go to different partitions or no data for the same key will not be in multiple partitions. Please correct me if I am wrong. If that is

spark (二)

ε祈祈猫儿з 提交于 2019-12-06 00:51:02
说明: 要在本地运行spark,因为spark是用scala语言写的,运行在JVM上面,你要做的只是安装java 6及以上版本就够了。 2.1 下载spark 访问http://spark.apache.org/downloads.html 的apache 官网下载spark 得到我们的预编译版本的 spark-1.2.0-bin-hadoop2.4.tgz 解压完是这样的: 我们看一下spark的目录结构: README.md 包含用来入门spark的简单的使用说明 bin 包含可以用来和spark进行各种方式的交互的一些列可执行文件,包括稍后会用到的spark shell core streaming python。。。 包含spark项目主要组件的源代码 examples 包含一些可以查看和运行的spark程序,对学习spark 的 API非常由帮助。 接下来我们先尝试一下用spark shell 运行一些spark自带的实例代码,然后下回在尝试编译并运行我们自己的spark程序。 2.2 spark 的 shell spark带有交互式的shell,可以作即使数据分析。和其他的系统中的shell类似,但不同的是在其他shell工具中你只能使用 单机的 硬盘和内存来操作数据。而spark shell 可以用来与 分布式 存储在许多台机器的内存或者硬盘上的数据 进行交互

What does the number meaning after the rdd

折月煮酒 提交于 2019-12-06 00:07:04
What does the meaning of the number in the bracket after rdd? hi-zir The number after RDD is its identifier: Welcome to ____ __ / __/__ ___ _____/ /__ _\ \/ _ \/ _ `/ __/ '_/ /___/ .__/\_,_/_/ /_/\_\ version 2.3.0 /_/ Using Scala version 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_151) Type in expressions to have them evaluated. Type :help for more information. scala> val rdd = sc.range(0, 42) rdd: org.apache.spark.rdd.RDD[Long] = MapPartitionsRDD[1] at range at <console>:24 scala> rdd.id res0: Int = 1 It is used to track RDD across the session, for example for purposes like caching : scala>

How to merge two presorted rdds in spark?

风格不统一 提交于 2019-12-06 00:01:02
I have two large csv files presorted by one of the columns. Is there a way to use the fact that they are already sorted to get a new sorted RDD faster, without full sorting again? The short answer: No, there is no way to leverage the fact that two input RDDs are already sorted when using the sort facilities offered by Apache Spark. The long answer: Under certain conditions, there might be a better way than using sortBy or sortByKey . The most obvious case is when the input RDDs are already sorted and represent distinct ranges. In this case, simply using rdd1.union(rdd2) is the fastest