尚硅谷大数据技术之电信客服

2) 创建随机生成通话时间的方法:randomDate

该时间生成后的格式为yyyy-MM-dd HH:mm:ss,并使之可以根据传入的起始时间和结束时间来随机生成。

//随机生成通话建立时间
private String randomDate(String startDate, String endDate) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date start = simpleDateFormat.parse(startDate);
        Date end = simpleDateFormat.parse(endDate);

        if (start.getTime() > end.getTime()) return null;

        long resultTime = start.getTime() + (long) (Math.random() * (end.getTime() - start.getTime()));
        return resultTime + "";
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

3) 创建生产日志一条日志的方法:productLog

随机抽取两个电话号码,随机产生通话建立时间,随机通话时长,将这几个字段拼接成一个字符串,然后return,便可以产生一条通话的记录。需要注意的是,如果随机出的两个电话号码一样,需要重新随机(随机过程可优化,但并非此次重点)。通话时长的随机为20分钟以内,即:60秒 * 20,并格式化为4位数字,例如:0600(10分钟)。

//拼接日志
private String productLog() {
    int call1Index = new Random().nextInt(phoneList.size());
    int call2Index = -1;

    String call1 = phoneList.get(call1Index);
    String call2 = null;
    while (true) {
        call2Index = new Random().nextInt(phoneList.size());
        call2 = phoneList.get(call2Index);
        if (!call1.equals(call2)) break;
    }
    //随机生成通话时长(30分钟内_0600)
    int duration = new Random().nextInt(60 * 30) + 1;

    //格式化通话时间,使位数一致
    String durationString = new DecimalFormat("0000").format(duration);
    //通话建立时间:yyyy-MM-dd,月份:0~11,天:1~31
    String randomDate = randomDate("2017-01-01", "2018-01-01");
    String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.parseLong(randomDate));

    //拼接log日志
    StringBuilder logBuilder = new StringBuilder();

    logBuilder.append(call1).append(",").append(call2).append(",").append(dateString).append(",")
            .append(durationString);
    System.out.println(logBuilder);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return logBuilder.toString();
}

 

4) 创建写入日志方法:writeLog

productLog每产生一条日志,便将日志写入到本地文件中,所以建立一个专门用于日志写入的方法,需要涉及到IO操作,需要注意的是,输出流每次写一条日之后需要flush,不然可能导致积攒多条数据才输出一次。最后需要将productLog方法放置于while死循环中执行。

//将产生的日志写入到本地文件calllog中
public void writeLog(String filePath, ProduceLog productLog) {
    OutputStreamWriter outputStreamWriter = null;
    try {
        outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8");
        while (true) {
            String log = productLog.productLog();
            outputStreamWriter.write(log + "\n");
            outputStreamWriter.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            assert outputStreamWriter != null;
            outputStreamWriter.flush();
            outputStreamWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

5) 在主函数中初始化以上逻辑,并测试:

public static void main(String[] args) {

    if(args == null || args.length <= 0) {

        System.out.println("no arguments");

        System.exit(1);

    }

    ProductLog productLog = new ProductLog();

    productLog.initContacts();

    productLog.writeLog(args[0], productLog);

}

3.1.3 打包测试

分别在Windows上和Linux中进行测试:

java -cp producer.jar com.atguigu.producer.ProductLog /本地目录/callLog.csv

为日志生成任务编写bash脚本:productlog.sh

#!/bin/bash

java -cp /home/atguigu/call/ producer.jar com.atguigu.producer.ProductLog /home/atguigu/call/calllog.csv