MapReduce综合应用案例 — 电信数据清洗

2023-12-20 07:05:29

文章目录


第1关:数据清洗

测试说明
平台会对你编写的代码进行测试:

评测之前先在命令行启动hadoop:start-all.sh;

点击测评后MySQL所需的数据库和表会自动创建好。

PhoneLog:封装对象
LogMR:MapReduce操作
DBHelper:MySQL工具类

具体本关的预期输出请查看右侧测试集。

因为大数据实训消耗资源较大,且map/reduce运行比较耗时,所以评测时间较长,大概在60秒左右,请耐心等待。
在这里插入图片描述
在该箭头所指的位置进行代码文件的切换。

代码文件如下:
LogMR类

package com;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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;
public class LogMR {
    /********** begin **********/
    static class MyMapper extends Mapper<LongWritable, Text, PhoneLog, NullWritable> {
        Map<String, String> userMap = new HashMap<>();
        Map<String, String> addressMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PhoneLog pl = new PhoneLog();
        Text text = new Text();
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            Connection connection = DBHelper.getConnection();
            try {
                Statement statement = connection.createStatement();
                String sql = "select * from userphone";
                ResultSet resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    String phone = resultSet.getString(2);
                    String trueName = resultSet.getString(3);
                    userMap.put(phone, trueName);
                }
                String sql2 = "select * from allregion";
                ResultSet resultSetA = statement.executeQuery(sql2);
                while (resultSetA.next()) {
                    String phone = resultSetA.getString(2);
                    String trueName = resultSetA.getString(3);
                    addressMap.put(phone, trueName);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            String[] split = str.split(",");
            if (split.length == 6) {
                String trueName1 = userMap.get(split[0]);
                String trueName2 = userMap.get(split[1]);
                String address1 = addressMap.get(split[4]);
                String address2 = addressMap.get(split[5]);
                long startTimestamp = Long.parseLong(split[2]);
                String startTime = sdf.format(startTimestamp * 1000);
                long endTimestamp = Long.parseLong(split[3]);
                String endTime = sdf.format(endTimestamp * 1000);
                long timeLen = endTimestamp - startTimestamp;
                pl.SetPhoneLog(trueName1, trueName2, split[0], split[1], startTime, endTime, timeLen, address1,
                        address2);
                context.write(pl, NullWritable.get());
            }
        }
    }
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(LogMR.class);
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(PhoneLog.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setNumReduceTasks(0);
        Path inPath = new Path("/user/test/input/a.txt");
        Path out = new Path("/user/test/output");
        FileInputFormat.setInputPaths(job, inPath);
        FileOutputFormat.setOutputPath(job, out);
        job.waitForCompletion(true);
    }
    /********** end **********/
}

DBHelper类

package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
    /********** begin **********/
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8";
    private static final String username = "root";// 数据库的用户名
    private static final String password = "123123";// 数据库的密码:这个是自己安装数据库的时候设置的,每个人不同。
    private static Connection conn = null; // 声明数据库连接对象
    static {
        
        try {
            Class.forName(driver);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static Connection getConnection() {
        if (conn == null) {
            try {
                conn = DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            } // 连接数据库
            return conn;
        }
        return conn;
    }
    /********** end **********/
}

PhoneLog类

package com;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
public class PhoneLog implements WritableComparable<PhoneLog> {
    private String userA;
    private String userB;
    private String userA_Phone;
    private String userB_Phone;
    private String startTime;
    private String endTime;
    private Long timeLen;
    private String userA_Address;
    private String userB_Address;
    public PhoneLog() {
    }
    public void SetPhoneLog(String userA, String userB, String userA_Phone, String userB_Phone, String startTime,
            String endTime, Long timeLen, String userA_Address, String userB_Address) {
        this.userA = userA;
        this.userB = userB;
        this.userA_Phone = userA_Phone;
        this.userB_Phone = userB_Phone;
        this.startTime = startTime;
        this.endTime = endTime;
        this.timeLen = timeLen;
        this.userA_Address = userA_Address;
        this.userB_Address = userB_Address;
    }
    public String getUserA_Phone() {
        return userA_Phone;
    }
    public void setUserA_Phone(String userA_Phone) {
        this.userA_Phone = userA_Phone;
    }
    public String getUserB_Phone() {
        return userB_Phone;
    }
    public void setUserB_Phone(String userB_Phone) {
        this.userB_Phone = userB_Phone;
    }
    public String getUserA() {
        return userA;
    }
    public void setUserA(String userA) {
        this.userA = userA;
    }
    public String getUserB() {
        return userB;
    }
    public void setUserB(String userB) {
        this.userB = userB;
    }
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
    public Long getTimeLen() {
        return timeLen;
    }
    public void setTimeLen(Long timeLen) {
        this.timeLen = timeLen;
    }
    public String getUserA_Address() {
        return userA_Address;
    }
    public void setUserA_Address(String userA_Address) {
        this.userA_Address = userA_Address;
    }
    public String getUserB_Address() {
        return userB_Address;
    }
    public void setUserB_Address(String userB_Address) {
        this.userB_Address = userB_Address;
    }
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(userA);
        out.writeUTF(userB);
        out.writeUTF(userA_Phone);
        out.writeUTF(userB_Phone);
        out.writeUTF(startTime);
        out.writeUTF(endTime);
        out.writeLong(timeLen);
        out.writeUTF(userA_Address);
        out.writeUTF(userB_Address);
    }
    @Override
    public void readFields(DataInput in) throws IOException {
        userA = in.readUTF();
        userB = in.readUTF();
        userA_Phone = in.readUTF();
        userB_Phone = in.readUTF();
        startTime = in.readUTF();
        endTime = in.readUTF();
        timeLen = in.readLong();
        userA_Address = in.readUTF();
        userB_Address = in.readUTF();
    }
    @Override
    public String toString() {
        return userA + "," + userB + "," + userA_Phone + "," + userB_Phone + "," + startTime + "," + endTime + ","
                + timeLen + "," + userA_Address + "," + userB_Address;
    }
     @Override
     public int compareTo(PhoneLog pl) {
     if(this.hashCode() == pl.hashCode()) {
     return 0;
     }
     return -1;
     }
}

之后在命令行启动hadoop

start-all.sh

在这里插入图片描述


文章来源:https://blog.csdn.net/2301_77225918/article/details/135079556
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。