繁星3.0.sql
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
-- s_task 任务信息表
CREATE TABLE IF NOT EXISTS s_task(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
name VARCHAR(128) NOT NULL COMMENT '任务名称',
alg_type VARCHAR(36) NOT NULL COMMENT '算法类型',
resource_need FLOAT NOT NULL COMMENT '任务所需资源数量',
priority INT NOT NULL DEFAULT 0 COMMENT '任务优先级',
store_config_id BIGINT NOT NULL COMMENT '存储配置id',
va_type VARCHAR(36) COMMENT '算法分析设备类型',
runtime_type INT NOT NULL DEFAULT 0 COMMENT '任务执行时间类型,全天分时段等',
runtime_conf TEXT COMMENT '任务执行时间段的配置',
device_unid VARCHAR(36) COMMENT '相机设备unid',
channel_unid VARCHAR(36) COMMENT '相机通道unid',
stream_path VARCHAR(1024) COMMENT '视频流地址',
stream_type INT COMMENT '视频流类型',
scene TEXT COMMENT '场景配置,包含画线预置位等',
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
status INT NOT NULL DEFAULT 0 COMMENT '任务状态',
INDEX s_task_name(name)
);
-- s_store_config 存储配置表
CREATE TABLE IF NOT EXISTS s_store_config(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
name VARCHAR(256) NOT NULL COMMENT '存储配置名称',
content_format INT NOT NULL COMMENT '配置内容格式,如xml',
content TEXT NOT NULL COMMENT '配置内容',
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
status INT NOT NULL DEFAULT 0 COMMENT '状态'
);
-- s_forward 转发信息表
CREATE TABLE IF NOT EXISTS s_forward(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
name VARCHAR(256) NOT NULL COMMENT '转发名称',
url VARCHAR(1024) NOT NULL COMMENT '转发地址',
total BIGINT NOT NULL DEFAULT 0 COMMENT '转发数据总量',
failed BIGINT NOT NULL DEFAULT 0 COMMENT '失败数量',
last_send_time TIMESTAMP COMMENT '最后发送时间',
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
status INT NOT NULL DEFAULT 0 COMMENT '状态'
);
-- s_forward_config 转发配置表
CREATE TABLE IF NOT EXISTS s_forward_config(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
forward_id BIGINT NOT NULL COMMENT '转发信息id',
event_cate VARCHAR(36) NOT NULL,
event_type VARCHAR(36) NOT NULL
);
-- d_traffic
CREATE TABLE IF NOT EXISTS d_traffic(
id BIGINT NOT NULL AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
task_id BIGINT COMMENT '任务id',
event_cate VARCHAR(36),
event_type VARCHAR(36),
event_time TIMESTAMP NOT NULL COMMENT '事件时间',
channel_unid VARCHAR(36) COMMENT '通道unid',
plate_color VARCHAR(36) COMMENT '车牌颜色',
plate_number VARCHAR(36) COMMENT '车牌号码',
location_code VARCHAR(36) COMMENT '位置编码',
location_name VARCHAR(500) COMMENT '位置名称',
lane_code VARCHAR(36) COMMENT '车道编码',
direction_code VARCHAR(36) COMMENT '卡口方向',
vehicle_type VARCHAR(36) COMMENT '车辆类型',
vehicle_color VARCHAR(36) COMMENT '车身颜色',
vehicle_logo VARCHAR(36) COMMENT '车标编码',
illegal_code VARCHAR(36) COMMENT '违法行为',
illegal_state INT COMMENT '违法行为是否可用',
feature_annual_inspection_mark SMALLINT DEFAULT 0 COMMENT '年检标',
feature_pendant SMALLINT DEFAULT 0 COMMENT '吊坠',
feature_decoration SMALLINT DEFAULT 0 COMMENT '摆件',
feature_sun_shield SMALLINT DEFAULT 0 COMMENT '遮阳板',
xcycle_type VARCHAR(36) COMMENT '非机动车类型',
event_id VARCHAR(64) COMMENT '事件唯一id' ,
special_type VARCHAR(36),
with_helmet INT COMMENT '是否戴头盔0不带',
json_data TEXT COMMENT '原始数据',
pics VARCHAR(2048),
video_name VARCHAR(256),
status INT NOT NULL DEFAULT 0 COMMENT '状态',
PRIMARY KEY(id,event_time)
);
-- d_traffic_face
CREATE TABLE IF NOT EXISTS d_traffic_face(
id BIGINT NOT NULL AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
traffic_id BIGINT NOT NULL COMMENT 'trafficId',
event_time TIMESTAMP NOT NULL COMMENT '事件时间',
state SMALLINT NOT NULL DEFAULT 1,
sex SMALLINT NOT NULL DEFAULT 0,
upper_color VARCHAR(36),
lower_color VARCHAR(36),
INDEX s_task_name(traffic_id),
PRIMARY KEY(id,event_time)
);
-- d_flow_data
CREATE TABLE IF NOT EXISTS d_flow_data(
id BIGINT NOT NULL AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
flow_event_id BIGINT COMMENT 'eventId',
task_id BIGINT,
detection_type VARCHAR(36) COMMENT '检测类型(bus,big_vehicle,xcycle等)',
road_code VARCHAR(36) COMMENT '道路编码',
direction_code VARCHAR(36) COMMENT '方向编码',
sample_dura BIGINT,
sample_num FLOAT,
velocity FLOAT COMMENT '速率',
velocity_unit VARCHAR(16) COMMENT '速率单位',
occupy FLOAT COMMENT '车道占有率',
distance FLOAT COMMENT '车头间距',
queue_length FLOAT COMMENT '车辆排队长度',
region_id VARCHAR(36),
region_name VARCHAR(128),
position_content TEXT,
head_content TEXT,
area FLOAT,
density FLOAT,
sample_num_in FLOAT,
sample_num_out FLOAT,
event_time TIMESTAMP,
dist_time FLOAT,
time_occupy FLOAT,
status INT NOT NULL DEFAULT 0 COMMENT '状态',
PRIMARY KEY(id,event_time)
);
-- d_flow_event
CREATE TABLE IF NOT EXISTS d_flow_event(
id BIGINT NOT NULL AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
task_id BIGINT COMMENT '任务id',
event_type VARCHAR(36),
event_time TIMESTAMP COMMENT '事件时间',
event_id VARCHAR(64) COMMENT '事件唯一id',
device_code VARCHAR(36),
device_name VARCHAR(64),
location_code VARCHAR(36),
location_name VARCHAR(64),
json_data TEXT,
pics VARCHAR(2048),
video_name VARCHAR(256),
PRIMARY KEY(id,event_time)
);
-- d_behavior
CREATE TABLE IF NOT EXISTS d_behavior(
id BIGINT NOT NULL AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
task_id BIGINT,
event_type VARCHAR(36),
event_cate VARCHAR(36),
event_refid VARCHAR(64),
event_time TIMESTAMP,
event_data TEXT,
pics VARCHAR(2048),
video TEXT,
channel_unid VARCHAR(36),
task_name VARCHAR(128),
event_data_num VARCHAR(36),
PRIMARY KEY(id,event_time)
);
-- s_dict_cate
CREATE TABLE IF NOT EXISTS s_dict_cate(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
type VARCHAR(36) COMMENT '分类'
code VARCHAR(36) COMMENT 'cate编码',
name VARCHAR(36) NOT NULL COMMENT 'cate名称',
note VARCHAR(36) COMMENT '注释'
);
-- s_dict_code
CREATE TABLE IF NOT EXISTS s_dict_code(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
cate_id BIGINT NOT NULL,
parent_id BIGINT COMMENT '上级code的id',
code VARCHAR(36) COMMENT 'code编码',
name VARCHAR(36) NOT NULL COMMENT 'code名称',
note VARCHAR(36) COMMENT '注释',
INDEX s_dict_code(cate_id)
);
-- s_channel
CREATE TABLE IF NOT EXISTS s_channel(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
unid VARCHAR(36) NOT NULL DEFAULT (UUID()),
channel_unid VARCHAR(64) NOT NULL,
device_unid VARCHAR(64) NOT NULL,
type INT NOT NULL default 0,
stream_type INT,
stream_path VARCHAR(1024),
username VARCHAR(36),
password VARCHAR(64),
address_unid VARCHAR(36),
name VARCHAR(128),
ip VARCHAR(36),
port INT,
expand VARCHAR(128),
longitude FLOAT,
latitude FLOAT,
status INT NOT NULL DEFAULT 0,
INDEX s_channel(channel_unid)
);
-- 分表
alter TABLE d_traffic PARTITION by RANGE(UNIX_TIMESTAMP(event_time))(
PARTITION d_traffic20210701 VALUES less than (UNIX_TIMESTAMP('2021-07-02'))
);
alter TABLE d_traffic_face PARTITION by RANGE(UNIX_TIMESTAMP(event_time))(
PARTITION d_traffic_face20210701 VALUES less than (UNIX_TIMESTAMP('2021-07-02'))
);
alter TABLE d_flow_event PARTITION by RANGE(UNIX_TIMESTAMP(event_time))(
PARTITION d_flow_event20210701 VALUES less than (UNIX_TIMESTAMP('2021-07-02'))
);
alter TABLE d_flow_data PARTITION by RANGE(UNIX_TIMESTAMP(event_time))(
PARTITION d_flow_data20210701 VALUES less than (UNIX_TIMESTAMP('2021-07-02'))
);
alter TABLE d_behavior PARTITION by RANGE(UNIX_TIMESTAMP(event_time))(
PARTITION d_behavior20210701 VALUES less than (UNIX_TIMESTAMP('2021-07-02'))
);
delimiter $$
DROP PROCEDURE IF EXISTS timestamp_partition
$$
CREATE PROCEDURE timestamp_partition(v_table_name VARCHAR(128),v_schema VARCHAR(128))
BEGIN
DECLARE v_sysdate date; # 声明 当前时间
DECLARE v_mindate date; # 声明 目前分区的最小值
DECLARE v_maxdate date; # 声明 目前分区值中的最大值
DECLARE v_pt varchar(20); # 声明 分区名称 数字部分
DECLARE v_maxval varchar(20); # 声明 最大值
DECLARE i int; # 声明 ??
-- 增加新分区
SELECT max(CAST(FROM_UNIXTIME(partition_description) as date)) AS val
INTO v_maxdate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = v_table_name AND TABLE_SCHEMA = v_schema;
set v_sysdate = sysdate(); # 赋值v_sysdate为当前时间
-- INTERVAL 时间计算的关键字
WHILE v_maxdate <= (v_sysdate + INTERVAL 7 DAY) DO
SET v_pt = date_format(v_maxdate ,'%Y%m%d');
SET v_maxval = date_format(v_maxdate + INTERVAL 1 DAY, '%Y-%m-%d');
SET @sql = concat('alter table ',v_table_name,' add partition (partition ',v_table_name, v_pt, ' values less than(UNIX_TIMESTAMP(''', v_maxval , ''')))');
PREPARE stmt FROM @sql;
EXECUTE stmt;
-- 删除预处理
DEALLOCATE PREPARE stmt;
SET v_maxdate = v_maxdate + INTERVAL 1 DAY; # 最大值 加一操作
END WHILE;
-- 删除旧分区
SELECT min(CAST(FROM_UNIXTIME(partition_description) as date)) AS val
INTO v_mindate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = v_table_name AND TABLE_SCHEMA = v_schema;
-- 删除7天前的旧分区
WHILE v_mindate <= (v_sysdate - INTERVAL 7 DAY) DO
SET v_pt = date_format(v_mindate - INTERVAL 1 DAY,'%Y%m%d');
SET @sql = concat('alter table ',v_table_name,' drop partition ',v_table_name, v_pt);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_mindate = v_mindate + INTERVAL 1 DAY;
END WHILE;
END$$
delimiter ;
-- 创建定时任务
delimiter $$
drop event if exists auto_pt $$
create event auto_pt
on schedule
every 1 day
do
BEGIN
call timestamp_partition('d_traffic','{schema}');
call timestamp_partition('d_behavior','{schema}');
call timestamp_partition('d_flow_event','{schema}');
call timestamp_partition('d_flow_data','{schema}');
call timestamp_partition('d_traffic_face','{schema}');
END$$
delimiter ;