java mapreduce教程_Java搭建MapReduce完成二次排序步骤
1、構(gòu)建新的作業(yè)
Configuration conf=getConf();
Job job=Job.getInstance(conf);
job.setJarByClass(SortYearAndTemp2.class);
2、設(shè)置輸入輸出目錄
Path inpath=new Path(conf.get("inpath"));
Path outpath=new Path(conf.get("outpath"));
FileInputFormat.addInputPath(job, inpath);
FileOutputFormat.setOutputPath(job, outpath);
3、設(shè)置Mapper Reducer函數(shù)
job.setMapperClass(SortYearAndTemp2.SortYearAndTemp2Mapper.class);
public static class SortYearAndTemp2Mapper extends Mapper
job.setReducerClass(SortYearAndTemp2.SortYearAndTemp2Reducer.class);
public static class SortYearAndTemp2Reducer extends Reducer
4、設(shè)置輸出格式
job.setMapOutputKeyClass(Weather.class);
job.setMapOutputValueClass(Text.class);
job.setOutputValueClass(Text.class);
job.setOutputKeyClass(NullWritable.class);
5、指定分區(qū)方式
job.setPartitionerClass(WeatherPartitioner.class); //參數(shù)為Partitioner的子類
創(chuàng)建Partitioner的子類:public static class WeatherPartitioner extends Partitioner
重寫getPartition()方法,返回int //指定分區(qū)方式
6、指定分組方式
job.setGroupingComparatorClass(WeatherGroup.class); //參數(shù)為WritableComparator的子類
創(chuàng)建WritableComparator的子類:public static class WeatherGroup extends WritableComparator
構(gòu)造無參構(gòu)造器WeatherGroup(){super(Weather, true);} //重寫父類帶有參數(shù)的構(gòu)造器
重寫compare()方法 //指定分組方式
7、提交執(zhí)行作業(yè),等待作業(yè)完成之后退出虛擬機
return job.waitForCompletion(true)?0:1;
8、創(chuàng)建傳遞給key的類
public static class Weather implements WritableComparable
重寫write()方法 //序列化
重寫readFields()方法 //反序列化
重寫compareTo()方法 //規(guī)定key參數(shù)類的排序規(guī)則
設(shè)置set() get()方法
構(gòu)造帶有全部參數(shù)和無參的構(gòu)造器
總結(jié)
以上是生活随笔為你收集整理的java mapreduce教程_Java搭建MapReduce完成二次排序步骤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 湖北省仙桃市2021年高考成绩查询,20
- 下一篇: Java两同_java:一个类实现的两个