当前位置: 首页 > news >正文

基于Hadoop平台的电信客服数据的处理与分析④项目实现:任务15:数据生产

任务描述

电信数据生产是一个完整且严密的体系,这样可以保证数据的鲁棒性。在本项目的数据生产模块中,我们来模拟生产一些电信数据。同时,我们必须清楚电信数据的格式和数据结构,这样才能在后续的数据产生、存储、分析和展示环节中正确使用数据,避免可能出现的问题。

任务指导

使用Java代码模拟后续处理需要的数据,此处使用IDEA创建Maven项目在pom.xml文件中引入需要使用的类库和插件。

编写producer.ProductLog类动态生成需要的数据,并将程序打包后测试输出的数据。

任务实现

1、 数据结构

在项目中我们使用HBase来存储数据,HBase中存储的信息包括:两个电话号码,通话开始时间,通话持续时间,以及一个flag作为判断第一个电话号码是否为主叫,姓名字段的存储可以放置在另外一张表中做关联查询,也可以在同一个表中。

列名

说明

示例
call1第一个手机号码15933445689
call1_name第一个手机号码人的姓名李四
call2第二个手机号码18644889345
call2_name第二个手机号码人的姓名张三
date_time建立通话的时间201806291136
date_time_ts建立通话的时间(时间戳)
duration通话持续时间(秒)600
flag标记本次通话第一个字段(call1)是主叫还是被叫1 主叫;2 被叫;

2、 编写代码

在这里我们创建一个Java项目,来模拟电信客服务产生的数据,步骤如下:

1)  激活IDEA

在master1的桌面双击桌面的IDEA Ultimate

如图依次选择【Evaluate for free】=> 【Evaluate】,激活IDEA的30天免费使用权限

再次打开IDEA进入欢迎界面

2)   创建ct_producer项目

点击“New Project”新建一个Java的Maven项目,在“New Project”左侧选择“Maven”后为项目选择所需的“Project SDK”

点击“Next”按钮后在“Name”文本框中输入项目名称“ct_producer”后点击“Finish”按钮。

依次点击“File”->“Settings”进入“Settings”界面,如图为项目制定Maven的相关配置

打开项目的pom.xml文件配置对其进行配置,pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ct_producer</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.12.4</version><configuration><skipTests>true</skipTests></configuration></plugin></plugins></build>
</project>

如图依次点击“Maven”->"Reload all Maven Projects"后,等待“Build”标签同步(Sync)完成。

3)  完成生产者代码

创建producer包,并在此包创建ProductLog类

ProductLog.java类代码如下

package producer;import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;public class ProductLog {private String startTime = "2023-01-01";private String endTime = "2023-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);//0000DecimalFormat df = new DecimalFormat("0000");String duration = df.format((int) (30 * 60 * Math.random()));StringBuilder sb = new StringBuilder();sb.append(caller + ",").append(callee + ",").append(buildTime + ",").append(duration);return sb.toString();}/*** 根据传入的时间区间,在此范围内随机通话建立的时间* startTimeTS + (endTimeTs - startTimeTs) * Math.random();** @param startTime* @param endTime*/public String randomBuildTime(String startTime, String endTime) {try {SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");Date startDate = sdf1.parse(startTime);Date endDate = sdf1.parse(endTime);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;} catch (ParseException e) {e.printStackTrace();}return null;}/*** 将数据写入到文件中*/public void writeLog(String filePath) {try {OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");while (true) {Thread.sleep(500);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) throws InterruptedException {if (args == null || args.length <= 0) {System.out.println("no arguments");return;}ProductLog productLog = new ProductLog();productLog.initPhone();productLog.writeLog(args[0]);}
}

4)  打包:在IDEA的Maven Project视图中进行打包:

image.png

双击“LifeCycle ---package”对项目进行打包。

   打包成功后日志消息回显示“BUILD SUCCESS”,并在项目目录中生成target目录,此目录下包含了打包后的jar文件“ct_producer-1.0-SNAPSHOT.jar”

将“ct_producer-1.0-SNAPSHOT.jar”文件拷贝到/opt/app目录下,目录【/root/IdeaProjects/ct_producer/target】为创建项目时选择的默认目录,如进行了更改请根据实际目录进行操作。

[root@master1 ~]# cd /root/IdeaProjects/ct_producer/target
[root@master1 target]# cp ct_producer-1.0-SNAPSHOT.jar /opt/app/

可以编写bash脚本,用于执行脚本生成日志。

创建/opt/app/productlog.sh文件

[root@master1 ~]# touch /opt/app/productlog.sh

/opt/app/productlog.sh文件内容如下

#!/bin/bash
java -cp /opt/app/ct_producer-1.0-SNAPSHOT.jar producer.ProductLog /opt/app/callLog.csv

相关文章:

  • 【unity实战】使用Unity实现动作游戏的攻击 连击 轻重攻击和打击感
  • 航空数据管控系统-①项目准备阶段:任务2:项目技术预研(技术架构)
  • 对标 GPT-4o 的开源实时语音多模态模型:Moshi
  • 【BUUCTF-PWN】9-ciscn_2019_n_8
  • Matplotlib 简介
  • 康姿百德磁性床垫好不好,效果怎么样靠谱吗
  • Hadoop集群搭建
  • threejs 微信小程序原生版本的使用 obj模型的加载
  • 学会python——用python制作一个登录和注册窗口(python实例十八)
  • python-Django项目:图书后台管理系统
  • SpringMVC之文件上传下载
  • spring6框架解析(by尚硅谷)
  • 降级Spring Boot版本
  • Generative Modeling by Estimating Gradients of the Data Distribution
  • ChatGPT-5:开创对话式AI的新纪元
  • 【comparator, comparable】小总结
  • 【面试系列】之二:关于js原型
  • Django 博客开发教程 16 - 统计文章阅读量
  • interface和setter,getter
  • iOS 系统授权开发
  • java多线程
  • js操作时间(持续更新)
  • PHP CLI应用的调试原理
  • 不上全站https的网站你们就等着被恶心死吧
  • 浏览器缓存机制分析
  • 免费小说阅读小程序
  • 判断客户端类型,Android,iOS,PC
  • 前端面试之闭包
  • 前端之React实战:创建跨平台的项目架构
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 微信小程序设置上一页数据
  • 用element的upload组件实现多图片上传和压缩
  • 再次简单明了总结flex布局,一看就懂...
  • linux 淘宝开源监控工具tsar
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • #《AI中文版》V3 第 1 章 概述
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (十五)使用Nexus创建Maven私服
  • (转)关于pipe()的详细解析
  • ****Linux下Mysql的安装和配置
  • *Django中的Ajax 纯js的书写样式1
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .CSS-hover 的解释
  • .gitignore
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .Net CF下精确的计时器
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .Net的C#语言取月份数值对应的MonthName值
  • .NET设计模式(11):组合模式(Composite Pattern)