hadoop 自定義GroupComparator實現求最大值
import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /* * 如下蝕測試資料,第一個欄位為訂單號,第二個欄位為id 第三個欄位為價格,資料以tab鍵分開 * 要求求出訂單號相同的價格最大的條記錄 O00001 123 1234 O00002 124 3435 O00003 125 132.78 O00004 126 334 O00004 127 8976 O00003 128 635 O00002 129 23 O00001 130 980 O00001 131 111 O00002 132 66 O00003 133 42 O00004 134 88 O00005 135 900 結果如下: O00001 123 1234.0 O00002 124 3435.0 O00003 128 635.0 O00004 127 8976.0 O00005 135 900.0 * */ public class GroupComparatorMian { static final Log LOG = LogFactory.getLog(GroupComparatorMian.class); public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(GroupComparatorMian.class); job.setMapperClass(GroupComparatorMapper.class); job.setReducerClass(GroupComparatorReducer.class); job.setOutputKeyClass(OrderBean.class); job.setOutputValueClass(NullWritable.class); job.setGroupingComparatorClass(CustGroupComparator.class); String jobName = "'Customize groupcomparator test'"; job.setJobName(jobName); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean bb = job.waitForCompletion(true); if(bb) { LOG.info("Job "+ jobName +" is done."); }else { LOG.info("Job "+ jobName +"is going wrong,now exit."); System.exit(0); } } } class CustGroupComparator extends WritableComparator{ public CustGroupComparator() { super(OrderBean.class,true); } @Override public int compare(WritableComparable a, WritableComparable b) { OrderBean oa = (OrderBean)a; OrderBean ob = (OrderBean)b; return oa.getOrder_id().compareTo(ob.getOrder_id()); } } class OrderBean implements WritableComparable<OrderBean>{ private String order_id; private String id ; private double prise; public OrderBean() { } public OrderBean(String order_id,String id,double prise) { this.order_id = order_id ; this.id = id; this.prise = prise; } public String getOrder_id() { return order_id; } public void setOrder_id(String order_id) { this.order_id = order_id; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getPrise() { return prise; } public void setPrise(double prise) { this.prise = prise; } @Override public void write(DataOutput out) throws IOException { out.writeUTF(order_id); out.writeUTF(id); out.writeDouble(prise); } @Override public void readFields(DataInput in) throws IOException { this.order_id = in.readUTF(); this.id = in.readUTF(); this.prise = in.readDouble(); } @Override public int compareTo(OrderBean o) { int cnt = this.order_id.compareTo(o.getOrder_id()); if(cnt==0) { cnt = (int) (-(this.prise- o.getPrise())); } return cnt; } @Override public String toString() { return order_id + "\t" + id + "\t" + prise ; } } class GroupComparatorMapper extends Mapper<LongWritable, Text, OrderBean, NullWritable>{ NullWritable nul = NullWritable.get(); OrderBean ob = new OrderBean(); @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, OrderBean, NullWritable>.Context context) throws IOException, InterruptedException { String[] split = value.toString().split("\t"); ob.setOrder_id(split[0]); ob.setId(split[1]); ob.setPrise(Double.parseDouble(split[2])); context.write(ob, nul); } } class GroupComparatorReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable>{ NullWritable nul = NullWritable.get(); @Override protected void reduce(OrderBean bean, Iterable<NullWritable> iter, Reducer<OrderBean, NullWritable, OrderBean, NullWritable>.Context context) throws IOException, InterruptedException { context.write(bean, nul); } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2213397/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ASP.NET Core - 實現Http自定義請求頭策略ASP.NETHTTP
- hadoop mapreducez自定義分割槽Hadoop
- hadoop自定義許可權Hadoop
- Angular HTTP 請求自定義 timeout 值的一種實現思路AngularHTTP
- Hadoop自定義輸出排序方式Hadoop排序
- 自定義SpringMVC部分實現SpringMVC
- 自定義實現Complex類
- Android自定義拍照實現Android
- Net 實現自定義Aop
- EventSource的自定義實現
- hadoop 自定義格式化輸出Hadoop
- CefSharp自定義快取實現快取
- Flutter自定義View的實現FlutterView
- Flutter自定義Banner的實現Flutter
- 編寫類A01,定義方法max,實現求某個double陣列的最大值,並返回陣列
- 20190118-自定義實現replace方法
- 實現MyBatisPlus自定義sql注入器MyBatisSQL
- Qt實現自定義控制元件QT控制元件
- 自定義事件實現子傳父事件
- @ConfigurationProperties實現自定義配置繫結
- Flutter實現自定義篩選框Flutter
- avalonia實現自定義小彈窗
- 2397 求最大值
- JavaScript實現自定義的生命週期JavaScript
- vue-自定義指令-實現提示功能Vue
- video自定義實現視訊播放功能IDE
- Highcharts 實現自定義匯出圖片
- Laravel 實現自定義生成檔案命令Laravel
- python實現自定義執行緒池Python執行緒
- .Net Core中自定義認證實現
- 淺談如何實現自定義的 iterator
- 封裝 Laravel 自定義表單請求封裝Laravel
- Spring Boot之Validation自定義實現總結Spring Boot
- 自定義檔案上傳功能實現方法
- Css實現checkbox及radio樣式自定義CSS
- 使用Guava的ComparisonChain實現自定義的排序GuavaAI排序
- OpenAPI生成器中實現自定義模板API
- 怎麼利用AbstractQueuedSynchronizer實現自定義同步元件?元件