目录
建一个普通实体类来表示一条车辆数据,车辆数据要按天存储到HDFS上,数据格式是JSON格式
创建一个方法,随机生成一辆车一次行程的车辆数据,并存到list
一、项目结构
-
建一个普通实体类来表示一条车辆数据,车辆数据要按天存储到HDFS上,数据格式是JSON格式
-
创建一个方法,随机生成一辆车一次行程的车辆数据,并存到list
-
创建一个类,负责将数据写入HDFS
-
循环生成多辆数据数据,并循环写入到HDFS
二、代码
1、数据实体类代码片段
public class CanData implements Serializable {
/**
* 车架号
*/
private String vin;
/**
* 车型
*/
private String vehType;
/**
* 采集时间
*/
private String collectTime;
/**
* 车速
*/
private Integer speed;
/**
* 行驶里程
*/
private Integer mileage;
/**
* 故障碍列表
*/
private List<Integer> errCoedList;
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public String getVehType() {
return vehType;
}
public void setVehType(String vehType) {
this.vehType = vehType;
}
public String getCollectTime() {
return collectTime;
}
public void setCollectTime(String collectTime) {
this.collectTime = collectTime;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
public Integer getMileage() {
return mileage;
}
public void setMileage(Integer mileage) {
this.mileage = mileage;
}
public List<Integer> getErrCoedList() {
return errCoedList;
}
public void setErrCoedList(List<Integer> errCoedList) {
this.errCoedList = errCoedList;
}
}
2、随机生成一辆车一次行程的车辆数据代码
public static void main(String[] args) throws ParseException, IOException {
// 指定要生成哪天的数据
//hdfs://192.168.53.100:9000/ 2023-09-18
String dataDate = args[1];
String defaultFS = args[0];
//hdfs://192.168.53.100:9000/can_data/2023-09-18
String canDataDir = "/can_data/" + dataDate;
String initCollectTime = dataDate + " 00:00:00";
WriteHdfsHandler writeHdfsHandler = new WriteHdfsHandler(canDataDir, defaultFS);
// 循环生成N辆车辆数据
for(int i = 0; i < 100; i++) {
System.out.println("生成第" + i + "辆车的一次行程数据...");
// 生成一辆车一次形成数据
List<CanData> canDataList = canDataListOfOneCarOneTrip( initCollectTime, 60+new Random().nextInt(20*60));
// 将数据写入到HDFS
writeHdfsHandler.writeCanDataToHdfs(canDataList);
}
writeHdfsHandler.close();
}
/**
* 根据指定的行驶时长,随机生成一辆车在本次行程中产生的车辆动态数据
* @param driveMinutes 行驶时长,单位是分钟。根据行驶时长来随机生成车辆数据
* @return
*/
public static List<CanData> canDataListOfOneCarOneTrip(String initCollectTime, Integer driveMinutes) throws ParseException {
List<CanData> canDataList = new ArrayList<>();
// 根据行驶时长计算要生成的数据条数
int totalNums = driveMinutes*60/2;
// 生成车架号、车型
String curVin = randomVin();
String curVehType = randomVehType();
// 初始化一个采集时间
String preCollectTime = initCollectTime;
// 循环生成车辆数据
for(int i = 0; i < totalNums; i++) {
CanData canData = new CanData();
// 获取采集时间
preCollectTime = getNewByPreCollectTime(preCollectTime);
// 获取车速
Integer speed = randomSpeed();
// 获取行驶里程
Integer mileage = randomMileage();
// 获取故障码列表
List<Integer> errCodeList = randomErrCode();
// 更新canData
canData.setVin(curVin);
canData.setVehType(curVehType);
canData.setCollectTime(preCollectTime);
canData.setSpeed(speed);
canData.setMileage(mileage);
canData.setErrCoedList(errCodeList);
canDataList.add(canData);
}
return canDataList;
}
/**
* 随机生成车架号
* @return
*/
public static String randomVin() {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < 32; i++) {
int index = random.nextInt(characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
}
/**
* 随机生成车架号
* @return
*/
public static String randomVehType() {
String[] vehTypes = new String[] {"E100", "E200", "E300", "E400", "E500", "E600"};
Random random = new Random();
int index = random.nextInt(vehTypes.length);
return vehTypes[index];
}
/**
* 根据前一次采集时间获取新的采集时间
* @return
*/
public static String getNewByPreCollectTime(String preCollectTime) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(preCollectTime);
long timestamp = date.getTime();
// 模拟间隔2秒采集一次
Date newDate = new Date(timestamp + 2*1000);
return format.format(newDate);
}
/**
* 随机生成车速
* @return
*/
public static Integer randomSpeed() {
return new Random().nextInt(100);
}
/**
* 随机生成行驶里程
* @return
*/
public static Integer randomMileage() {
return new Random().nextInt(50000);
}
/**
* 随机生成故障码列表
* @return
*/
public static List<Integer> randomErrCode() {
List<Integer> res = new ArrayList<>();
Integer[] errCodes = new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
Random random = new Random();
int ifGenErr = random.nextInt(100);
// 如果ifGenErr随机数是6的倍数,则产生故障码
if(ifGenErr%3 == 0) {
// 决定生成多少个故障码
int numErrCode = random.nextInt(6);
for(int i = 0; i < numErrCode; i++) {
int codeIndex = random.nextInt(errCodes.length);
if(!res.contains(errCodes[codeIndex])) {
res.add(errCodes[codeIndex]);
}
}
}
reture res
}
}
3数据写入HDFS代码
/**
* 父路径,即数据文件所存放的路径
*/
private String canDataDir;
/**
* 文件名
*/
private final String FILE_NAME = "can_data.json.";
/**
* 文件名后缀
*/
private int curFileNameSuffix = 1;
/**
* 写了多少行数据到当前文件
*/
private int curLineNum = 0;
/**
* 每个文件最大行数,超过,则写入下一个文件
*/
private final int MAX_LINE_NUMS = 1000000;
private FileSystem fs;
/**
* 构造函数
* @param canDataDir
*/
public WriteHdfsHandler(String canDataDir, String defaultFS) throws IOException {
this.canDataDir = canDataDir;
// 获取HDFS文件系统对象
Configuration conf = new Configuration();
conf.set("fs.defaultFS", defaultFS);
fs = FileSystem.get(conf);
// 父目录删除
boolean exists = fs.exists(new Path(this.canDataDir));
if (exists) {
fs.delete(new Path(this.canDataDir), true);
}
fs.mkdirs(new Path(this.canDataDir));
}
/**
* 获取当前can data文件路径
* @return
*/
public String getCurCanDataFilePath() {
return this.canDataDir + "/" + FILE_NAME + this.curFileNameSuffix;
}
/**
* 将车辆数据写入到HDFS
* @param canDataList
*/
public void writeCanDataToHdfs(List<CanData> canDataList) throws IOException {
if(canDataList.size() == 0) {
return ;
}
int curHandedIndex = 0;
// 循环写入新的文件
while(curHandedIndex < canDataList.size()) {
if (curLineNum >= MAX_LINE_NUMS) {
this.curFileNameSuffix += 1;
curLineNum = 0;
}
// 创建新的文件
String curCanDataFilePath = getCurCanDataFilePath();
FSDataOutputStream out = null;
if(fs.exists(new Path(curCanDataFilePath))) {
out = fs.append(new Path(curCanDataFilePath));
} else {
out = fs.create(new Path(curCanDataFilePath));
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
Gson gson = new Gson();
// 循环将数据写入HDFS
for(; curHandedIndex < canDataList.size() && curLineNum < MAX_LINE_NUMS; curHandedIndex++) {
CanData canData = canDataList.get(curHandedIndex);
writer.write(gson.toJson(canData)+"\r\n");
curLineNum ++;
}
writer.close();
}
// 特殊处理:避免追加操作。伪分布式下,append会报错。
if(this.curLineNum > 0 && this.curLineNum < MAX_LINE_NUMS) {
this.curFileNameSuffix ++;
curLineNum = 0;
}
}
public void close() throws IOException {
fs.close();
}
}