1. Requisitos
Para as informações fornecidas no log de log, conte o número de visitas aos usuários em diferentes províncias do site.
2. Código de programação
pacote org.apache.hadoop.studyhdfs.mapreduce; importar java.io.ioException; importar org.apache.commons.lang.stringutils; importar org.apache.hadoop.conf.configature; org.apache.hadoop.fs.path; importar org.apache.hadoop.io.intwritable; importar org.apache.hadoop.io.longwritable; importar org.apache.hadoop.io.text; importar org.apache.hadoop.mapreduce.job; import.appache.apache.apache.hadoop.mapreduce.job; importar; org.apache.hadoop.mapreduce.mapper.Context; importar org.apache.hadoop.mapreduce.reducer; importar org.apache.hadoop.mapreduce.lib.input.fileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileIrput.FileOrt.Fileout.FileOut; 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 Intwritável mapOutputKey = new IntWritable (); private Intwrity MapOutputValue = new IntWritable (1);@SubsteridePublic Void Map (Chave Longwrity, Valor de Texto, Contexto) lança IoException, interruptedException {// obting lineValuestring line = value.ToString (); // Splitsring [StretString [] [] linha de linha); BlankString url = strs [1]; String ProvinceIdValue = strs [23]; // guolvif (strs.length <30 || Stringutils.isblank (ProvinceIdValue) || Stringutils.isblank (url)) {return; } int ProvinceId = Integer.max_value; tente {ProvinceID = Integer.Valueof (ProvinceIdValue);} Catch (Exceção e) {return;} if (ProvinceId == Integer.max_value) {return;} maputKey.SetKey (provrince); class WordCountReduce estende o redutor <intwenhas, intransigável, intrawável, intrawável> {private IntWritable OutputValue = new IntWritable ();@SubstridePublic void Reduce (Chave IntWidade, Iterable <intwenCht> Valores, contexto) lança IOException, InteruptException {// ToTeng. += value.get ();} outputValue.set (soma); context.write (key, output);}} public int run (string [] args) lança exceção {// 1.get confiativeConfiguration conf = super.getConf (); // 2.create jobjob job = job.getInstance (conf, this.getclass (). getSImplename ()); job.setJarbyclass (ProvincecountMapreduce.class); // 3.Set Job // 3.1 Definir inputPath inputPath = new Path (args [0]); FileInputFormat.addinputPath (Job, inputPath); // 3.2 set mapperjob.setMapperClass (WordCountMapper.Class); Job.SetMaputputKeyClass (intwritable.class); Job.SetMaputputValuEclass (intwritable.class); // 3.3 Conjunto ReducedJob.SetReducClass (WordCountReduce.class); Job.SetOutputKeyClass (intWritable.class); Job.SetOutputVputValuEclass (intWity.class); // 3.4 Set InputPathPathPathPath = new Path (args [1]); FileOrsputFormForm.SetOutppTpathPathPathPath); = job.waitforCompletion (true); retorno issuccess? 0: 1;} public static void main (string [] args) lança exceção {args = novo String [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/input/2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020/outduce15/"}; configature conf = new configatury (); configpress "; Status = ToolRunner.run (conf, new ProvinceCountMapreduce (), args); System.exit (status);}} 3. Resultados da operação
1) Código de execução: bin/hdfs dfs -text/output15/par*
2) Resultados da operação:
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
O exposto acima é uma introdução relacionada ao número de visitas de usuários em diferentes províncias no site de estatísticas de código Java, apresentado a você pelo editor. Espero que seja útil para você. Aqui, o editor também agradece a todos pelo apoio ao site da Wulin Network!