在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java代码统计网站中不同省份用户的访问数

2016-7-29 15:34| 发布者: zhangjf| 查看: 693| 评论: 0

摘要: 一、需求 针对log日志中给定的信息,统计网站中不同省份用户的访问数 二、编程代码 package org.apache.hadoop.studyhdfs.mapreduce;import java.io.IOException;import org.apache.commons.lang.StringUtils;imp ...

一、需求

针对log日志中给定的信息,统计网站中不同省份用户的访问数

二、编程代码

  1. package org.apache.hadoop.studyhdfs.mapreduce;
  2. import java.io.IOException;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.conf.Configured;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Mapper.Context;
  13. import org.apache.hadoop.mapreduce.Reducer;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. import org.apache.hadoop.util.Tool;
  17. import org.apache.hadoop.util.ToolRunner;
  18. import org.jboss.netty.util.internal.StringUtil;
  19. public class ProvinceCountMapReduce extends Configured implements Tool {
  20. //1.map
  21. /*
  22. * <KEYIN,VALUEIN,KEYOUT,VALUEOUT>
  23. */
  24. public static class WordCountMapper extends Mapper<LongWritable,Text,IntWritable,IntWritable>{
  25. private IntWritable mapOutputKey =new IntWritable();
  26. private IntWritable mapOutputValue =new IntWritable(1);
  27. @Override
  28. public void map(LongWritable key, Text value, Context context)
  29. throws IOException, InterruptedException {
  30. //get lineValue
  31. String lineValue =value.toString();
  32. //split
  33. String[] strs =lineValue.split("t");
  34. //line blank
  35. String url=strs[1];
  36. String provinceIdValue =strs[23];
  37. //guolv
  38. if(strs.length < 30 || StringUtils.isBlank(provinceIdValue) || StringUtils.isBlank(url)){
  39. return;
  40. }
  41. int provinceId =Integer.MAX_VALUE;
  42. try {
  43. provinceId=Integer.valueOf(provinceIdValue);
  44. } catch (Exception e) {
  45. return;
  46. }
  47. if(provinceId == Integer.MAX_VALUE){
  48. return;
  49. }
  50. mapOutputKey.set(provinceId);
  51. context.write(mapOutputKey, mapOutputValue);
  52. }
  53. }
  54. //2.reduce
  55. public static class WordCountReduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
  56. private IntWritable outputValue =new IntWritable();
  57. @Override
  58. public void reduce(IntWritable key, Iterable<IntWritable> values,Context context)
  59. throws IOException, InterruptedException {
  60. //to do
  61. int sum = 0;
  62. for(IntWritable value:values){
  63. sum +=value.get();
  64. }
  65. outputValue.set(sum);
  66. context.write(key, outputValue);
  67. }
  68. }
  69. public int run(String[] args) throws Exception{
  70. //1.get Configuration
  71. Configuration conf =super.getConf();
  72. //2.create job
  73. Job job =Job.getInstance(conf, this.getClass().getSimpleName());
  74. job.setJarByClass(ProvinceCountMapReduce.class);
  75. //3.set job
  76. //3.1 set input
  77. Path inputPath =new Path(args[0]);
  78. FileInputFormat.addInputPath(job, inputPath);
  79. //3.2 set mapper
  80. job.setMapperClass(WordCountMapper.class);
  81. job.setMapOutputKeyClass(IntWritable.class);
  82. job.setMapOutputValueClass(IntWritable.class);
  83. //3.3 set reduce
  84. job.setReducerClass(WordCountReduce.class);
  85. job.setOutputKeyClass(IntWritable.class);
  86. job.setOutputValueClass(IntWritable.class);
  87. //3.4 set input
  88. Path outputPath =new Path(args[1]);
  89. FileOutputFormat.setOutputPath(job, outputPath);
  90. //4.submmit
  91. boolean isSuccess =job.waitForCompletion(true);
  92. return isSuccess?0:1;
  93. }
  94. public static void main(String[] args) throws Exception {
  95. args =new String[]{
  96. "hdfs://Hadoop-senior02.beifeng.com:8020/input/2015082818",
  97. "hdfs://Hadoop-senior02.beifeng.com:8020/output15/"
  98. };
  99. Configuration conf =new Configuration();
  100. conf.set("mapreduce.map.output.compress", "true");
  101. int status=ToolRunner.run(conf, new ProvinceCountMapReduce() , args);
  102. System.exit(status);
  103. }
  104. }
复制代码

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代码统计网站中不同省份用户的访问数的相关介绍,希望对大家有所帮助,在此小编也非常感谢大家对程序员之家网站的支持!

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:11

Copyright 2015-2025 djqfx

返回顶部