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

所属分类: 软件编程 / java 阅读数: 28
收藏 0 赞 0 分享

一、需求

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

二、编程代码

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);
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//get lineValue
String lineValue =value.toString();
//split
String[] strs =lineValue.split("\t");
//line blank
String url=strs[1];
String provinceIdValue =strs[23];
//guolv
if(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.reduce
public static class WordCountReduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
private IntWritable outputValue =new IntWritable();
@Override
public void reduce(IntWritable key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
//to do
int 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 Configuration
Configuration conf =super.getConf();
//2.create job
Job job =Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(ProvinceCountMapReduce.class);
//3.set job
//3.1 set input
Path inputPath =new Path(args[0]);
FileInputFormat.addInputPath(job, inputPath);
//3.2 set mapper
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
//3.3 set reduce
job.setReducerClass(WordCountReduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
//3.4 set input
Path outputPath =new Path(args[1]);
FileOutputFormat.setOutputPath(job, outputPath);
//4.submmit
boolean 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、运行结果

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

更多精彩内容其他人还在看

Springmvc restful配置遇到的小坑

本文是小编给大家带了的Springmvc restful配置遇到的小小坑,小编给大家带来了问题原因及解决办法,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
收藏 0 赞 0 分享

Java中的匿名内部类小结

java内部类分为: 成员内部类、静态嵌套类、方法内部类、匿名内部类。这篇文章主要介绍了Java中的匿名内部类的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的云打印Lodop

这篇文章主要介绍了Java的云打印Lodop 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程池框架核心代码解析

这篇文章主要针对Java线程池框架核心代码进行详细解析,分析Java线程池框架的实现ThreadPoolExecutor,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java 交换两个变量的数值实现方法

下面小编就为大家带来一篇Java 交换两个变量的数值实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

全面了解JAVA_BaseDAO数据处理类

下面小编就为大家带来一篇全面了解JAVA_BaseDAO数据处理类。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java、python、JavaScript以及jquery循环语句的区别

本篇文章主要介绍java、python、JavaScript以及jquery的循环语句的区别,这里整理了它们循环语句语法跟示例,以便大家阅读,更好的区分它们的不同
收藏 0 赞 0 分享

基于JDBC封装的BaseDao(实例代码)

下面小编就为大家带来一篇基于JDBC封装的BaseDao(实例代码)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

简单通用JDBC辅助类封装(实例)

下面小编就为大家带来一篇简单通用JDBC辅助类封装(实例)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈java线程中生产者与消费者的问题

下面小编就为大家带来一篇浅谈java线程中生产者与消费者的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多