2.模拟数据
日期模拟
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
public class DataUtil {
public static void main(String[] args) {
String s = getTodayDate();
System.out.println(s);
}
// 获取今天的日期 2022-09-13
public static String getTodayDate() {
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
String format = dfs.format(date);
return format;
}
// 获取今天的随机时间 2022-09-13 8:00:00
public static String getTodayTime() {
Random random = new Random();
String date = getTodayDate();
String hour = random.nextInt(24) + "";
if (Integer.parseInt(hour) <= 9) {
hour = "0" + hour;
}
String min = random.nextInt(60) + "";
if (Integer.parseInt(min) <= 9) {
min = "0" + min;
}
String seconds = random.nextInt(60) + "";
if (Integer.parseInt(seconds) <= 9) {
seconds = "0" + seconds;
}
String time = date + " " + hour + ":" + min + ":" + seconds;
return time;
}
}
车流量数据模拟
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CarFlow {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(
new FileWriter("G:/monitor_car_flow.csv", true));
// 1. 获取今天的日期
String todayDate = DataUtil.getTodayDate();
String[] chePaiPresix =
{"晋A", "晋A", "晋A", "晋A", "晋A", "晋A",
"晋A", "晋A", "晋A", "晋A", "晋A", "晋A",
"晋B", "晋C", "晋D", "晋E", "晋K", "晋K",
"晋K", "晋K", "晋K", "晋K", "晋K", "晋K",
"晋K", "晋K", "晋K", "晋K", "晋M", "晋L"};
char[] array =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'};
Random random = new Random();
// 模拟多少条数据 100000
List<String> list = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
// 7. roadId
String roadId = 1 + random.nextInt(100) + "";
// 8. areaId
String areaId = 1 + random.nextInt(6) + "";
// 2. 随机生成卡口id 0~100
String monitorId = areaId + "-" + roadId + "-" + random.nextInt(101);
// 3. 摄像头id 2~8个 编号从1开始
int camera_id = 1+random.nextInt(8);
int num = 1 + random.nextInt(1000);
for (int j = 0; j < num; j++) {
// 4. 车牌号 模拟山西的车牌号 晋A/B/C/D/H/E/K/M/L 5位
String che = chePaiPresix[random.nextInt(chePaiPresix.length)];
String chepai = che + array[random.nextInt(array.length)] +
array[random.nextInt(array.length)]
+ array[random.nextInt(array.length)]
+ array[random.nextInt(array.length)]
+ array[random.nextInt(array.length)];
// 5. 年月日 时分秒
String actionTime = DataUtil.getTodayTime();
// 6. 车速
String speed = random.nextInt(120) + "";
// 车辆通过时间
// 卡口编号
// 摄像头编号
// 通过车辆车牌
// 某个摄像头拍摄时间 单位:秒
// 通过卡口的速度
// 道路id
// 区域id
String data = todayDate + "," + monitorId + ","
+ camera_id + "," + chepai + "," + actionTime + ","
+ speed + "," + roadId + "," + areaId;
list.add(data);
}
}
// 将每个卡口的摄像头数据打散
Collections.shuffle(list);
for (String data: list) {
bw.write(data);
bw.newLine();
bw.flush();
}
System.out.println("卡口车流量数据模拟完成");
}
}
摄像头模拟
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* 摄像头数据模拟
* 卡扣编号 摄像头编号
* monitor_id camera_id
*
* 卡口摄像头不一定在车流量数据文件中存在,因为有些道路可能每天就没有车辆通过
* 或者卡口摄像头损坏了
*/
public class CameraData {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("G:/cameras.csv"));
/**
* 卡口id 区域id+街道id+卡口编号
* 摄像头 1-8
*/
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 100; j++) {
for (int k = 1; k <=100 ; k++) {
for (int l = 1; l <= 8 ; l++) {
String monitor_id = i +"-"+ j +"-"+k;
String cameraId = l+"";
bw.write(monitor_id+","+cameraId);
bw.newLine();
bw.flush();
}
}
}
}
}
}
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/16695606.html