1. 程式人生 > >大資料專案之電信客服二

大資料專案之電信客服二

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

這樣就可以看到執行效果了

在命令最後加上&還可以在後臺執行該程式