今天就跟大家聊聊有關MapReduce怎樣實現TopK,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
創(chuàng)新互聯公司是一家集網站建設,渾江企業(yè)網站建設,渾江品牌網站建設,網站定制,渾江網站建設報價,網絡營銷,網絡優(yōu)化,渾江網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
需求: HTTP日志文件中全部流量前80%的記錄, 按流量值降序排序
輸出格式
HTTP日志文件:
1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 24681 200 1363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 120.197.40.4 4 0 264 0 200 1363157991076 13926435656 20-10-7A-28-CC-0A:CMCC 120.196.100.99 2 4 132 1512 200 1363154400022 13926251106 5C-0E-8B-8B-B1-50:CMCC 120.197.40.4 4 0 240 0 200 1363157993044 18211575961 94-71-AC-CD-E6-18:CMCC-EASY 120.196.100.99 iface.qiyi.com 視頻網站 15 12 1527 2106 200 1363157995074 84138413 5C-0E-8B-8C-E8-20:7DaysInn 120.197.40.4 122.72.52.12 20 16 4116 1432 200 1363157993055 13560439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 18 15 1116 954 200 1363157995033 15920133257 5C-0E-8B-C7-BA-20:CMCC 120.197.40.4 sug.so.# 信息安全 20 20 3156 2936 200 1363157983019 13719199419 68-A1-B7-03-07-B1:CMCC-EASY 120.196.100.82 4 0 240 0 200 1363157984041 13660577991 5C-0E-8B-92-5C-20:CMCC-EASY 120.197.40.4 s19.# 站點統(tǒng)計 24 9 6960 690 200 1363157973098 15013685858 5C-0E-8B-C7-F7-90:CMCC 120.197.40.4 rank.ie.sogou.com 搜索引擎 28 27 3659 3538 200 1363157986029 15989002119 E8-99-C4-4E-93-E0:CMCC-EASY 120.196.100.99 www.umeng.com 站點統(tǒng)計 3 3 1938 180 200 1363157992093 13560439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 15 9 918 4938 200 1363157986041 13480253104 5C-0E-8B-C7-FC-80:CMCC-EASY 120.197.40.4 3 3 180 180 200 1363157984040 13602846565 5C-0E-8B-8B-B6-00:CMCC 120.197.40.4 2052.flash3-http.qq.com 綜合門戶 15 12 1938 2910 200 1363157995093 13922314466 00-FD-07-A2-EC-BA:CMCC 120.196.100.82 img.qfc.cn 12 12 3008 3720 200 1363157982040 13502468823 5C-0A-5B-6A-0B-D4:CMCC-EASY 120.196.100.99 y0.ifengimg.com 綜合門戶 57 102 7335 110349 200 1363157986072 18320173382 84-25-DB-4F-10-1A:CMCC-EASY 120.196.100.99 input.shouji.sogou.com 搜索引擎 21 18 9531 2412 200 1363157990043 13925057413 00-1F-64-E1-E6-9A:CMCC 120.196.100.55 t3.baidu.com 搜索引擎 69 63 11058 48243 200 1363157988072 13760778710 00-FD-07-A4-7B-08:CMCC 120.196.100.82 2 2 120 120 200 1363157985066 13726238888 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 24681 200 1363157993055 13560436666 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 18 15 1116 954 200
定義FlowBean類,該類實現WritableComparable接口
實現write(), readFields(), compareTo()方法
public class FlowBean implements WritableComparable{ private String phoneNB;// 號碼 private long up_flow;// 上行流量 private long down_flow;// 下行流量 private long sum_flow;// 總流量 public String getPhoneNB() { return phoneNB; } public void setPhoneNB(String phoneNB) { this.phoneNB = phoneNB; } public long getUp_flow() { return up_flow; } public void setUp_flow(long up_flow) { this.up_flow = up_flow; } public long getDown_flow() { return down_flow; } public void setDown_flow(long down_flow) { this.down_flow = down_flow; } public long getSum_flow() { return sum_flow; } public void setSum_flow(long sum_flow) { this.sum_flow = sum_flow; } public FlowBean() { } public FlowBean(String phoneNB, long up_flow, long down_flow) { this.phoneNB = phoneNB; this.up_flow = up_flow; this.down_flow = down_flow; this.sum_flow = up_flow + down_flow; } /** * up_flow + "\t" + down_flow + "\t" + sum_flow */ @Override public String toString() { return up_flow + "\t" + down_flow + "\t" + sum_flow; } /** * 序列化, 序列化與反序列化各屬性順序一致 */ @Override public void write(DataOutput out) throws IOException { out.writeUTF(phoneNB); out.writeLong(up_flow); out.writeLong(down_flow); out.writeLong(sum_flow); } /** * 反序列化, 反序列化與序列化各屬性順序一致 */ @Override public void readFields(DataInput in) throws IOException { phoneNB = in.readUTF(); up_flow = in.readLong(); down_flow = in.readLong(); sum_flow = in.readLong(); } /** * 按總流量降序排序, 但總流量相等時, 兩個FlowBean對象內容并不相等 */ @Override public int compareTo(FlowBean o) { if (sum_flow == o.sum_flow) { return 1; } return -Long.compare(sum_flow, o.sum_flow); } }
定義Mapper類TopKFlowMapper
并重寫map方法
public class TopKFlowMapper extends Mapper{ // mapper輸出格式: @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] data = StringUtils.split(line, "\t"); String phoneNB = data[1]; long up_flow = Long.parseLong(data[7]); long down_flow = Long.parseLong(data[8]); context.write(new Text(phoneNB), new FlowBean(phoneNB, up_flow, down_flow)); } }
定義Reducer類TopKFlowReducer
并實現reduce(), 重寫cleanup()方法
public class TopKFlowReducer extends Reducer{ // 利用TreeMap的排序功能, 將FlowBean對象按總流量降序排序 private Map treeMap = new TreeMap (); private double globalFlow = 0;// 全局流量計數器, 初值值為0 // reducer輸入格式: @Override protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { long up_sum = 0; long down_sum = 0; for (FlowBean bean : values) { up_sum += bean.getUp_flow(); down_sum += bean.getDown_flow(); } // 每求得一條phoneNB的總流量, 就累加到全局流量計數器globalCount中 globalFlow += (up_sum + down_sum); // 利用TreeMap的排序功能, 將FlowBean對象按總流量降序排序 treeMap.put(new FlowBean("", up_sum, down_sum), key.toString()); } // cleanup方法是在reduce階段退出前被調用一次 @Override protected void cleanup(Context context) throws IOException, InterruptedException { double itemCount = 0; for (Map.Entry item : treeMap.entrySet()) { if (itemCount > globalFlow * 0.8) { return; } // 只輸出全局流量計數器globalCount前80%的記錄 context.write(new Text(item.getValue()), new VLongWritable(item.getKey().getSum_flow())); itemCount += item.getKey().getSum_flow(); } } }
測試TopK
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job job = Job.getInstance(new Configuration()); job.setJarByClass(TopKFlowRunner.class); // 設置job的主類 job.setMapperClass(TopKFlowMapper.class); // 設置Mapper類 job.setReducerClass(TopKFlowReducer.class); // 設置Reducer類 job.setMapOutputKeyClass(Text.class); // 設置map階段輸出Key的類型 job.setMapOutputValueClass(FlowBean.class); // 設置map階段輸出Value的類型 job.setOutputKeyClass(Text.class); // 設置reduce階段輸出Key的類型 job.setOutputValueClass(VLongWritable.class); // 設置reduce階段輸出Value的類型 // 設置job輸入路徑(從main方法參數args中獲取) FileInputFormat.setInputPaths(job, new Path(args[0])); // 設置job輸出路徑(從main方法參數args中獲取) FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); // 提交job }
job輸出的結果文件:
13726230503 27162
13726238888 27162
13925057413 11121
18320173382 9549
13502468823 7437
13660577991 6969
13922314466 6728
13560439658 6292
看完上述內容,你們對MapReduce怎樣實現TopK有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝大家的支持。