1. Anforderungen
Für die im Protokollprotokoll angegebenen Informationen zählen Sie die Anzahl der Besuche bei Benutzern in verschiedenen Provinzen auf der Website.
2. Programmiercode
paket org.apache.hadoop.studyhdfs.mapreduce; import java.io.ioxception; import org.apache.commons.lang.stringutils import org.apache.hadoop.conf.conf.configuration; import.hadoop.conf.conf.conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf.Conf. org.apache.hadoop.fs.path; import org.apache.hadoop.io.intwritable; import org.apache.hadoop.io.longWrable; org.apache.hadoop.mapreduce.mapper.context; import org.apache.hadoop.mapreduce.reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputformat; importieren org. org.apache.hadoop.util.Tool;import 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, intRabitable, intritable> {private intRitable mapoutputkey = new intwribled (); private intRabitable mapoutputValue = new intwribleable (1);@oversidepublic void map (langwritierbarer Schlüssel, Textwert, Kontextkontext) löst IoException, InterconthedException {// // // // // // // // lineestring linevalue = value.toString (); // splitstring [] strs = lineValue.split ("/t"); // Line Blankstring url = strs [1]; String provinceValue = strs [23]; // Guolvif (strs.length <30 || Stringutils.isblank (ProvincIdValue) || Stringutils.isblank (url)) {return; }int provinceId =Integer.MAX_VALUE;try {provinceId=Integer.valueOf(provinceIdValue);} catch (Exception e) {return;}if(provinceId == Integer.MAX_VALUE){return;}mapOutputKey.set(provinceId);context.write(mapOutputKey, mapOutputValue);}}//2.reducepublic static class WordCountReduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{private IntWritable outputValue =new IntWritable();@Overridepublic void reduce(IntWritable key, Iterable<IntWritable> values,Context context)throws IOException, InterruptedException {//to doint sum = 0; für (intwrable value: values) {sum += value.get ();} outputValue.set (sum); context.write (Schlüssel, outputValue);}} public int run (String [] args) löst eine Ausnahme aus {// 1.get Konfigurationskonfiguration conf = super.getConf (). this.getClass().getSimpleName());job.setJarByClass(ProvinceCountMapReduce.class);//3.set job//3.1 set inputPath inputPath = new Path(args[0]);FileInputFormat.addInputPath(job, inputPath);//3.2 set mapperjob.setMapperClass (WordCountMapper.Class); Job.SetMapoutputKeyClass (intRable.class); Job.SetMapoutputValueClass (intReBable.class); // 3.3 Set recordjob.setReDucerClass (WordcounTreduce.class); Job.SetoutputKeyClass (intRabitable.class); = Job.waitforCompletion (true); return issuccess? 0: 1;} public static void main (String [] args) löst Ausnahme aus {args = neu String [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/input/2015082818", "Hdfs: //hadoop-senior02.beifeng.com: 8020/output15/"}; configuration Conf = New Configuration (); Conf. Neue provincountMapReduce (), args); System.exit (Status);}} 3. Betriebsergebnisse
1) Code ausführen: bin/hdfs dfs -text/output15/par*
2) Betriebsergebnisse:
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
Die oben genannte ist eine damit verbundene Einführung in die Anzahl der Besuche von Benutzern in verschiedenen Provinzen auf der vom Editor vorgestellten Java -Code -Statistik -Website. Ich hoffe, es wird Ihnen hilfreich sein. Hier dankt der Herausgeber auch allen für ihre Unterstützung für die Wulin Network -Website!