当前位置: 移动技术网 > IT编程>开发语言>Java > Java代码统计网站中不同省份用户的访问数

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

好瘦嘉嘉董事招代理,许昌市魏都区邮编,马靴吧

一、需求

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网