使用Alluxio优化Spark RDD
原文链接:http://blog.csdn.net/zy_zhengyang/article/details/52538562
Alluxio把内存存储的功能从Spark中分离出来,使Spark可以更专注计算的本身,以求通过更细的分工达到更高的执行效率。Alluxio可以有效地解决Spark的以下几个问题:(1)当两个Spark作业需要共享数据时,无需再通过写磁盘,而是借助Alluxio进行内存读写,从而提高计算效率;(2)在使用Alluxio对数据进行缓存后,即便在Spark程序崩溃JVM进程退出后,所缓存数据也不会丢失。这样,Spark工作重启时可以直接从Alluxio内存读取数据了;(3)当两个Spark作业需要操作相同的数据时,它们可以直接从Alluxio获取,并不需要各自缓存一份数据,从而降低JVM内存压力,减少垃圾收集发生的频率。
具体的优化方式目前主要从RDD的读写和缓存两个方面来考虑,其他方面,以后再做补充。
一、RDD的读写
1.1 RDD的读取
文件的输入路径要以alluxio://sparktest-m1:19998/开头,值得注意的是,第一次加载文件会从所挂载的HDFS中读入然后写入Alluxio(如果Alluxio存储够用的话)供spark计算使用。
>valdomain=sc.textFile("alluxio://sparktest-m1:19998/chengdu/dnsDomainChengdu_distinct")1.2 RDD的写出
文件的写出根据实际需要进行判断,当两个Spark作业需要共享数据时,可以借助Alluxio进行内存读写,从而提高计算效率;如不需要,则不必写入。
>valresult=domain.map(line=>(line,1L)).reduceByKey(_+_) >result.saveAsTextFile("hdfs://sparktest-m1:9000/chengdu/tes")二、RDD的缓存
介绍了两种缓存RDD的方式,2.1介绍了Spark自身缓存的使用,2.2介绍了Alluxio的缓存的使用,2.3介绍了两种方式在不同数据规模下的性能对比以及使用时机。
2.1使用Spark API缓存 RDD
使用Spark进行计算时,通常使用spark API提供的cache或者persist来提高spark的计算性能,此时spark将RDD中的数据缓存到executors的JVM内存中,下一次使用该数据时就直接可以从其中的内存中读取。
Spark的缓存(持久化)方式(这里只列举三种):
1) MEMORY_ONLY: stores Javaobjects in the Spark JVM memory
2) MEMORY_ONLY_SER: storesserialized java objects in the Spark JVM memory
3) DISK_ONLY: stores the data on the local disk
>rdd.persist(MEMORY_ONLY) 或者rdd.persist() 或者rdd.cache() >rdd.count()2.2 使用Alluxio缓存 RDD
使用Alluxio进行缓存时,通常使用的Spark API为saveAsTextFile和saveAsObjectFile。
1) saveAsTextFile: writes the RDD as a text file, where eachelement is a line in the file
2) saveAsObjectFile: writes the RDD out to a file, by usingJava serialization on each element
>rdd.saveAsTextFile(alluxioPath) >rdd = sc.textFile(alluxioPath) >rdd.count()2.3 不同缓存方式在count操作下的性能对比
From the figure,it is clear that reading from an RDD saved in Alluxio results in very stable,and predictable performance. However, when persisting data within Spark, theperformance is high for smaller data set sizes, but large data set sizes causesa significant decrease in performance. For instance, when using persist(MEMORY_ONLY) on a machinewith 61GB of memory, when the data set size exceeds 10GB, the data nolonger can fully fit in Spark memory, and the runtime slows down.
This figure also shows that when using saveAsTextFile with filesin Alluxio memory, the performance is slower than using the Spark cache directlyfor smaller data set sizes. However, for larger data set sizes, reading the RDDfrom Alluxio files performs significantly better, because it scales linearlywith the data size. Therefore, for a given size of memory for a node, Alluxioenables applications to process more data at memory speeds.
总的来说,使用Alluxio缓存时性能是相对稳定的、线性可预测的,使用Spark缓存则会在数据规模增大时会出现严重的衰退。数据规模相对较小时,spark缓存性能更高,而Alluxio缓存使用saveAsTextFile操作写文件性能要比Spark缓存差。数据规模增大时,Alluxio缓存呈现出稳定性的特点,Spark缓存会出现严重的性能衰退。例如,在一个61GB内存的平台上进行 persist(MEMORY_ONLY)操作时,数据规模超过10GB性能开始衰退。
参考链接:
1. http://www.alluxio.com/2016/08/effective-spark-rdds-with-alluxio/