Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
ZHHT-IRN-BD-ANALYSIS
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李凯旋
ZHHT-IRN-BD-ANALYSIS
Commits
6bb3ebdf
Commit
6bb3ebdf
authored
Nov 08, 2022
by
吴延飞
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善实时计算指标旅行信息相关的5个指标的计算逻辑代码
parent
ecaed0d6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
100 deletions
+72
-100
Travel.java
...m-streaming/src/main/java/com/zhht/irn/entity/Travel.java
+9
-5
TravelInfoJob.java
...reaming/src/main/java/com/zhht/irn/job/TravelInfoJob.java
+58
-94
TravelMetricSink.java
...ing/src/main/java/com/zhht/irn/sink/TravelMetricSink.java
+5
-1
No files found.
realtime/hologram-streaming/src/main/java/com/zhht/irn/entity/Travel.java
View file @
6bb3ebdf
...
@@ -106,12 +106,16 @@ public class Travel {
...
@@ -106,12 +106,16 @@ public class Travel {
//实际停车次数
//实际停车次数
private
int
stopCount
;
private
int
stopCount
;
//停车总时长
//停车总时长
private
double
stopTime
;
private
double
stopTime
;
//平均车速
//平均车速
private
double
aveSpeed
;
private
double
avgSpeed
;
//最低速度
//最低速度
private
double
minSpeed
;
private
double
minSpeed
;
//最高速度
//最高速度
private
double
maxSpeed
;
private
double
maxSpeed
;
...
@@ -371,12 +375,12 @@ public class Travel {
...
@@ -371,12 +375,12 @@ public class Travel {
this
.
stopTime
=
stopTime
;
this
.
stopTime
=
stopTime
;
}
}
public
double
getAv
e
Speed
()
{
public
double
getAv
g
Speed
()
{
return
av
e
Speed
;
return
av
g
Speed
;
}
}
public
void
setAv
eSpeed
(
double
ave
Speed
)
{
public
void
setAv
gSpeed
(
double
avg
Speed
)
{
this
.
av
eSpeed
=
ave
Speed
;
this
.
av
gSpeed
=
avg
Speed
;
}
}
public
double
getMinSpeed
()
{
public
double
getMinSpeed
()
{
...
...
realtime/hologram-streaming/src/main/java/com/zhht/irn/job/TravelInfoJob.java
View file @
6bb3ebdf
...
@@ -3,24 +3,15 @@ package com.zhht.irn.job;
...
@@ -3,24 +3,15 @@ package com.zhht.irn.job;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.zhht.irn.entity.Location
;
import
com.zhht.irn.entity.Location
;
import
com.zhht.irn.entity.Travel
;
import
com.zhht.irn.entity.Travel
;
import
com.zhht.irn.entity.mysql.TripsMetric
;
import
com.zhht.irn.sink.TravelMetricSink
;
import
com.zhht.irn.sink.TravelMetricSink
;
import
org.apache.flink.api.common.functions.FilterFunction
;
import
com.zhht.irn.utils.FlinkUtils
;
import
org.apache.flink.api.common.functions.MapFunction
;
import
org.apache.flink.api.common.functions.MapFunction
;
import
org.apache.flink.api.common.restartstrategy.RestartStrategies
;
import
org.apache.flink.api.common.serialization.SimpleStringSchema
;
import
org.apache.flink.api.common.time.Time
;
import
org.apache.flink.api.java.utils.ParameterTool
;
import
org.apache.flink.runtime.state.filesystem.FsStateBackend
;
import
org.apache.flink.streaming.api.datastream.DataStream
;
import
org.apache.flink.streaming.api.datastream.DataStream
;
import
org.apache.flink.streaming.api.environment.CheckpointConfig
;
import
org.apache.flink.streaming.api.environment.StreamExecutionEnvironment
;
import
org.apache.flink.streaming.api.environment.StreamExecutionEnvironment
;
import
org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
/**
/**
* 旅行信心实时任务
* 旅行信心实时任务
...
@@ -34,92 +25,65 @@ public class TravelInfoJob {
...
@@ -34,92 +25,65 @@ public class TravelInfoJob {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// 获取流式运行环境
// 获取流式运行环境
StreamExecutionEnvironment
env
=
StreamExecutionEnvironment
.
getExecutionEnvironment
();
StreamExecutionEnvironment
env
=
StreamExecutionEnvironment
.
getExecutionEnvironment
();
// 获取Kafka配置文件
ParameterTool
finkArgs
=
ParameterTool
.
fromArgs
(
args
);
// 通过kafka配置文件,获取String类型的实时数据流
String
kafkaConfigPath
=
finkArgs
.
getRequired
(
"kafka.conf"
);
DataStream
<
String
>
rawStringStream
=
FlinkUtils
.
createKafkaStream
(
args
,
env
);
ParameterTool
kafkaTool
;
try
{
kafkaTool
=
ParameterTool
.
fromPropertiesFile
(
kafkaConfigPath
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"kafka 配置文件未设置,请检查!"
);
throw
new
RuntimeException
(
e
);
}
// 加载Kafka信息 { topic 相关 }
String
groupId
=
kafkaTool
.
get
(
"group.id"
,
"test1"
);
String
servers
=
kafkaTool
.
getRequired
(
"bootstrap.servers"
);
List
<
String
>
topics
=
Arrays
.
asList
(
kafkaTool
.
getRequired
(
"kafka.input.topics"
).
split
(
","
));
String
autoCommit
=
kafkaTool
.
get
(
"enable.auto.commit"
,
"false"
);
String
offsetReset
=
kafkaTool
.
get
(
"auto.offset.reset"
,
"earliest"
);
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"bootstrap.servers"
,
servers
);
properties
.
setProperty
(
"group.id"
,
groupId
);
properties
.
setProperty
(
"enable.auto.commit"
,
autoCommit
);
properties
.
setProperty
(
"auto.offset.reset"
,
offsetReset
);
int
checkpointInterval
=
kafkaTool
.
getInt
(
"checkpoint.interval"
,
5000
);
String
checkpointPath
=
kafkaTool
.
get
(
"checkpoint.path"
,
"file:///f/workspace/imooc-flink/state"
);
env
.
enableCheckpointing
(
checkpointInterval
);
env
.
setStateBackend
(
new
FsStateBackend
(
checkpointPath
));
env
.
getCheckpointConfig
().
enableExternalizedCheckpoints
(
CheckpointConfig
.
ExternalizedCheckpointCleanup
.
RETAIN_ON_CANCELLATION
);
env
.
setRestartStrategy
(
RestartStrategies
.
fixedDelayRestart
(
2
,
Time
.
of
(
5
,
TimeUnit
.
SECONDS
)));
// 基于Kafka 原始JSON格式的字符串,获取消息流
FlinkKafkaConsumer
<
String
>
kafkaConsumer
=
new
FlinkKafkaConsumer
<>(
topics
,
new
SimpleStringSchema
(),
properties
);
DataStream
<
String
>
rawStringStream
=
env
.
addSource
(
kafkaConsumer
);
// 开始计算旅行信息相关指标
// 开始计算旅行信息相关指标
rawStringStream
.
map
(
new
MapFunction
<
String
,
Travel
>()
{
rawStringStream
.
map
((
MapFunction
<
String
,
Travel
>)
s
->
{
@Override
// json 格式字符串转化为 Travel 实体类 @todo 不一定能正确解析
public
Travel
map
(
String
s
)
throws
Exception
{
Travel
travel
=
JSON
.
parseObject
(
s
,
Travel
.
class
);
// json 格式字符串转化为 TravelInfo 实体类
Travel
travel
=
JSON
.
parseObject
(
s
,
Travel
.
class
);
List
<
Location
>
locations
=
travel
.
getLocations
();
String
crossId
=
travel
.
getCrossId
();
String
recordId
=
travel
.
getRecordId
();
int
stopCount
=
0
;
long
stopTime
=
0L
;
long
carId
=
travel
.
getCarId
();
double
minSpeed
=
0.0
,
maxSpeed
=
0.0
,
totalSpeed
=
0.0
;
List
<
Location
>
locations
=
travel
.
getLocations
();
//两个location之间的间隔时长0.1s
boolean
stopped
=
false
;
// 停车状态,判断截止到处理时的停车状态 false 行驶状态(默认值),true 停车状态
double
intervalTime
=
0.1
;
//停车次数
for
(
int
i
=
0
;
i
<
locations
.
size
();
i
++)
{
int
stopCount
=
0
;
double
currentSpeed
=
locations
.
get
(
i
).
getSpeed
();
//停车总时长
double
stopTime
=
0
;
// 获取最大最小值
//速度总和
if
(
currentSpeed
>
maxSpeed
)
maxSpeed
=
currentSpeed
;
double
totalSpeed
=
0
;
if
(
currentSpeed
<
minSpeed
)
minSpeed
=
currentSpeed
;
//速度数组
List
<
Double
>
speedList
=
new
ArrayList
<
Double
>();
// 累计速度
//上一个location的速度,初始化取第一条数据
totalSpeed
+=
currentSpeed
;
double
lastSpeed
=
locations
.
get
(
0
).
getSpeed
();
for
(
int
i
=
0
;
i
<
locations
.
size
();
i
++){
// 实际停车为整个旅行中车辆速度低于某个下限阈值的(暂定3km/h)次数. 当车辆一直低于该下限阈值(10km/h)时, 视为一次连续的实际停车.
Location
location
=
locations
.
get
(
i
);
// 当车辆高于某个上限阈值(暂定10km/h)后, 再次低于下限阈值(暂定3km/h)时, 再记为一次实际停车
Double
speed
=
location
.
getSpeed
();
speedList
.
add
(
speed
);
// 每低于3 => 视为停车状态
if
(
speed
<=
3
){
// 每高于10 => 视为行驶状态
if
(
lastSpeed
>
3
)
{
//
stopCount
=
stopCount
+
1
;
// 停车次数 = 行驶状态 切换至 停车状态 的次数
}
// 状态 stopped 从行驶状态切换停车状态一次, 记一次实际停车数 @宇昂提供的计算思路
stopTime
=
stopTime
+
intervalTime
;
if
(
stopped
&&
currentSpeed
>=
10
)
{
}
// 停车状态下,只关注速度上限, 10km/h, 累计停车时长
totalSpeed
=
totalSpeed
+
speed
;
stopped
=
false
;
lastSpeed
=
speed
;
stopTime
+=
(
locations
.
get
(
i
).
getDtTranjectory
().
getTime
()
-
locations
.
get
(
i
-
1
).
getDtTranjectory
().
getTime
());
}
else
if
(!
stopped
&&
currentSpeed
<
3
)
{
// 行驶状态下,只关注速度下限, 3km/h, 增加停车次数
stopped
=
true
;
stopCount
++;
}
}
//最大速度
double
maxSpeed
=
Collections
.
max
(
speedList
);
//最小速度,初始化0
double
minSpeed
=
Collections
.
min
(
speedList
);
//平均速度
double
aveSpeed
=
totalSpeed
/
locations
.
size
();
travel
.
setStopCount
(
stopCount
);
travel
.
setStopTime
(
stopTime
);
travel
.
setAveSpeed
(
aveSpeed
);
travel
.
setMaxSpeed
(
maxSpeed
);
travel
.
setMinSpeed
(
minSpeed
);
return
travel
;
}
}
}).
addSink
(
new
TravelMetricSink
());
//@todo 插入数据库的逻辑
// 获取5个实时旅行信息指标
travel
.
setStopCount
(
stopCount
);
travel
.
setStopTime
(
stopTime
);
travel
.
setAvgSpeed
(
totalSpeed
/
locations
.
size
());
travel
.
setMaxSpeed
(
maxSpeed
);
travel
.
setMinSpeed
(
minSpeed
);
logger
.
info
(
"完成旅行信息的指标计算,当前旅行车辆旅行ID{}的实时指标为, 最高速度:{}, 最低速度:{}, 平均速度:{}, 停车次数: {},"
+
"停车总时长:{}"
,
travel
.
getRecordId
(),
maxSpeed
,
minSpeed
,
travel
.
getAvgSpeed
(),
stopCount
,
stopTime
);
return
travel
;
}).
addSink
(
new
TravelMetricSink
());
}
}
}
}
realtime/hologram-streaming/src/main/java/com/zhht/irn/sink/TravelMetricSink.java
View file @
6bb3ebdf
...
@@ -21,7 +21,11 @@ public class TravelMetricSink extends RichSinkFunction<Travel> {
...
@@ -21,7 +21,11 @@ public class TravelMetricSink extends RichSinkFunction<Travel> {
// path暂定为固定值
// path暂定为固定值
String
path
=
"F:\\workbench\\ZHHT-IRN-BD-ANALYSIS\\realtime\\hologram-streaming\\src\\main\\resources\\mysql.properties"
;
String
path
=
"F:\\workbench\\ZHHT-IRN-BD-ANALYSIS\\realtime\\hologram-streaming\\src\\main\\resources\\mysql.properties"
;
connection
=
MySQLUtils
.
getConnection
(
path
);
connection
=
MySQLUtils
.
getConnection
(
path
);
String
insertSql
=
"insert into app_travel_metric(record_id,cross_id,car_id,plate,capture_time,lost_time,stop_count,stop_time,ave_speed,min_speed,max_speed,update_time) values (?,?,?,?,?,?,?,?,?,?,?,?)"
;
String
insertSql
=
""
+
"insert into app_travel_metric"
+
"(id, car_id, cross_id, plate, record_id, capture_time, lost_time, arrived_time, incross_time, "
+
"outcross_time, away_time, stop_count, stop_time, avg_speed, min_speed, max_speed, update_time)"
+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
;
insertPstmt
=
connection
.
prepareStatement
(
insertSql
);
insertPstmt
=
connection
.
prepareStatement
(
insertSql
);
}
}
@Override
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment