电商项目实战-省份浏览量统计之功能实现(六)

1、ProvinceStartApp.java

src/main/java/project/mrv1/ProvinceStartApp.java

package project.mrv1;

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.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 project.utils.IPParser;
import project.utils.LogParser;

import java.io.IOException;
import java.util.Map;

//省份浏览量统计
public class ProvinceStartApp {
    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();

        FileSystem fileSystem = FileSystem.get(configuration);
        Path outputPath = new Path("output/v1/pvstart");
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }

        Job job = Job.getInstance(configuration);
        job.setJarByClass(ProvinceStartApp.class);

        job.setMapperClass(ProvinceStartApp.MyMapper.class);
        job.setReducerClass(ProvinceStartApp.MyReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job, new Path("access/raw/input/trackinfo_20130721.data"));
        FileOutputFormat.setOutputPath(job, new Path("output/v1/pvstart"));

        job.waitForCompletion(true);

    }

    //Text:省份
    //LongWritable:量的统计
    static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        private LongWritable ONE = new LongWritable(1);
        private LogParser logParser;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            logParser = new LogParser();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //value:是日志
            //拿到日志
            String log = value.toString();
            //将日志传进来
            Map<String, String> info = logParser.parser(log);
            String ip = info.get("ip");  //拿到的是LogParser.java里的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);


            }
        }


    }

    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 value: values){
                count++;
            }
            context.write(key, new LongWritable(count));
        }
    }
}

posted @ 2021-07-23 16:58  酱汁怪兽  阅读(85)  评论(0编辑  收藏  举报