浏览量统计功能实现
统计页面的浏览量
count
一行记录做成一个固定的KEY,value赋值为1
PVStatApp
点击查看代码
package com.imooc.bigdata.hadoop.mr.project.mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/**
* 第一版本浏览量的统计
*/
public class PVStatApp {
public static void main(String[] args) throws Exception{
Configuration configuration = new Configuration();
// 如果输出目录已经存在,则先删除
FileSystem fileSystem = FileSystem.get(configuration);
Path outputPath = new Path("output/v1/pvstat");
if(fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath,true);
}
Job job = Job.getInstance(configuration);
job.setJarByClass(PVStatApp.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job,new Path("input/raw/trackinfo_20130721.data"));
FileOutputFormat.setOutputPath(job,new Path("output/v1/pvstat"));
job.waitForCompletion(true);
}
static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable> {
private Text KEY = new Text("key");
private LongWritable ONE = new LongWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(KEY,ONE);
}
}
static class MyReducer extends Reducer<Text,LongWritable,NullWritable,LongWritable>{
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable value:values){
count++;
}
context.write(NullWritable.get(),new LongWritable(count));
}
}
}
- Linux查看文件行数
wc -l 文件名
省份浏览量统计
1.IP库解析
统计各个省份的浏览量
select province count(1) from xxx group by province;
城市信息我们是可以通过ip解析得到的 <== ip如何转换成城市信息
ip解析:提供一个IP数据库QQWry.dat,用于将IP地址转换为相应的地理位置信息。
- IPTest
package com.imooc.bigdata.hadoop.mr.project.utils;
import org.junit.Test;
public class IPTest {
@Test
public void testIP(){
IPParser.RegionInfo regionInfo = IPParser.getInstance().analyseIp("123.116.60.97");
System.out.println(regionInfo.getCountry());
System.out.println(regionInfo.getProvince());
System.out.println(regionInfo.getCity());
}
}
2.日志解析
数据集展示:
- LogParseTest
public class LogParseTest {
LogParser logParser;
@Before
public void setUp(){
logParser = new LogParser();
}
@After
public void tearDown(){
logParser = null;
}
@Test
public void test01(){
Map<String, String> map = logParser.parse("20946835322\u0001http://www.yihaodian.com/1/?tracker_u=2225501&type=3\u0001http://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235\u00011号店\u00011\u0001SKAPHD3JZYH9EE9ACB1NGA9VDQHNJMX1NY9T\u0001\u0001\u0001\u0001\u0001PPG4SWG71358HGRJGQHQQBXY9GF96CVU\u00012225501\u0001\\N\u0001124.79.172.232\u0001\u0001msessionid:YR9H5YU7RZ8Y94EBJNZ2P5W8DT37Q9JH,unionKey:2225501\u0001\u00012013-07-21 09:30:01\u0001\\N\u0001http://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235\u00011\u0001\u0001\\N\u0001null\u0001-10\u0001\u0001\u0001\u0001\u0001Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; MATP; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)\u0001Win32\u0001\u0001\u0001\u0001\u0001\u0001上海市\u00011\u0001\u00012013-07-21 09:30:01\u0001上海市\u0001\u000166\u0001\u0001\u0001\u0001\u0001\\N\u0001\\N\u0001\\N\u0001\\N\u00012013-07-21\n");
for(Map.Entry<String,String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
测试:
3.省份浏览量统计功能的实现
点击查看代码
package com.imooc.bigdata.hadoop.mr.project.mr;
import com.imooc.bigdata.hadoop.mr.project.utils.IPParser;
import com.imooc.bigdata.hadoop.mr.project.utils.LogParser;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.Map;
public class ProvinceStatApp {
// Driver端的代码:八股文
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
// 如果输出目录已经存在,则先删除
FileSystem fileSystem = FileSystem.get(configuration);
Path outputPath = new Path("output/v1/provicestat");
if(fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath,true);
}
Job job = Job.getInstance(configuration);
job.setJarByClass(ProvinceStatApp.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job, new Path("/Users/rocky/data/trackinfo_20130721.data"));
FileOutputFormat.setOutputPath(job, new Path("output/v1/provicestat"));
job.waitForCompletion(true);
}
static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
private LogParser parser;
private LongWritable ONE = new LongWritable(1);
@Override
protected void setup(Context context) throws IOException, InterruptedException {
parser = new LogParser();
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String log = value.toString();
Map<String, String> logInfo = parser.parse(log);
if (StringUtils.isNotBlank(logInfo.get("ip"))) {
IPParser.RegionInfo regionInfo = IPParser.getInstance().analyseIp(logInfo.get("ip"));
String province = regionInfo.getProvince();
if (StringUtils.isNotBlank(province)) {
context.write(new Text(province), ONE);
} else {
context.write(new Text("-"), ONE);
}
} else {
context.write(new Text("-"), ONE);
}
}
}
static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable access : values) {
count++;
}
context.write(key, new LongWritable(count));
}
}
}