fold and reduce both aggregate over a collection by implementing an operation you specify, the major different is the starting point of the aggregation. For fold(), you have to specify the starting value, and for reduce() the starting value is the first (or possibly an arbitrary) element in the collection.

Simple examples - we can sum the numbers in a collection using both functions: 
(1 until 10).reduce( (a,b) => a+b ) 
(1 until 10).fold(0)( (a,b) => a+b )

With fold, we want to start at 0 and cumulatively add each element. In this case, the operation passed to fold() and reduce() were very similar, but it is helpful to think about fold in the following way. For the operation we pass to fold(), imagine its two arguments are (i) the current accumulated value and (ii) the next value in the collection,

(1 until 10).fold(0)( (accumulated_so_far, next_value) => accumulated_so_far + next_value ).

So the result of the operation, accumulated_so_far + next_value, will be passed to the operation again as the first argument, and so on.

In this way, we could count the number of elements in a collection using fold,

(1 until 10).fold(0)( (accumulated_so_far, next_value) => accumulated_so_far + 1 ).

When it comes to Spark, here’s another thing to keep in mind. For both reduce and fold, you need to make sure your operation is both commutative and associative. For RDDs, reduce and fold are implemented on each partition separately, and then the results are combined using the operation.  With fold, this could get you into trouble because an empty partition will emit fold’s starting value, so the number of partitions might erroneously affect the result of the calculation, if you’re not careful about the operation. This would occur with the ( (a,b) => a+1) operation from above (see http://stackoverflow.com/questions/29150202/pyspark-fold-method-output).

reduce & fold in Spark的更多相关文章

  1. Spark计算模型

    [TOC] Spark计算模型 Spark程序模型 一个经典的示例模型 SparkContext中的textFile函数从HDFS读取日志文件,输出变量file var file = sc.textF ...

  2. 【原】Learning Spark (Python版) 学习笔记(一)----RDD 基本概念与命令

    <Learning Spark>这本书算是Spark入门的必读书了,中文版是<Spark快速大数据分析>,不过豆瓣书评很有意思的是,英文原版评分7.4,评论都说入门而已深入不足 ...

  3. (转)Spark 算子系列文章

    http://lxw1234.com/archives/2015/07/363.htm Spark算子:RDD基本转换操作(1)–map.flagMap.distinct Spark算子:RDD创建操 ...

  4. zhihu spark集群,书籍,论文

    spark集群中的节点可以只处理自身独立数据库里的数据,然后汇总吗? 修改 我将spark搭建在两台机器上,其中一台既是master又是slave,另一台是slave,两台机器上均装有独立的mongo ...

  5. [转]Spark学习之路 (三)Spark之RDD

    Spark学习之路 (三)Spark之RDD   https://www.cnblogs.com/qingyunzong/p/8899715.html 目录 一.RDD的概述 1.1 什么是RDD? ...

  6. 【spark 深入学习 06】RDD编程之旅基础篇02-Spaek shell

    --------------------- 本节内容: · Spark转换 RDD操作实例 · Spark行动 RDD操作实例 · 参考资料 --------------------- 关于学习编程方 ...

  7. Spark学习之路 (三)Spark之RDD

    一.RDD的概述 1.1 什么是RDD? RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素 ...

  8. <Spark><Programming><RDDs>

    Introduction to Core Spark Concepts driver program: 在集群上启动一系列的并行操作 包含应用的main函数,定义集群上的分布式数据集,操作数据集 通过 ...

  9. Spark(三)RDD与广播变量、累加器

    一.RDD的概述 1.1 什么是RDD RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可 ...

随机推荐

  1. python之UnittTest模块

    一. UnitTest单元测试框架 1.1概述 unittest原名为PyUnit,是由java的JUnit衍生而来.单元测试是对程序中最小的可测试模块(函数)来进行测试:对于单元测试,需要设置预先条 ...

  2. IOS - Display a base64 image within a UIImageView: 显示一个base64的图片

    base64字符串(base64String)-存的是image数据NSData* data = [[NSData alloc] initWithBase64EncodedString:base64S ...

  3. OPENGL学习【一】VS2008开发OPENGL程序开发环境搭建

    1.VS2008工具自行在网上下载安装,现只提供VS2008开发工具中配置OPENGL环境的详细步骤.开发包及编译工具会在下方一并放出链接. 2.打开CMake的工具,主要的配置信息如下,按照数字顺序 ...

  4. Linux目录结构组成

    linux目录图: / root --- 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home --- 存储普通用户的个人文件 ftp --- 用户所有服务 httpd ...

  5. php输出网页源代码莫名奇妙的多了一堆方框,导致ajax验证失败.

    今天在用一个ajax验证用户名的功能,返回值报错,抓包看了下,多出来一堆点,源代码里显示方框和6个空行 这堆东西导致ajax判断返回值会错误,度娘了一下午(皇天不负游戏人啊),原来是一个坑爹的BOM头 ...

  6. Ruby中使用patch HTTP方法

    Ruby中使用patch HTTP方法 如果使用patch,在后台可以看到只更新了改动的部分: Started PATCH "/ads/5/update" for ::1 at 2 ...

  7. iOS 相似QQ空间表视图下拉头部视图放大效果实现

    UITableView 是 UIScrollView 的子类. 所以 UIScrollView 的代理方法.在UITableView 上相同可以得到适用. 既然如此那么我们就行知道.在表格下拉的过程中 ...

  8. js+ canvas 实现人物走动

    在网上看了一篇管道工玛利亚走动的图片,感觉人物走动的太生涩了,就写了一下代码改动一下: js 代码: //定义数组图片集合 var marios = new Array("image/QQ截 ...

  9. 升级Xcode 导致插件失效的解决的方法

    我们在升级xcode的情况下,我们的一些第三方插件就会失效. 比方cocoapods,等比較重要的三方插件, 解决这个问题例如以下: 进入插件文件夹:~/Library/Application Sup ...

  10. Sublime text3 Emmet使用

    Emmet需要配置pyv8 进入 https://github.com/emmetio/pyv8-binaries 下载解压文件放入Sublime Installed Packages下面 就可以使用 ...