1. Persyaratan
Untuk informasi yang diberikan dalam log log, hitung jumlah kunjungan ke pengguna di berbagai provinsi di situs web.
2. Kode pemrograman
Paket org.apache.hadoop.studyhdfs.mapreduce; impor java.io.ioException; impor org.apache.commons.lang.stringutils; impor org.apache.hadoop.configuration; Impor org.apache.hadoop.conf.configuration; org.apache.hadoop.fs.path; import org.apache.hadoop.io.intwritable; import org.apache.hadoop.io.longwritable; impor org.apache.hadoop.io.text; import org.apache.hadoop.mapreduce.job; import org.apache.hadopache.hadoop.mapreduce; org.apache.hadoop.mapreduce.mapper.context; impor org.apache.hadoop.mapreduce.reducer; import org.apache.hadoop.mapreduce.lib.input.fileInputformat; import orgache.hadoop.mapreduce.lib.outpute.fileoPeTache; org.apache.hadoop.util.ToolRunner;import org.jboss.netty.util.internal.StringUtil;public class ProvinceCountMapReduce extends Configured implements Tool {//1.map/** <KEYIN,VALUEIN,KEYOUT,VALUEOUT>*/public static class WordCountMapper extends Mapper<LongWritable,Text,IntWritable,IntWritable>{private IntWritable MapOutputKey = new Intwritable (); private intrwritable mapOutputValue = new Intwritable (1);@overridepublic void peta (kunci longwriting, nilai teks, konteks konteks) melempar ioException, interruptException {// dapatkan linevaluestring linevalue = value.tostring (); // = 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; coba {provinceId = integer.valueof (provinceIdValue);} catch (pengecualian e) {return;} if (provinceid == integer.max_value) {return;} petaoutputKey.set (provinceid); context.write) {petauTon;} petaoutputKey.set (provinceid); context.write) {mAPOutput; MapOutputValue);}} // 2.Reducepublic Public Static Class WordCountreduce Memperluas Reducer <Intwritable, Intworsible, Intwritable, Intwrible> {Private Intwrowable OutputValue = new IntWrited ();@overridepublic ded berkurang (intrawit conception yang dapat dituntun, tidak dapat diterjemahkan <). doint sum = 0; untuk (nilai yang tidak dapat diterimisi: nilai) {sum += value.get ();} outputValue.set (sum); context.write (key, outputValue);}} public int run (string [] args) lemparan pengecualian {// 1.get configurationconfiguration confor. this.getClass (). getsImplename ()); job.setjarbyclass (provinceCountMapreduce.class); // 3.set job // 3.1 Set inputpath inputpath = Path baru (args [0]); setInputFormat.addinputPath (job, inputpath); // 3.2 mapperjob.setMapperclass (wordcountmapper.class); job.setMapoutputKeyClass (intrwritable.class); job.setMapoutputValueClass (intrwritable.class); // set dedikeJob.setreducerclass (wordcountreduce.class); job.setoutputKeyclass (intrwritable.class); job.setoutputValueClass (intrwritable.class); // 3.4 outpath outputpath = args [1]); FileoutputFormat.setpath outputPath = new path (args [1]); FileoutputFormat.setpathpathpath = new Path (args [1]); FileOutputFormat.setPutPath = Path, args [1]); FileOutputFormat.setPutPath = Path, args [1]); FileOutputFormat.setSputPATHPATH = NEW = job.waitforCompetion (true); return issuccess? 0: 1;} public static void main (string [] args) melempar pengecualian {args = baru String [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/input/2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020/output15/"}; konfigurasi conf = new configuration (); conf.set ("mapreduc.Map. status = toolrunner.run (conf, provinceCountMapreduce (), args); System.exit (status);}}} 3. Hasil Operasi
1) Jalankan kode: bin/hdfs dfs -text/output15/par*
2) Hasil Operasi:
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
Di atas adalah pengantar terkait dengan jumlah kunjungan pengguna di berbagai provinsi di situs web Java Code Statistics yang diperkenalkan kepada Anda oleh editor. Saya harap ini akan membantu Anda. Di sini, editor juga berterima kasih kepada semua orang atas dukungan mereka untuk situs web Wulin Network!