1. ข้อกำหนด
สำหรับข้อมูลที่ให้ไว้ในบันทึกจำนวนการเข้าชมผู้ใช้ในจังหวัดต่าง ๆ ในเว็บไซต์
2. รหัสการเขียนโปรแกรม
แพ็คเกจ org.apache.hadoop.studyhdfs.mapreduce; นำเข้า java.io.ioexception; นำเข้า org.apache.commons.lang.stringutils; นำเข้า org.apache.hadoop.conf.configuration; org.apache.hadoop.fs.path; นำเข้า org.apache.hadoop.io.intwable; นำเข้า org.apache.hadoop.io.longwritable; นำเข้า org.apache.hadoop.io.Text; นำเข้า org.apache.hadoop.mapreduce.mapreduce. org.apache.hadoop.mapreduce.mapper.context; นำเข้า org.apache.hadoop.mapreduce.reducer; นำเข้า org.apache.hadoop.mapreduce.lib.input.fileinputformat; นำเข้า org.apache.hadoop.mapreduce.lib.lib.lib.lib.lib.lib.lib.lib.lib.lib.lib.lib. org.apache.hadoop.util.toolrunner; นำเข้า org.jboss.netty.util.internal.stringutil; คลาสสาธารณะ provincecountmapreduce ขยายเครื่องมือดำเนินการที่กำหนดค่า {//1.map/** <keyin IntWritable MapOutputKey = ใหม่ intWritable (); mapOutputValue ส่วนตัว = ใหม่ intWritable (1);@แผนที่โมฆะ overridepublic (กุญแจที่เขียนได้ยาว, ค่าข้อความ, บริบทบริบท) โยน iOexception, interruptedException {// = linevalue.split ("/t"); // line blankstring url = strs [1]; String ProvinceIdValue = strs [23]; // guolvif (strs.length <30 || StringUtils.isblank (ProvinceIdValue) || stringutils.isblank (url)) {return; } int provinceId = integer.max_value; ลอง {provinceid = integer.valueof (ProvinceIdValue);} catch (ข้อยกเว้น e) {return;} ถ้า (ProvinceId == Integer.max_value) {return; MapOutputValue);}} // 2. REDUCEPUNT แบบคงที่คลาสคงที่ WordCountREDUCE ขยายตัวลด <intwlitable, intwlitable, intwritable, intwritable> {outputValue ส่วนตัวที่เขียนได้ sum = 0; สำหรับ (ค่า intwritable: ค่า) {sum += value.get ();} outputValue.set (sum); context.write (key, outputValue);}} public int run (string [] args) throws {// this.getClass (). getSimplename ()); job.setjarbyclass (ProvincecountMapreduce.class); // 3.Set Job // 3.1 Set InputPather InputPath = เส้นทางใหม่ (args [0]); fileInputFormat.adDinputPath mapperjob.setmapperclass (wordcountmapper.class); job.setmapoutputkeyclass (intwritable.class); job.setMapoutputValueclass (intwritable.class); // 3.3 Set Set Downjob.setReducerclass (WordCountreDuce.class); job.setOutputkeChyclass (Intwritable.class); job.setOutputValueclass (intwritable.class); // 3.4 set inputpath outputPath = เส้นทางใหม่ (args [1]); = job.waitforcompletion (จริง); return issuccess? 0: 1;} โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {args = ใหม่ สตริง [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/อินพุต/2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020/output15/"}; สถานะ = toolrunner.run (conf, ใหม่ ProvincecountMapreduce (), args); system.exit (สถานะ);}} 3. ผลการดำเนินงาน
1) เรียกใช้รหัส: bin/hdfs dfs -text/output15/par*
2) ผลการดำเนินงาน:
1 3527
2 1672
3 511
4 325
5 776
6 661
7 95
8 80
9 183
10 93
11 135
12 289
13 264
14 374
15 163
16 419
17 306
18 272
19 226
20 2861
21 124
22 38
23 96
24 100
25 20
26 157
27 49
28 21
29 85
30 42
32 173
ข้างต้นเป็นการแนะนำที่เกี่ยวข้องกับจำนวนการเข้าชมของผู้ใช้ในจังหวัดต่าง ๆ ในเว็บไซต์สถิติรหัส Java ที่แนะนำโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ ที่นี่บรรณาธิการยังขอบคุณทุกคนที่ให้การสนับสนุนเว็บไซต์ Wulin Network!