1. Требования
Для получения информации, представленной в журнале журнала, подсчитайте количество посещений пользователей в разных провинциях на веб -сайте.
2. Код программирования
пакет org.apache.hadoop.studyhdfs.mapreduce; import java.io.ioexception; импорт org.apache.commons.lang.stringutils; импорт org.apache.hadoop.conf.configuration; импорт org.apache.hadoop.configuration; import.codop.conf.confible.confible.confible.confignf.confible.confible.confible.confible.confible.confible.configerf.confible.confible.configerf.conficure.confible.configerf.com org.apache.hadoop.fs.path; import org.apache.hadoop.io.intwraite; import org.apache.hadoop.io.longwrable; import org.apache.hadoop.io.text; import org.apache.hadoop.mapreduce.job; import orgach.hadoop.mapred.mapper org.apache.hadoop.mapreduce.mapper.context; import org.apache.hadoop.mapreduce.reducer; import org.apache.hadoop.mapreduce.lib.input.fileInputFormat; import.Apache.hadoop.mapreduce.lib.output.fileOutputformat; org.apache.hadoop.util.toolrunner; import org.jboss.netty.util.internal.stringutil; public class provincecountmapreduce extends configenced intourt {//1.map/** <keyin, valuein, keyout, valueout>*/public Static WordCountmapper extends <longwrable, valueout>*/public Static wordcountmap Intrable MapoutputKey = new IntWraite (); Private Intrable MapoutputValue = new IntWraite (1);@overridePublic void map (давно написанный ключ, значение текста, контекст контекста) бросает ioException, прерывание. = lineValue.split ("/t"); // line blankstring url = strs [1]; String provinceidValue = strs [23]; // guolvif (strs.length <30 || StringUtils.isblank (провинция) || 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.set (provinceid); контекст. 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; for (intwrable value: values) {sum += value.get ();} outputValue.set (sum); контекст. this.getClass (). getSiMplename ()); job.setJarbyClass (provinceCountMapreduce.class); // 3.set job // 3.1 Установить inputPath inputPath = new Path (args [0]); mapperjob.setmapperclass (wordcountmapper.class); job.setmapoutputkeyclass (intwritember.class); job.setmapoutputvalueclass (intwritable.class); // 3.3 Set Reductjob.setreducerclass (wordcountreduce.class); job.setOutputKeclass (intwrite.class); job.setOutputValueClass (intWrite.Class); // 3.4 Установить inputPath outputPath = new Path (args [1]); fileOutputFormat.setOutpathpath (jabypath); = job.waitforComplotion (true); return issuccess? 0: 1;} public static void main (string [] args) выбрасывает исключение {args = new String [] {"hdfs: //hadoop-senior02.beifeng.com: 8020/input/2015082818", "hdfs: //hadoop-senior02.beifeng.com: 8020/output15/"}; configuration confuce = new Configuration (); conf.set ("mapreduce.map.output.com. New ProvinceCountMapreduce (), args); System.Exit (Status);}} 3. Результаты работы
1) Запустите код: bin/hdfs dfs -Text/output15/par*
2) Результаты работы:
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
Выше приведено связанное введение в количество посещений пользователей в разных провинциях на веб -сайте статистики кода Java, представленный вам редактором. Я надеюсь, что это будет полезно для вас. Здесь редактор также благодарит всех за поддержку веб -сайта Wulin Network!