1. Requisitos
Para la información dada en el registro de registro, cuente el número de visitas a los usuarios en diferentes provincias en el sitio web.
2. Código de programación
paquete org.apache.hadoop.studyhdfs.mapreduce; import java.io.ioException; import org.apache.commons.lang.stringutils; import org.apache.hadoop.conf.confonfiation; import.apache.hadoop.conf.configuration; importación; org.apache.hadoop.fs.path; importar org.apache.hadoop.io.intwitable; import org.apache.hadoop.io.longwritable; import org.apache.hadoop.io.text; importar org.apache.hadoop.mapreduce.job; import.apache.hadoop.mapreduce; import; import; import; import; import; importar; import; org.apache.hadoop.mapreduce.mapper.context; import org.apache.hadoop.mapreduce.reducer; importar org.apache.hadoop.mapreduce.lib.input.fileInputFormat; import org.apache.hadoop.mapreduce.lib.output.fileOutputformat; importación; importación; import org.apache.hadoop.util.tool; importar org.apache.hadoop.util.toolrunner; import org.jboss.netty.util.internal.stringutil; public class provinceCountMapreduce extiende la herramienta de implementos configurados {//1.map/** <keyin, valoros, llave, valueut>*/público static classalT. Mapper <longwritable, text, intwritable, intwritable> {private intwritable mapoutputkey = new intwritable (); private intwritable mapoutputPutValue = new intwritable (1);@mapa de voides nocturno de largo = value.ToString (); // splitString [] strs = lineValue.split ("/t"); // Línea en blanco 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 (ProvinceDValue);} Catch (Exception e) {return;} if (ProvinceID == Integer.max_value) {return;} mapoutputkey.set (provinceid); context.write (mapoutputkey,, mapOutputValue);}} // 2. Clase estática estática pública WordCountreduce extiende Reductor <intwritable, intwritable, intwritable, intwritable> {private intwritable outwritable outwritable outwritable oxtitione doin 0; for (valor intwritable: valores) {sum += value.get ();} outputValue.set (sum); context.write (key, outputValue);}} public int run (string [] args) lanza excepción {// 1.get ConfigurationConfiguration conf = super.getConf (); // 2.Create JobJob this.getClass (). mApperJob.SetMapperClass (WordCountMapper.Class); Job.SetMapoutPutKeyClass (intwritable.class); Job.SetMapoutputValueClass (intwritable.class); // 3.3 set RedeJob.SetEducerClass (WordCounTReduce.Class); Job.SetOutPutKeyClass (intwritable.class); Job.SetOutputValueclass (intwritable.class); // 3.4 Estable = Job.WaitForCompletion (verdadero); return issuccess? 0: 1;} public static void main (string [] args) arroja excepción {args = nuevo Cadena [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/input/2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020/outbut15/" status = Toolrunner.run (conf, New ProvCeCountMapreduce (), args); System.exit (status);}} 3. Resultados de la operación
1) Ejecutar código: bin/hdfs dfs -text/output15/par*
2) Resultados de la operación:
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
Lo anterior es una introducción relacionada con el número de visitas de usuarios en diferentes provincias en el sitio web de estadísticas de código Java que le presenta el editor. Espero que te sea útil. ¡Aquí, el editor también agradece a todos por su apoyo al sitio web de Wulin Network!