spark RDD详解及源码分析

本文深入探讨Spark的Resilient Distributed Datasets (RDD),介绍其基本概念、适用范围、特性和创建方式。重点讲解RDD的操作、缓存机制、窄依赖与宽依赖的区别,并对RDD的源码进行了分析,包括RDD的子类、属性和方法。
摘要由CSDN通过智能技术生成

spark RDD详解及源码分析

@(SPARK)[spark]

一、基础

(一)什么是RDD

A Resilient Distributed Dataset (RDD), the basic abstraction in Spark. Represents an immutable, partitioned collection of elements that can be operated on in parallel.

RDD是spark最基本的抽象概念,spark中的所有数据均通过RDD的形式进行组织。RDD是弹性的,自动容错的,分区的,只读的记录集合。

(二)RDD的适用范围

RDD尤其适用于迭代式的数据处理,如机器学习等。但它不适合那些异步更新共享状态的应用,例如web爬虫。

(三)一些特性

1、 在部分分区数据丢失时,spark可以通过依赖关系重新计算丢失的分区数据,而不是对RDD的所有分区进行重算。
2、用户可以在创建RDD时指定RDD的分区数量,如果没有指定,那么就会采用默认值,即程序分区到的CPU core数目。对于HDFS,每个block会分配一个分区。对于由父RDD生成的子RDD,其分区数量与父RDD相同,或者在transformation中显式指定。详见spark调优那篇文章。

(四)RDD的创建

RDD有2种创建方式

1、由一个已经存在的scala集合创建

val rdd = sc.paralellize(List(1,2,3,4))

一般只在试验性代码中使用,生产环境不大可能用到。

2、由外部存储系统的数据创建

比如本地文件,HDFS, HBASE等,常用textFile方法

val rdd = sc.textFile("hdfs:///tmp/myfile.txt")

(五)RDD的操作

RDD有2种操作:transformation 与 action,详见RDD的transformation 与 action那篇文章。

二、RDD的缓存

对于一个经常被使用的RDD或者计算代价较大的RDD,将其缓存下来,会大大的提高处理速度。

(一)缓存方式

persist()是标准的缓存方法
cache()是其简化方法,当只使用内存作缓存时使用。

(二)缓存级别

MEMORY_ONLY Store RDD as deserialized Java objects in the JVM. If the RDD does not fit in memory, some partitions will not be cached and will be recomputed on the fly each time they’re needed. This is the default level.
MEMORY_AND_DISK Store RDD as deserialized Java objects in the JVM. If the RDD does not fit in memory, store the partitions that don’t fit on disk, and read them from there when they’re needed.
MEMORY_ONLY_SER Store RDD as serialized Java objects (one byte array per partition). This is generally more space-efficient than deserialized objects, especially when using a fast serializer, but more CPU-intensive to read.
MEMORY_AND_DISK_SER Similar to MEMORY_ONLY_SER, but spill partitions that don’t fit in memory to disk instead of recomputing them on the fly each time they’re needed.
DISK_ONLY Store the RDD partitions only on disk.
MEMORY_ONLY_2, MEMORY_AND_DISK_2, etc. Same as the levels above, but replicate each partition on two cluster nodes.
OFF_HEAP (experimental) Store RDD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值