程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java代碼統計網站中分歧省分用戶的拜訪數

Java代碼統計網站中分歧省分用戶的拜訪數

編輯:關於JAVA

Java代碼統計網站中分歧省分用戶的拜訪數。本站提示廣大學習愛好者:(Java代碼統計網站中分歧省分用戶的拜訪數)文章只能為提供參考,不一定能成為您想要的結果。以下是Java代碼統計網站中分歧省分用戶的拜訪數正文


1、需求

針對log日記中給定的信息,統計網站中分歧省分用戶的拜訪數

2、編程代碼

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代碼統計網站中分歧省分用戶的拜訪數的相干引見,願望對年夜家有所贊助,在此小編也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved