【电商项目第三问】


一、创建类

在这里插入图片描述

二、代码如下:

LogETLMapper

import com.bigdata.hadoop.project.utils.GetPageId;
import com.bigdata.hadoop.project.utils.LogParser;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class LogETLMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private static final IntWritable one = new IntWritable(1);
    private Text outputKey = new Text();
    private LogParser logParser = new LogParser();
    private Logger logger = LoggerFactory.getLogger(LogETLMapper.class);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 解析日志记录
        Map<String, String> logInfo = logParser.parse(value.toString());

        if (logInfo == null) {
            logger.error("日志记录的格式不正确或解析失败:" + value.toString());
            return;
        }

        // 获取需要的字段
        String ip = logInfo.get("ip");
        String url = logInfo.get("url");
        String country = logInfo.get("country");
        String province = logInfo.get("province");
        String city = logInfo.get("city");

        // 调用 GetPageId 获取 topicId
        String topicId = GetPageId.getPageId(url);
        logInfo.put("pageId", topicId);

        // 检查所有字段是否全部为空
        if (ip != null || url != null || topicId != null || country != null || province != null || city != null) {
            StringBuilder sb = new StringBuilder();

            if (ip != null && !ip.isEmpty()) sb.append("IP: ").append(ip).append(", ");
            if (url != null && !url.isEmpty()) sb.append("URL: ").append(url).append(", ");
            if (topicId != null && !topicId.isEmpty()) sb.append("PageId: ").append(topicId).append(", ");
            if (country != null && !country.isEmpty()) sb.append("Country: ").append(country).append(", ");
            if (province != null && !province.isEmpty()) sb.append("Province: ").append(province).append(", ");
            if (city != null && !city.isEmpty()) sb.append("City: ").append(city);

            // 移除末尾的逗号和空格
            String outputString = sb.toString().replaceAll(", $", "");
            outputKey.set(outputString);
            context.write(outputKey, one);
        } else {
            logger.error("所有字段为空,日志记录:" + value.toString());
        }
    }
}



LogETLReducer

代码如下(示例):

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class LogETLReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable result = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        result.set(sum);
        context.write(key, result);
    }
}

LogETLDriver

代码如下(示例):

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;

public class LogETLDriver {

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: LogETLDriver <input path> <output path>");
            System.exit(-1);
        }

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Log ETL");

        job.setJarByClass(LogETLDriver.class);
        job.setMapperClass(LogETLMapper.class);
        job.setCombinerClass(LogETLReducer.class);
        job.setReducerClass(LogETLReducer.class);

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

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}



打jar包

查看结果

在这里插入图片描述

相关推荐

  1. 项目-day01

    2024-06-18 11:40:01       14 阅读
  2. 项目-day03

    2024-06-18 11:40:01       8 阅读
  3. 控制台增删改查接口的编写

    2024-06-18 11:40:01       44 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 11:40:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 11:40:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 11:40:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 11:40:01       20 阅读

热门阅读

  1. 如何在pandas创建一个seris结构?

    2024-06-18 11:40:01       9 阅读
  2. 使用 Web Share API 分享内容

    2024-06-18 11:40:01       9 阅读
  3. MLIR。

    2024-06-18 11:40:01       5 阅读
  4. 高级优化理论与方法(十五)

    2024-06-18 11:40:01       6 阅读
  5. Linux 网络请求工具:curl

    2024-06-18 11:40:01       4 阅读
  6. 系统日志排查:Linux应急响应与溯源技术

    2024-06-18 11:40:01       8 阅读
  7. Python - 一个恶意脚本

    2024-06-18 11:40:01       6 阅读
  8. Git 克隆 GitHub 仓库时遇到了 SSL 证书问题

    2024-06-18 11:40:01       8 阅读