大数据项目之电信客服二

1.数据生产

在实际生产中,这个环节并不会让你来做,更不会来模拟生产数据,但是这里是为了项目运行,也是为了让你连接每个环节

2.项目创建

在IDEA中先创建一个Java项目ct,然后在该项目中建立各个Module模块进行编写,这样比较清晰,比如这里的生产数据模块就可以建立一个ct_producer的Maven模块

由于生产数据模块不是重点,只是作为实际生产环境中的一部分,所以这里不做过多说明,直接放出源码,可以直接创建项目复制该代码运行测试即可

package producer;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ProductLog {

    private String startTime = "2017-01-01";
    private String endTime = "2017-12-31";

    //生产数据
    //用于存放待随机的电话号码
    private List<String> phoneList = new ArrayList<String>();
    private Map<String, String> phoneNameMap = new HashMap<String, String>();

    public void initPhone() {
        phoneList.add("17078388295");
        phoneList.add("13980337439");
        phoneList.add("14575535933");
        phoneList.add("19902496992");
        phoneList.add("18549641558");
        phoneList.add("17005930322");
        phoneList.add("18468618874");
        phoneList.add("18576581848");
        phoneList.add("15978226424");
        phoneList.add("15542823911");
        phoneList.add("17526304161");
        phoneList.add("15422018558");
        phoneList.add("17269452013");
        phoneList.add("17764278604");
        phoneList.add("15711910344");
        phoneList.add("15714728273");
        phoneList.add("16061028454");
        phoneList.add("16264433631");
        phoneList.add("17601615878");
        phoneList.add("15897468949");

        phoneNameMap.put("17078388295", "李雁");
        phoneNameMap.put("13980337439", "卫艺");
        phoneNameMap.put("14575535933", "仰莉");
        phoneNameMap.put("19902496992", "陶欣悦");
        phoneNameMap.put("18549641558", "施梅梅");
        phoneNameMap.put("17005930322", "金虹霖");
        phoneNameMap.put("18468618874", "魏明艳");
        phoneNameMap.put("18576581848", "华贞");
        phoneNameMap.put("15978226424", "华啟倩");
        phoneNameMap.put("15542823911", "仲采绿");
        phoneNameMap.put("17526304161", "卫丹");
        phoneNameMap.put("15422018558", "戚丽红");
        phoneNameMap.put("17269452013", "何翠柔");
        phoneNameMap.put("17764278604", "钱溶艳");
        phoneNameMap.put("15711910344", "钱琳");
        phoneNameMap.put("15714728273", "缪静欣");
        phoneNameMap.put("16061028454", "焦秋菊");
        phoneNameMap.put("16264433631", "吕访琴");
        phoneNameMap.put("17601615878", "沈丹");
        phoneNameMap.put("15897468949", "褚美丽");
    }

    /**
     * 形式:15837312345,13737312345,2017-01-09 08:09:10,0360
     */
    public String product() {
        String caller = null;
        String callee = null;

        String callerName = null;
        String calleeName = null;

        //取得主叫电话号码
        int callerIndex = (int) (Math.random() * phoneList.size());
        caller = phoneList.get(callerIndex);
        callerName = phoneNameMap.get(caller);
        while (true) {
            //取得被叫电话号码
            int calleeIndex = (int) (Math.random() * phoneList.size());
            callee = phoneList.get(calleeIndex);
            calleeName = phoneNameMap.get(callee);
            if (!caller.equals(callee)) break;
        }

        String buildTime = randomBuildTime(startTime, endTime);
        //0000
        DecimalFormat df = new DecimalFormat("0000");
        String duration = df.format((int) (30 * 60 * Math.random()));
        StringBuilder sb = new StringBuilder();
        return caller + "," +  "," + callee + "," +  "," + buildTime + "," + duration;
    }

    /**
     * 根据传入的时间区间,在此范围内随机通话建立的时间
     * startTimeTS + (endTimeTs - startTimeTs) * Math.random();
     *
     * @param startTime
     * @param endTime
     */
    public String randomBuildTime(String startTime, String endTime) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        try {
            startDate = sdf1.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endDate = null;
        try {
            endDate = sdf1.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (endDate.getTime() <= startDate.getTime()) return null;

        long randomTS = startDate.getTime() + (long) ((endDate.getTime() - startDate.getTime()) * Math.random());
        Date resultDate = new Date(randomTS);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String resultTimeString = sdf2.format(resultDate);
        return resultTimeString;
    }


    /**
     * 将数据写入到文件中
     */
    public void writeLog(String filePath) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
            while (true) {
                Thread.sleep(10);
                String log = product();
                System.out.println(log);
                osw.write(log + "\n");
                //一定要手动flush才可以确保每条数据都写入到文件一次
                osw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ProductLog log = new ProductLog();
       log.initPhone();

        log.writeLog(args[0]);
    }
}

3.运行测试

使用Maven打包该程序,然后将jar包上传到集群机器上,使用下面命令运行该程序

java -cp ct_producer-1.0-SNAPSHOT.jar producer.ProductLog /opt/package/log.csv

这样就可以看到运行效果了

在命令最后加上&还可以在后台运行该程序

猜你喜欢

转载自blog.csdn.net/ys_230014/article/details/84637598