美文网首页
尚硅谷大数据技术之电信客服

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

作者: 尚硅谷教育 | 来源:发表于2018-12-19 17:27 被阅读27次
    1. 随机输入一些手机号码以及联系人,保存于Java的集合中。
      新建类:ProductLog
      //存放联系人电话与姓名的映射
      public Map<String, String> contacts = null;
      //存放联系人电话号码
      public List<String> phoneList = null;
      public void initContacts() {
      contacts = new HashMap<String, String>();
      phoneList = new ArrayList<String>();

      phoneList.add("15369468720");
      phoneList.add("19920860202");
      phoneList.add("18411925860");
      phoneList.add("14473548449");
      phoneList.add("18749966182");
      phoneList.add("19379884788");
      phoneList.add("19335715448");
      phoneList.add("18503558939");
      phoneList.add("13407209608");
      phoneList.add("15596505995");
      phoneList.add("17519874292");
      phoneList.add("15178485516");
      phoneList.add("19877232369");
      phoneList.add("18706287692");
      phoneList.add("18944239644");
      phoneList.add("17325302007");
      phoneList.add("18839074540");
      phoneList.add("19879419704");
      phoneList.add("16480981069");
      phoneList.add("18674257265");
      phoneList.add("18302820904");
      phoneList.add("15133295266");
      phoneList.add("17868457605");
      phoneList.add("15490732767");
      phoneList.add("15064972307");

      contacts.put("15369468720", "李雁");
      contacts.put("19920860202", "卫艺");
      contacts.put("18411925860", "仰莉");
      contacts.put("14473548449", "陶欣悦");
      contacts.put("18749966182", "施梅梅");
      contacts.put("19379884788", "金虹霖");
      contacts.put("19335715448", "魏明艳");
      contacts.put("18503558939", "华贞");
      contacts.put("13407209608", "华啟倩");
      contacts.put("15596505995", "仲采绿");
      contacts.put("17519874292", "卫丹");
      contacts.put("15178485516", "戚丽红");
      contacts.put("19877232369", "何翠柔");
      contacts.put("18706287692", "钱溶艳");
      contacts.put("18944239644", "钱琳");
      contacts.put("17325302007", "缪静欣");
      contacts.put("18839074540", "焦秋菊");
      contacts.put("19879419704", "吕访琴");
      contacts.put("16480981069", "沈丹");
      contacts.put("18674257265", "褚美丽");
      contacts.put("18302820904", "孙怡");
      contacts.put("15133295266", "许婵");
      contacts.put("17868457605", "曹红恋");
      contacts.put("15490732767", "吕柔");
      contacts.put("15064972307", "冯怜云");
      }

    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,月份:011,天:131
      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);
      }

    本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。

    相关文章

      网友评论

          本文标题:尚硅谷大数据技术之电信客服

          本文链接:https://www.haomeiwen.com/subject/tvajkqtx.html