Day8实习日记

继续做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的过程可以直接调用工具类

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值