设为首页 加入收藏

TOP

新版MapReduce的API编程简单模板
2014-11-24 03:32:19 来源: 作者: 【 】 浏览:0
Tags:新版 MapReduce API 编程 简单 模板

新版MapReduce的API编程简单模板


import java.io.IOException;
import java.util.*;


public class MyJob extends Configured implements Tool{
public static class Map extends Mapper {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line," \t\n\r\f,.: ![]'`;");
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}


public static class Reduce extends Reducer {
public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int sum = 0;
while (values.iterator().hasNext()) {
sum += values.iterator().next().get();
}
context.write(key, new IntWritable(sum));
}
}

public int run(String[] args) throws Exception {
Configuration conf=getConf();

Job job= new Job(conf,"WordCount");
job.setJarByClass(MyJob.class);

FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.setJobName("WordCount");
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) 0:1) ;
return 1;
}

public static void main(String[] args) throws Exception {
int res =ToolRunner.run(new Configuration(), new MyJob(), args);
System.exit(res);
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下Shell编程——grep命令的.. 下一篇在线升级uboot,内核和文件系统

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)