继续做Hadoop实现的电商实战
项目要求
根据电商日志文件,分析:1.统计页面浏览量(每行记录就是一次浏览)
2.统计各个省份的浏览量 (需要解析IP)
3.日志的ETL操作(ETL:数据从来源端经过抽取(Extract)、转换(Transform)、加载(Load)至目的端的过程)
为什么要ETL:没有必要解析出所有数据,只需要解析出有价值的字段即可。本项目中需要解析出:ip、url、pageId(topicId对应的页面Id)、country、province、city
2.统计各个省份的浏览量 (需要解析IP)
直接调用工具类
Mapper类
package dblab.test2;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import utils.*;
import java.io.IOException;
import java.util.Map;
public class MyMapper2 extends Mapper<LongWritable, Text, Text, LongWritable> {
private LongWritable ONE = new LongWritable(1);
private LogParser logParser;
@Override
protected void setup(Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
logParser = new LogParser();
}
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
String log = value.toString();
Map<String,String> info = logParser.parse(log);
String ip = info.get("ip");
if (StringUtils.isNotBlank(ip)) {
IPParser.RegionInfo regionInfo = IPParser.getInstance().analyseIp(ip);
if (regionInfo != null) {
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);
}
} else {
context.write(new Text("-"), ONE);
}
}
}
Reducer类
package dblab.test2;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class MyReduce2 extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
int count = 0;
for (LongWritable value:values){
count++;
}
context.write(key,new LongWritable(count));
}
}
driver类
package dblab.test2;
import dblab.test1.Main;
import org.apache.hadoop.conf.Configuration;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class ProvinceApp {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(Main.class);
job.setMapperClass(MyMapper2.class);
job.setReducerClass(MyReduce2.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job, new Path("input/trackinfo_20130721.txt"));
FileOutputFormat.setOutputPath(job, new Path("output/ot2"));
boolean result = job.waitForCompletion(true);
System.out.println(result?"ok":"0");
}
}
本质还是WordCount,唯一的区别是有查不到所属省份的浏览记录,无法查到的话在mapper阶段的key置为“-”,其他都和WordCount一样,解析ip的过程可以直接调用工具类