reduce方法最常见的场景就是叠加。
var items = [10, 120, 1000];
// our reducer function
var reducer = function add(sumSoFar, item) { return sumSoFar + item; };
// do the job
var total = items.reduce(reducer, 0);
console.log(total); // 1130
假定我们需要计算大文本中每一行的长度,并且报告每个长度的行数。在hadoopmapreduce中,我们首先使用一个mapper,生成为以行的长度作为key,1作为value的键值对。
public class linelengthmapper extends
mapper
@override
protected void map(longwritable linenumber, text line, context context)
throws ioexception, interruptedexception {
context.write(new intwritable(line.getlength()), new intwritable(1));
}
}
值得注意的是mappers和reducers只对键值对进行操作。所以由textinputformat提供输入给linelengthmapper,实际上也是以文本中位置为key(很少这么用,但是总是需要有东西作为key),文本行为值的键值对。
与之对应的spark实现:
lines.map(line => (line.length, 1))
spark中,输入只是string构成的rdd,而不是key-value键值对。spark中对key-value键值对的表示是一个scala的元组,用(a,b)这样的语法来创建。上面的map操作的结果是(int,int)元组的rdd。当一个rdd包含很多元组,它获得了多个方法,如reducebykey,这对再现mapreduce行为将是至关重要的。
reduce
reduce()与reducebykey()
统计行的长度的键值对,需要在reducer中对每种长度作为key,计算其行数的总和作为value。
public class linelengthreducer extends
reducer
@override
protected void reduce(intwritable length, iterable
context context) throws ioexception, interruptedexception {
int sum = 0;
for (intwritable count : counts) {
sum += count.get();
}
context.write(length, new intwritable(sum));
}
}
spark中与上述mapper,reducer对应的实现只要一行代码:
val lengthcounts = lines.map(line => (line.length, 1)).reducebykey(_ + _)
spark的rdd api有个reduce方法,但是它会将所有key-value键值对reduce为单个value。这并不是hadoop mapreduce的行为,spark中与之对应的是reducebykey。
另外,reducer的reduce方法接收多值流,并产生0,1或多个结果。而reducebykey,它接受的是一个将两个值转化为一个值的函数,在这里,就是把两个数字映射到它们的和的简单加法函数。此关联函数可以被调用者用来reduce多个值到一个值。与reducer方法相比,他是一个根据key来reduce value的更简单而更精确的api。
mapper
map() 与 flatmap()
现在,考虑一个统计以大写字母开头的单词的个数的算法。对于每行输入文本,mapper可能产生0个,1个或多个键值对。
public class countuppercasemapper extends
mapper
@override
protected void map(longwritable linenumber, text line, context context)
throws ioexception, interruptedexception {
for (string word : line.tostring().split(" ")) {
if (character.isuppercase(word.charat(0))) {
context.write(new text(word), new intwritable(1));
}
}
}
}
spark对应的写法:
lines.flatmap(
_.split(" ").filter(word => character.isuppercase(word(0))).map(word => (word,1))
)
简单的spark map函数不适用于这种场景,因为map对于每个输入只能产生单个输出,但这个例子中一行需要产生多个输出。所以,和mapperapi支持的相比,spark的map函数语义更简单,应用范围更窄。
spark的解决方案是首先将每行映射为一组输出值,这组值可能为空值或多值。随后会通过flatmap函数被扁平化。数组中的词会被过滤并被转化为函数中的元组。这个例子中,真正模仿mapper行为的是flatmap,而不是map。
groupbykey()
写一个统计次数的reducer是简单的,在spark中,reducebykey可以被用来统计每个单词的总数。比如出于某种原因要求输出文件中每个单词都要显示为大写字母和其数量,在mapreduce中,实现如下:
public class countuppercasereducer extends
reducer
@override
protected void reduce(text word, iterable
throws ioexception, interruptedexception {
int sum = 0;
for (intwritable count : counts) {
sum += count.get();
}
context
.write(new text(word.tostring().touppercase()), new intwritable(sum));
}
}
但是redecebykey不能单独在spark中工作,因为他保留了原来的key。为了在spark中模拟,我们需要一些更像reducer api的操作。我们知道reducer的reduce方法接受一个key和一组值,然后完成一组转换。groupbykey和一个连续的map操作能够达到这样的目标:
groupbykey().map { case (word,ones) => (word.touppercase, ones.sum) }
groupbykey只是将某一个key的所有值收集在一起,并且不提供reduce功能。以此为基础,任何转换都可以作用在key和一系列值上。此处,将key转变为大写字母,将values直接求和。