使用Hadoop MapReduce分析邮件日志提取 id、状态 和 目标邮箱

使用Hadoop MapReduce分析邮件日志提取 id、状态 和 目标邮箱

在大数据处理和分析的场景中,Hadoop MapReduce是一种常见且高效的工具。本文将展示如何使用Hadoop MapReduce来分析邮件日志,提取邮件的发送状态(成功、失败或退回)和目标邮箱。

项目结构

我们将创建一个Java项目,该项目包含三个主要部分:

**Mapper类:**解析邮件日志,提取ID、状态和目标邮箱。

**Reducer类:**汇总Mapper输出的数据,生成最终结果。

**Driver类:**配置和运行MapReduce作业。

数据格式

我们将处理的邮件日志示例如下:

在这些日志中,我们需要提取邮件的ID、发送状态(成功、失败或退回)和目标邮箱。

代码实现

以下是完整的Java代码,包含Mapper、Reducer和Driver类:

java 复制代码
package org.example.mapReduce;

import java.io.IOException;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MailLogAnalysis {

    public static class MailLogMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();

            if (line.contains("starting delivery")) {
                String[] parts = line.split(" ");
                String id = parts[3].replace(":", "");
                String targetEmail = parts[8];
                context.write(new Text(id), new Text("email," + targetEmail));
            }

            if (line.contains("success") || line.contains("failure") || line.contains("bounce")) {
                String status = "success";
                if (line.contains("failure")) {
                    status = "failure";
                }
                if (line.contains("bounce")) {
                    status = "bounce";
                }
                String[] parts = line.split(" ");
                String id = parts[2].replace(":", "");
                context.write(new Text(id), new Text("status," + status));
            }
        }
    }

    public static class MailLogReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            String email = "";
            String status = "failure";
            for (Text val : values) {
                String[] parts = val.toString().split(",", 2);
                if (parts[0].equals("email")) {
                    email = parts[1];
                } else if (parts[0].equals("status")) {
                    status = parts[1];
                }
            }
            context.write(key, new Text(status + "," + email));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Mail Log Analysis");
        job.setJarByClass(MailLogAnalysis.class);
        job.setMapperClass(MailLogMapper.class);
        job.setReducerClass(MailLogReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

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

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

使用Hadoop MapReduce分析邮件日志

在大数据处理和分析的场景中,Hadoop MapReduce是一种常见且高效的工具。本文将展示如何使用Hadoop MapReduce来分析邮件日志,提取邮件的发送状态(成功、失败或退回)和目标邮箱。我们将通过一个具体的例子来实现这一目标。

项目结构

我们将创建一个Java项目,该项目包含三个主要部分:

Mapper类:解析邮件日志,提取ID、状态和目标邮箱。

Reducer类:汇总Mapper输出的数据,生成最终结果。

Driver类:配置和运行MapReduce作业。

数据格式

我们将处理的邮件日志示例如下:

less

复制代码

@400000004faa61e21e8e3e24 starting delivery 1820: msg 850901 to remote sunkang@189.cn

@400000004faa61e536864a44 delivery 1820: success: 121.14.53.136_accepted_message./Remote_host_said:_250_Ok:_queued_as_43A2222C006/

@400000004faa61e70a73c60c delivery 1823: deferral: 210.32.157.174_failed_after_I_sent_the_message./Remote_host_said:_450_Requested_action_not_taken:_AQAAf5CrT+qlYqpPamRUAA--.7571S2,_please_try_again/

@400000004faa61e70a73c60c bounce 1824: 550 Mailbox not found

在这些日志中,我们需要提取邮件的ID、发送状态(成功、失败或退回)和目标邮箱。

代码实现

以下是完整的Java代码,包含Mapper、Reducer和Driver类:

java

复制代码

package org.example.mapReduce;

import java.io.IOException;

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.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MailLogAnalysis {

复制代码
public static class MailLogMapper extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();

        if (line.contains("starting delivery")) {
            String[] parts = line.split(" ");
            String id = parts[3].replace(":", "");
            String targetEmail = parts[8];
            context.write(new Text(id), new Text("email," + targetEmail));
        }

        if (line.contains("success") || line.contains("failure") || line.contains("bounce")) {
            String status = "success";
            if (line.contains("failure")) {
                status = "failure";
            }
            if (line.contains("bounce")) {
                status = "bounce";
            }
            String[] parts = line.split(" ");
            String id = parts[2].replace(":", "");
            context.write(new Text(id), new Text("status," + status));
        }
    }
}

