1. Exigences
Pour les informations fournies dans le journal du journal, comptez le nombre de visites aux utilisateurs dans différentes provinces du site Web.
2. Code de programmation
package org.apache.hadoop.studyhdfs.mapreduce; import java.io.ioexception; import org.apache.commons.lang.stringutils; import org.apache.hadoop.conf.configuration; import org.apache.hadoop.conf.configuration; import org.apache.hadoop.conf. org.apache.hadoop.fs.path; import org.apache.hadoop.io.intwitable; import org.apache.hadoop.io.longwitable; import org.apache.hadoop.io.text; import org.apache.hadoop.mapreduce.job; import org.apache.hadoop.mapreduce.mapper; import; import; org.apache.hadoop.mapreduce.mapper.context; import org.apache.hadoop.mapreduce.reducer; import org.apache.hadoop.mapreduce.lib.input.fileInputformat; import org.apache.hadoop.mapreduce.lib.output.Fileoutputformat; Importer org.apache.hadoop.Tool. org.apache.hadoop.util.toolrunner; import org.jboss.netty.util.internal.stringutil; classe publique ProventiCountMapReduce étend l'outil d'implémente configuré {//1.map/** <keyin, valueIn, keyout, valueout> * / public static wordCountMapper extension insest mapOutputKey = new IntWitable (); private intwitable mapOutputValue = new IntWitable (1); @ OverRidePublic void map (clé longue, valeur texte, contexte de contexte) lance ioException, interruptedException {// get LineValueString linevalue = value.tostring (); // splitString [] strs = linevalue.split ("); BLANKSTRING URL = STRS [1]; String provinceIdValue = STRS [23]; // Guolvif (StrS.Length <30 || StringUtils.isblank (provinceIdValue) || StringUtils.isblank (url)) {return; } int provinceId = Integer.max_value; try {provinceId = Integer.valueof (provinceIdValue);} catch (exception e) {return;} if (provinceid == Integer.max_value) {return;} mapoutputke mapOutputValue);}} // 2.reducepublic statique class wordcountreduce étend le réducteur <intrwitable, intrwitable, intrwitable, intrwitable> {private intwitable outwordValue = new intwitable (); @ overdepublic vide réteint (intrwitable key, itablent <intwult>, context) throws ioexception, interrupted 0; pour (IntWitable Value: Values) {sum + = value.get ();} outputValue.Set (SUM); context.write (key, outputValue);}} public int run (String [] args) lance l'exception {// 1.get configurationconfiguration conf = super.getConf (); // 2.Create jobJob) this.getClass (). getIMPlename ()); job.setjarbyclass (provinceCountMapreduce.class); // 3.set Job // 3.1 set inputpath inputpath = new Path (args [0]); fileInputTormat.AdDinInputPath (Job, inputPath); // 3.2 set set MAPPERJOB.SetMapperClass (WordCountMapper.Class); Job.SetMapOutputKeyClass (intwitable.class); Job.SetMapOutputValuclass (intwitable.class); // 3.3 set réductionjob.setReducerclass (wordCounTreduce.class); job.setOutputkeyClass (intwitable.class); job.setOutputvaluclass (intwitable.class); // 3.4 set inputpathpathPath = new Path (args [1]); fileoutputformat.setUpPath (job, outputpath); // 4.SubputArms.SetUpPath (job, outputpath); // 4.SUBPORTBOLANS = Job.WaitForCompletion (true); return Issucess? 0: 1;} public static void main (String [] args) lève une exception {args = new String [] {"hdfs: //hadoop-senior02.beifeng.com: 8020 / entrée / 2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020 / output15 /"}; configuration conf = new Configuration (); Conf.set ("mapreduce.map.output.ComphyPress", "true"); int status = toolRunner.run (conf, new provinceCountMapreduce (), args); System.exit (status);}} 3. Résultats de l'opération
1) Exécuter le code: bin / hdfs dfs -text / output15 / par *
2) Résultats de l'opération:
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
Ce qui précède est une introduction connexe au nombre de visites d'utilisateurs dans différentes provinces sur le site Web des statistiques du code Java qui vous est présentée par l'éditeur. J'espère que cela vous sera utile. Ici, l'éditeur remercie également tout le monde pour son soutien au site Web du réseau Wulin!