国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java代碼統(tǒng)計網(wǎng)站中不同省份用戶的訪問數(shù)

2019-11-26 14:21:44
字體:
供稿:網(wǎng)友

一、需求

針對log日志中給定的信息,統(tǒng)計網(wǎng)站中不同省份用戶的訪問數(shù)

二、編程代碼

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.Configured;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;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;import 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,IntWritable,IntWritable>{private IntWritable mapOutputKey =new IntWritable();private IntWritable mapOutputValue =new IntWritable(1);@Overridepublic void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {//get lineValueString lineValue =value.toString();//splitString[] strs =lineValue.split("/t");//line 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;}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;for(IntWritable value:values){sum +=value.get();}outputValue.set(sum);context.write(key, outputValue);}}public int run(String[] args) throws Exception{//1.get ConfigurationConfiguration conf =super.getConf();//2.create jobJob job =Job.getInstance(conf, 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(IntWritable.class);job.setMapOutputValueClass(IntWritable.class);//3.3 set reducejob.setReducerClass(WordCountReduce.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(IntWritable.class);//3.4 set inputPath outputPath =new Path(args[1]);FileOutputFormat.setOutputPath(job, outputPath);//4.submmitboolean isSuccess =job.waitForCompletion(true);return isSuccess?0:1;}public static void main(String[] args) throws Exception {args =new String[]{"hdfs://Hadoop-senior02.beifeng.com:8020/input/2015082818","hdfs://Hadoop-senior02.beifeng.com:8020/output15/"};Configuration conf =new Configuration();conf.set("mapreduce.map.output.compress", "true");int status=ToolRunner.run(conf, new ProvinceCountMapReduce() , args);System.exit(status);}} 

3、運(yùn)行結(jié)果

1)運(yùn)行代碼:bin/hdfs dfs -text /output15/par*

2)運(yùn)行結(jié)果:

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代碼統(tǒng)計網(wǎng)站中不同省份用戶的訪問數(shù)的相關(guān)介紹,希望對大家有所幫助,在此小編也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 通榆县| 青岛市| 天祝| 栾川县| 察隅县| 威远县| 清远市| 鄂伦春自治旗| 阿城市| 湘阴县| 沙洋县| 大宁县| 万安县| 彭泽县| 瓦房店市| 文水县| 叙永县| 宝清县| 明星| 郸城县| 外汇| 犍为县| 武宣县| 新源县| 凤山县| 股票| 杂多县| 哈巴河县| 永安市| 南开区| 尼玛县| 兴安县| 内黄县| 安泽县| 浠水县| 罗定市| 曲水县| 大同市| 繁峙县| 芷江| 临夏市|