public static class MailLogReducer extends Reducer<Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        String email = "";
        String status = "failure";
        for (Text val : values) {
            String[] parts = val.toString().split(",", 2);
            if (parts[0].equals("email")) {
                email = parts[1];
            } else if (parts[0].equals("status")) {
                status = parts[1];
            }
        }
        context.write(key, new Text(status + "," + email));
    }
}

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "Mail Log Analysis");
    job.setJarByClass(MailLogAnalysis.class);
    job.setMapperClass(MailLogMapper.class);
    job.setReducerClass(MailLogReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

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

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

}

代码解释

Mapper类

MailLogMapper类从日志中提取邮件的ID、目标邮箱和发送状态,并将这些信息作为键值对输出:

如果行包含"starting delivery",则提取邮件的ID和目标邮箱,并输出键值对<ID, email, 目标邮箱>。

如果行包含"success"、"failure"或"bounce",则提取邮件的ID和发送状态,并输出键值对<ID, status, 发送状态>。
Reducer类

MailLogReducer类汇总Mapper输出的数据,生成最终的结果:

对于每个邮件ID,汇总对应的目标邮箱和发送状态。

输出包含ID、发送状态和目标邮箱的最终结果。
Driver类

MailLogAnalysis类配置和运行MapReduce作业:

设置作业名称、Mapper类和Reducer类。

设置输入路径和输出路径。

提交作业并等待完成。

MapReduce运行结果

总结

通过本文的示例,我们展示了如何使用Hadoop MapReduce来分析邮件日志,提取邮件的发送状态和目标邮箱。希望本文能为您的大数据处理和分析工作提供一些帮助。

如有遇到问题可以找小编沟通交流哦。另外小编帮忙辅导大课作业,学生毕设等。不限于MapReduce, MySQL, python,java,大数据,模型训练等。 hadoop hdfs yarn spark Django flask flink kafka flume datax sqoop seatunnel echart可视化 机器学习等

相关推荐
朗心心理4 小时前
朗心科技:以数智化引领心理健康服务新标杆
大数据·人工智能·科技·心理健康·朗心科技·数智化心理育人·一站式心理中心建设
无忧智库5 小时前
破局与重构:大型集团化协同管理平台的全景式深度解构(PPT)
大数据
码云数智-大飞6 小时前
进程、线程与协程:并发模型的演进与 Go 语言的 GMP 革命
大数据
XiaoMu_0017 小时前
基于大数据的糖尿病数据分析可视化
大数据·数据挖掘·数据分析
阿里云大数据AI技术7 小时前
Celeborn 如何让 EMR Serverless Spark 的 Shuffle 舒心、放心、安心
大数据·spark
AI营销快线8 小时前
AI营销获客难?原圈科技深度解析SaaS系统增长之道
大数据·人工智能
星幻元宇VR8 小时前
VR环保学习机|科技助力绿色教育新模式
大数据·科技·学习·安全·vr·虚拟现实
CryptoPP9 小时前
开发者指南:构建实时期货黄金数据监控系统
大数据·数据结构·笔记·金融·区块链
ZGi.ai10 小时前
生产级 Agent 编排 从单一 LLM 调用到多智能体工作流的工程设计
大数据·数据库·人工智能
天远数科10 小时前
分布式系统实战:基于天远二手车估值API构建高可用车辆估值微服务
大数据·微服务·云原生·架构