注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
mapreduce很多都听过或用过,在面试中也遇到几次,coding该怎么写做示范
// calcuate distinct words number for each file
// map():
// emit([fileid, word], 1)
// partition:
// sort by fileid
// reduce:
// sum = 0
// while (v: values) {
// sum += 1;
// }
// emit (fileid, sum)
- //Pseudo-code for "word counting"
- map(String key, String value) {
- // key: document name,
- // value: document contents
- for each word w in value:
- EmitIntermediate(w, "1");
- }
- combine(String key, Iterator values){
- // Combiner for map output
- // key: a word, values: a list of counts
- int partial_word_count = 0;
- for each v in values:
- partial_word_count += ParseInt(v);
- Emit(key, AsString(partial_word_count));
- }
- reduce(String key, Iterator values) {
- // key: a word
- // values: a list of counts
- int word_count = 0;
- for each v in values:
- word_count += ParseInt(v);
- Emit(key, AsString(word_count));
- }
复制代码- class Mapper {
- final IntWritable ONE = new IntWritable(1);
- map(LongWritable key, Text value, Context context) {
- String[] columns = value.split(" ");
- String url = columns[0];
- context.write(url, ONE);
- }
- }
- class Reducer {
- IntWritable result = new IntWritable();
- reduce(Text key, Iterable<IntWritable> values, Context context) {
- int sum = sum(values);
- result.set(sum);
- context.write(key, result);
- }
- }
复制代码 一些高级用法大家可以细看 https://datascienceguide.github.io/map-reduce
|