hadoop 中的mapreduce编程模板
2013-11-11 21:49
288 查看
public class MyClass extends Configured implements Tool {
public static class MapClass extends MapReduceBase implements Mapper<Text,Text,Text,Text>{
public void map(Text key,Text value,OutputCollector <Text,Text> output,Reporter reporter) throws IOException{
output.collect(value,key);
}
}
public static class Reduce extends MapReduceBase implements Reducer <Text,Text,Text,Text> {
public void reduce(Text key,Iterator<Text> values,OutputCollector<Text,Text>output,Report reporter) throws IOException {
String csv="";
while(value.hasNext()){
if(csv.length()>0) csv+=",";
csv+=values.next().toString();
}
output.collect(key,new Text(csv));
}
}
public int run(String [] args) throws Exception {
Configuration conf =getConf();
JobConf job =new JobConf(conf ,MyJob.class);
Path in=new Path(args[0]);
Path out=new Path(args[1]);
FileInputFormat.setInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
job.setJobName("MyJob");
job.setMapperClass(MapClass.class);
job.setReducerClass(Reduce.class);
job.setInputFormat(KeyVlaueTextInputFormat.class);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.set("key.value.separator.in.input.line",",");
JobClient.run.Job(job);
}
public static void main(String [] args) throws Exception{
int res = ToolRunner.run(new Configuration(),new MyClass(),args);
System.exit(res);
}
public static class MapClass extends MapReduceBase implements Mapper<Text,Text,Text,Text>{
public void map(Text key,Text value,OutputCollector <Text,Text> output,Reporter reporter) throws IOException{
output.collect(value,key);
}
}
public static class Reduce extends MapReduceBase implements Reducer <Text,Text,Text,Text> {
public void reduce(Text key,Iterator<Text> values,OutputCollector<Text,Text>output,Report reporter) throws IOException {
String csv="";
while(value.hasNext()){
if(csv.length()>0) csv+=",";
csv+=values.next().toString();
}
output.collect(key,new Text(csv));
}
}
public int run(String [] args) throws Exception {
Configuration conf =getConf();
JobConf job =new JobConf(conf ,MyJob.class);
Path in=new Path(args[0]);
Path out=new Path(args[1]);
FileInputFormat.setInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
job.setJobName("MyJob");
job.setMapperClass(MapClass.class);
job.setReducerClass(Reduce.class);
job.setInputFormat(KeyVlaueTextInputFormat.class);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.set("key.value.separator.in.input.line",",");
JobClient.run.Job(job);
}
public static void main(String [] args) throws Exception{
int res = ToolRunner.run(new Configuration(),new MyClass(),args);
System.exit(res);
}
相关文章推荐
- 【Apache Hadoop】MapReuce 编程总结-多MapReduce执行
- 关于hadoop的mapreduce编程中自定义key,value建立的类实现writable接口
- [Hadoop编程实践]一个实用、清晰的MapReduce程序
- Hadoop MapReduce编程案例
- Hadoop中关于MapReduce的编程实例(过滤系统日志)
- Hadoop MapReduce编程 API入门系列之MapReduce多种输入格式(十七)
- Hadoop MapReduce编程 API入门系列之FOF(Fund of Fund)(二十三)
- Hadoop MapReduce编程 API入门系列之计数器(二十七)
- hadoop编程(6)-MapReduce案例:Partitioner应用实例——全局排序
- Hadoop系列之三:函数式编程语言和MapReduce
- Hadoop MapReduce编程 API入门系列之wordcount版本4(八)
- hadoop mapreduce编程 内存溢出的解决办法
- 对于Hadoop的MapReduce编程makefile
- hadoop mapReduce程序模板-倒排索引
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
- Hadoop实战-中高级部分 之 Hadoop MapReduce高级编程
- Hadoop实践(三)---MapReduce相关编程笔记
- hadoop2.0 MapReduce编程(java客户端)
- Hadoop MapReduce编程 API入门系列之二次排序(十六)
- Hadoop MapReduce编程 API入门系列之倒排索引(二十四)