一、背景知识
RDD : Resilient Distributed DataSet
回弹性 分布式 数据集合
什么是 Resilient /rɪˈzɪliənt/ ? 英文解释是这样的:
able to recoil or spring back into shape after bending, stretching,
or being compressed.(
其形状能够在弯曲、拉伸、被压缩后 恢复回原来的模样)
那么 Resilient
应用到 RDD 中是什么意思?这需要引入 RDD 操作的另一个概念:Map。
Map 是什么? Java 中有 HashMap 集合类(使用Hash算法实现的Map)
HashMap 的对象是 (Key, Value) 格式的。即:一个键 映射 一个值。
这里的 Map 也是这个意思:映射。把一个 RDD 映射为 一个新的 RDD,
通过某种映射规则(函数)。 RDD 映射完成后,还可以恢复到映射前的样子。
二、reduceByKey() 函数
在 Apache Spark 的 Quick Start 页面 有这么一行代码,是关于 MapReduce 的概念及用法的。
//
//该行代码用来统计 ReadMe.md 文件中,每个单词出现的次数。
//
scala> val textFile = sc.textFile("README.md")
scala> val wordCounts = textFile
.flatMap(line => line.split(" "))
.map(word => (word, 1))
.reduceByKey((a, b) => a + b)
scala> wordCounts.collect()
//res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3),
// (Because,1), (Python,2), (agree,1), (cluster.,1), ...)
//
//
其中有这么一个函数: reduceByKey((a, b) => a + b)
经过查看 API,现记录如下:
________________________________________________________________________________
- reduceByKey(func, [numTasks])
________________________________________________________________________________
When called on a dataset of (K, V) pairs,
returns a dataset of (K, V) pairs where the values for each key
are aggregated using the given reduce function func, which must
be of type (V,V) => V.
Like in groupByKey, the number of reduce tasks is configurable
through an optional second argument.
------------------------------------------------------------------------------
解释:
reduceByKey 函数应用于(Key,Value)格式的数据集。
reduceByKey 函数的作用是把 key 相同的合并。
reduceByKey 函数同样返回一个(Key,Value)格式的数据集。
reduceByKey 函数的参数 func 的格式:(V,V) => V.
即:两个 value 作为参数;必须返回一个 Value 格式的数据。
________________________________________________________________________________
故:reduceByKey((a, b) => a + b) 的完整写法应该是这样的(大概意思):
function reduceByKey( function(value_a, value_b){
return value_a + value_b;
});
最后:
关于 Lambda 表达式,请参看我的这篇技术博文:
Java 8 之新特性介绍 及 Lambda 表达式
引用:
Apache Spark - Quick Start
http://spark.apache.org/docs/latest/quick-start.html
Apache Spark - Programming Guid: Transformations
http://spark.apache.org/docs/latest/programming-guide.html#transformations
-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2345420
-
分享到:
相关推荐
ApacheSpark设计与实现.pdf+ApacheSpark源码剖析.pdf+Spark原著中文版.pdf
By the end of this book, you will have all the knowledge you need to develop efficient large-scale applications using Apache Spark. What you will learn Get to know the fundamentals of Spark 2 and the...
Learning Apache Spark 2 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Mastering Apache Spark(掌握Apache Spark)英文版.pdf
Apache Spark开发环境搭建
Apache Spark 2.x Cookbook by Rishi Yadav English | 31 May 2017 | ASIN: B071HX7GHW | 294 Pages | AZW3 | 7.94 MB Key Features This book contains recipes on how to use Apache Spark as a unified compute...
Apache Spark是一个围绕速度、易用性和复杂分析构建的大数据处理框架。最初在2009年由加州大学伯克利分校的AMPLab开发,并于2010年成为Apache的开源项目之一。本文档用7步让你上手spark,节省学习时间
本文主要讨论 Apache Spark 的设计与实现,重点关注其设计思想、运行原理、实现架构及性能调优,附带讨论与 Hadoop MapReduce 在设计与实现上的区别。不喜欢将该文档称之为“源码分析”,因为本文的主要目的不是去...
Apache Spark 2.4 comes packed with a lot of new functionalities and improvements, including the new barrier execution mode, flexible streaming sink, the native AVRO data source, PySpark’s eager ...
Apache Spark 2.X for Java Developers 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
大数据--Apache Spark编程详解
Mastering Apache Spark 2.x - Second Edition by Romeo Kienzler English | 26 July 2017 | ISBN: 1786462745 | ASIN: B01MR4YF5G | 354 Pages | AZW3 | 13.74 MB Advanced analytics on your Big Data with ...
Apache Spark 2.x for Java Developers by Sourav Gulati English | 26 July 2017 | ISBN: 1787126498 | ASIN: B01LY3N7ZO | 350 Pages | AZW3 | 4.48 MB Key Features Perform big data processing with Spark—...
大数据--Apache Spark 入门知识
Apache Spark is a fast, scalable, and flexible open source distributed processing engine for big data systems and is one of the most active open source big data projects to date. In just 24 lessons of...
Apache Spark源码走读之3 -- Task运行期之函数调用关系分析
Frank Kane's Taming Big Data with Apache Spark and Python English | 2017 | ISBN-10: 1787287947 | 296 pages | AZW3/PDF/EPUB (conv) | 6.12 Mb Key Features Understand how Spark can be distributed across...
Apache Spark 命令注入(CVE-2022-33891)格式化文档 Apache Spark 命令注入(CVE-2022-33891)漏洞复现 CVE-2022-33891POC Apache Spark 命令注入(CVE-2022-33891)POC CVE-2022-33891 影响版本 Apache spark ...
Advanced analytics on your Big Data with latest Apache Spark 2.x About This Book An advanced guide with a combination of instructions and practical examples to extend the most up-to date Spark ...
Beginning Apache Spark 2: With Resilient Distributed Datasets, Spark SQL, Structured Streaming and Spark Machine Learning library