RDD(Resilient Distributed Dataset)全称是弹性分布式数据集,官方文档的描述是:
The main abstraction Spark provides is a resilient distributed dataset (RDD), which is a collection of elements partitioned across the nodes of the cluster that can be operated on in parallel
RDD虽然叫数据集,但它并不存储数据,可以抽象理解为待处理数据的代理
在分布式情况下,数据分布在不同位置,我们不可能一个一个去处理。所以用RDD代表要处理的所有数据集,这样就能方便的进行并行计算
* Internally, each RDD is characterized by five main properties:** - A list of partitions* - A function for computing each split* - A list of dependencies on other RDDs* - Optionally, a Partitioner for key-value RDDs (e.g. to say that the RDD is hash-partitioned)* - Optionally, a list of preferred locations to compute each split on (e.g. block locations for an HDFS file)
// 创建配置对象val sparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount");val sc = new SparkContext(sparkConf)/** Word Count **/val fileRDD: RDD[String] = sc.textFile("data/input/word.txt") // (1)读取,一行一行的读取文件val flatRDD: RDD[String] = fileRDD.flatMap( _.split(" ") ) // (2)切分,一行字符切成单个单词val mapRDD: RDD[(String, Int)] = flatRDD.map((_,1)) // (3)转换,数据结构 word => (word, 1)val reduceRDD: RDD[(String, Int)] = mapRDD.reduceByKey(_+_) // (4)聚合,相同相加
RDD几个重要特点
分区的弹性:在经过转换算子处理时,RDD中的分区数以及分区所在的位置是可以改变的。这样可以改变并行度,合理利用资源
文章转载自码农大腿哥,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




