Commit f18a37d2 by 黄准

车道指标实时任务修改

parent c5b088a5
...@@ -5,6 +5,7 @@ import com.zhht.irn.entity.*; ...@@ -5,6 +5,7 @@ import com.zhht.irn.entity.*;
import com.zhht.irn.enums.EventType; import com.zhht.irn.enums.EventType;
import com.zhht.irn.utils.*; import com.zhht.irn.utils.*;
import org.apache.flink.api.common.state.*; import org.apache.flink.api.common.state.*;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration; import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction; import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
...@@ -42,6 +43,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -42,6 +43,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
private Connection connection; private Connection connection;
//每个路口允许的最大时间数据 //每个路口允许的最大时间数据
private static Long maxTravel=50000L; private static Long maxTravel=50000L;
//每天的cycleOrder 不允许重复
private MapState<String,String> dayCycle;
@Override @Override
public void open(Configuration parameters) throws Exception { public void open(Configuration parameters) throws Exception {
...@@ -51,6 +54,16 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -51,6 +54,16 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
dict = getRuntimeContext().getMapState(new MapStateDescriptor("dict", String.class, Object.class)); dict = getRuntimeContext().getMapState(new MapStateDescriptor("dict", String.class, Object.class));
processCycle = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("processCycle", Boolean.class)); processCycle = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("processCycle", Boolean.class));
connection=DorisUtils.getConnection(); connection=DorisUtils.getConnection();
StateTtlConfig ttlConfig = StateTtlConfig
.newBuilder(Time.hours(3))
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
.build();
MapStateDescriptor<String, String> mapStateDescriptor = new MapStateDescriptor("dayCycle", String.class, String.class);
//
mapStateDescriptor.enableTimeToLive(ttlConfig);
dayCycle = getRuntimeContext().getMapState(mapStateDescriptor);
} }
@Override @Override
...@@ -65,6 +78,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -65,6 +78,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
public void processElement1(Cycle cycle, KeyedCoProcessFunction<String, Cycle, TravelEvent, List<LaneNorm>>.Context ctx, Collector<List<LaneNorm>> out) throws Exception { public void processElement1(Cycle cycle, KeyedCoProcessFunction<String, Cycle, TravelEvent, List<LaneNorm>>.Context ctx, Collector<List<LaneNorm>> out) throws Exception {
processCycle.update(true); processCycle.update(true);
List<LaneNorm> laneNorms = new ArrayList<>(); List<LaneNorm> laneNorms = new ArrayList<>();
if(!dayCycle.contains(DateUtils.toDateStr(new Date())+"-"+cycle.getCrossCode()+"-"+cycle.getCycleOrder())){
dayCycle.put(DateUtils.toDateStr(new Date())+"-"+cycle.getCrossCode()+"-"+cycle.getCycleOrder(),"true");
}else{
//如果内存中已经存在重复的周期 则不进行计算
return;
}
//初始化字典数据 //初始化字典数据
if (dict.get("phaseDirection") == null || dict.get("laneList") == null || dict.get("dictResult") == null || dict.get("positionList") == null || dict.get("positionNameList") == null) { if (dict.get("phaseDirection") == null || dict.get("laneList") == null || dict.get("dictResult") == null || dict.get("positionList") == null || dict.get("positionNameList") == null) {
if(connection==null){ if(connection==null){
...@@ -131,7 +150,7 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -131,7 +150,7 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
LaneNorm laneNorm = getLaneNorm(map, f1, carForLane, laneList); LaneNorm laneNorm = getLaneNorm(map, f1, carForLane, laneList);
list.add(laneNorm); list.add(laneNorm);
//交通流量为0得不参与计算 //交通流量为0得不参与计算
if (laneNorm != null && laneNorm.getTrafficCapacity() != 0d) { if (laneNorm != null) {
laneNorms.add(laneNorm); laneNorms.add(laneNorm);
} }
} }
...@@ -268,8 +287,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -268,8 +287,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
laneNorm.setType("1"); laneNorm.setType("1");
laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime())); laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime()));
laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime())); laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime()));
//获取所有这个方向得车道 交通流量为0得不参与计算 //获取所有这个方向得车道
List<LaneNorm> collect = list.stream().filter(recode -> position.equals(recode.getDirection()) && recode.getLaneName().indexOf(name) != -1 && recode.getTrafficCapacity() != 0d).collect(Collectors.toList()); List<LaneNorm> collect = list.stream().filter(recode -> position.equals(recode.getDirection())&&recode.getLaneName().indexOf(name) != -1).collect(Collectors.toList());
if (collect.size() == 0) { if (collect.size() == 0) {
continue; continue;
} }
...@@ -282,8 +301,10 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -282,8 +301,10 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
//交通流量求和 //交通流量求和
laneNorm.setTrafficCapacity(collect.stream().mapToDouble(LaneNorm::getTrafficCapacity).sum()); laneNorm.setTrafficCapacity(collect.stream().mapToDouble(LaneNorm::getTrafficCapacity).sum());
//剩余承载力求和 //剩余承载力求和
double sum2 = collect.stream().mapToDouble(LaneNorm::getResidualCapacity).sum(); double sum2 = collect.stream().reduce(0.0,(x,y)->x+(y.getPassCapacity()-y.getTrafficCapacity()),Double::sum);
laneNorm.setResidualCapacity(new BigDecimal(sum2).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue()); //double sum2 = collect.stream().mapToDouble(LaneNorm::getResidualCapacity).sum();
laneNorm.setResidualCapacity(new BigDecimal(sum2<=0?0d:sum2).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue());
//laneNorm.setResidualCapacity(new BigDecimal(sum2).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue());
//有效绿灯时间 相位下此方向非右转的绿灯有效时间之和 //有效绿灯时间 相位下此方向非右转的绿灯有效时间之和
laneNorm.setEffectGreenTime(gettEffectGreenTimeForDirction(cycle.getStageList(), position,name)); laneNorm.setEffectGreenTime(gettEffectGreenTimeForDirction(cycle.getStageList(), position,name));
//识别空间空间占有率 车道长度需要交通流量为0得数据 //识别空间空间占有率 车道长度需要交通流量为0得数据
...@@ -320,8 +341,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -320,8 +341,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
laneNorm.setType("0"); laneNorm.setType("0");
laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime())); laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime()));
laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime())); laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime()));
//获取所有这个方向得车道 交通流量为0得不参与计算 //获取所有这个方向得车道
List<LaneNorm> collect = list.stream().filter(recode -> position.equals(recode.getDirection()) && recode.getTrafficCapacity() != 0d).collect(Collectors.toList()); List<LaneNorm> collect = list.stream().filter(recode -> position.equals(recode.getDirection())).collect(Collectors.toList());
if (collect.size() == 0) { if (collect.size() == 0) {
continue; continue;
} }
...@@ -333,8 +354,9 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -333,8 +354,9 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
//交通流量求和 //交通流量求和
laneNorm.setTrafficCapacity(collect.stream().mapToDouble(LaneNorm::getTrafficCapacity).sum()); laneNorm.setTrafficCapacity(collect.stream().mapToDouble(LaneNorm::getTrafficCapacity).sum());
//剩余承载力求和 //剩余承载力求和
double sum2 = collect.stream().mapToDouble(LaneNorm::getResidualCapacity).sum(); double sum2 = collect.stream().reduce(0.0,(x,y)->x+(y.getPassCapacity()-y.getTrafficCapacity()),Double::sum);
laneNorm.setResidualCapacity(new BigDecimal(sum2).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue()); //double sum2 = collect.stream().mapToDouble(LaneNorm::getResidualCapacity).sum();
laneNorm.setResidualCapacity(new BigDecimal(sum2<=0?0d:sum2).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue());
//有效绿灯时间 相位下此方向非右转得绿灯有效时间之和 //有效绿灯时间 相位下此方向非右转得绿灯有效时间之和
laneNorm.setEffectGreenTime(gettEffectGreenTimeForPosition(cycle.getStageList(), position)); laneNorm.setEffectGreenTime(gettEffectGreenTimeForPosition(cycle.getStageList(), position));
//识别空间空间占有率 车道长度需要交通流量为0得数据 //识别空间空间占有率 车道长度需要交通流量为0得数据
...@@ -350,6 +372,20 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -350,6 +372,20 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
} }
} }
public static void main(String[] args) {
List<LaneNorm> objects = new ArrayList<LaneNorm>();
LaneNorm laneNorm = new LaneNorm();
laneNorm.setTrafficCapacity(100d);
laneNorm.setPassCapacity(50d);
LaneNorm laneNorm1 = new LaneNorm();
laneNorm1.setTrafficCapacity(100d);
laneNorm1.setPassCapacity(130d);
objects.add(laneNorm);
objects.add(laneNorm1);
Double reduce = objects.stream().reduce(0.0, (x, y) -> x + (y.getPassCapacity() - y.getTrafficCapacity()), Double::sum);
System.out.println(reduce);
}
/** /**
* @param map 车道信息 * @param map 车道信息
* @param cycle 周期信息 * @param cycle 周期信息
...@@ -378,10 +414,11 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -378,10 +414,11 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
laneNorm.setType("2"); laneNorm.setType("2");
laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime())); laneNorm.setCycleStartTime(DateUtils.stampToTime(cycle.getBeginDateTime()));
laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime())); laneNorm.setCycleEndTime(DateUtils.stampToTime(cycle.getEndDateTime()));
//如果交通流量为0,则不统计此车道指标数据 /*//如果交通流量为0,则不统计此车道指标数据
//2.0版本 参与计算
if (trafficCapacity == 0d) { if (trafficCapacity == 0d) {
return laneNorm; return laneNorm;
} }*/
//车道饱和度 //车道饱和度
int saturation_flow_rate = getSaturation(map); int saturation_flow_rate = getSaturation(map);
//车道有效绿灯时间 //车道有效绿灯时间
...@@ -397,12 +434,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -397,12 +434,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
laneNorm.setMaxQueueLength(maxQueueLength); laneNorm.setMaxQueueLength(maxQueueLength);
laneNorm.setEffectGreenTime(effcetGreenTime[0]); laneNorm.setEffectGreenTime(effcetGreenTime[0]);
//通行能力 //通行能力
double v = saturation_flow_rate * laneEffectGreenTime; double v = saturation_flow_rate * laneEffectGreenTime>1d?1:laneEffectGreenTime;
BigDecimal bd2 = new BigDecimal(v); BigDecimal bd2 = new BigDecimal(v);
Double d3 = bd2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); Double d3 = bd2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
laneNorm.setPassCapacity(d3); laneNorm.setPassCapacity(d3);
//饱和流率 //饱和流率
Double saturation = (trafficCapacity / (saturation_flow_rate * laneEffectGreenTime)); Double saturation = (trafficCapacity / (saturation_flow_rate * laneEffectGreenTime>1d?1:laneEffectGreenTime));
BigDecimal bd1 = new BigDecimal(laneEffectGreenTime==0d?0:saturation); BigDecimal bd1 = new BigDecimal(laneEffectGreenTime==0d?0:saturation);
Double d2 = bd1.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue(); Double d2 = bd1.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue();
laneNorm.setSaturation(d2); laneNorm.setSaturation(d2);
...@@ -478,6 +515,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -478,6 +515,8 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
int AllGreenTime = 0; int AllGreenTime = 0;
//从字典中获取当前车道所有流向信息 //从字典中获取当前车道所有流向信息
List<Map> currentLane = laneList.stream().filter(a -> laneId.equals(String.valueOf(a.get("lane_id")))).collect(Collectors.toList()); List<Map> currentLane = laneList.stream().filter(a -> laneId.equals(String.valueOf(a.get("lane_id")))).collect(Collectors.toList());
//已经参与计算的相位,不累计计算
Map<Object, Object> HasStage = new HashMap<>();
//获取周期的相位信息 //获取周期的相位信息
for (Map map : currentLane) { for (Map map : currentLane) {
int greeTime = 0; int greeTime = 0;
...@@ -487,8 +526,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -487,8 +526,12 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
List<StageForDirection> stageForDirectionList = stage.getStageForDirectionList(); List<StageForDirection> stageForDirectionList = stage.getStageForDirectionList();
for (StageForDirection dir : stageForDirectionList) { for (StageForDirection dir : stageForDirectionList) {
if (position.equals(dir.getDirection()) && name.equals(dir.getTraffic_flow_direction())) { if (position.equals(dir.getDirection()) && name.equals(dir.getTraffic_flow_direction())) {
greeTime += stage.getValidGreen(); //已经参与计算的相位,不参与累积绿灯时间计算
if(!HasStage.containsKey(stage.getPhaseValue())){
AllGreenTime += stage.getValidGreen(); AllGreenTime += stage.getValidGreen();
HasStage.put(stage.getPhaseValue(),"1");
}
greeTime += stage.getValidGreen();
} }
} }
} }
...@@ -522,7 +565,7 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl ...@@ -522,7 +565,7 @@ public class LaneNormProcessFunction extends KeyedCoProcessFunction<String, Cycl
maxQueneLength=queneLength; maxQueneLength=queneLength;
} }
} }
//旧版逻辑 根据有到达时间 没有进入时间的车辆取 最大排队长度 //todo 旧版逻辑根据有到达时间 没有进入时间的车辆取 最大排队长度
/*i = laneCarMap.get(laneId).stream(). /*i = laneCarMap.get(laneId).stream().
filter(a -> StringUtils.isNotEmpty(a.getArrayTime()) && filter(a -> StringUtils.isNotEmpty(a.getArrayTime()) &&
StringUtils.isEmpty(a.getInCorssTime())).collect(Collectors.toList()).size();*/ StringUtils.isEmpty(a.getInCorssTime())).collect(Collectors.toList()).size();*/
......
...@@ -33,8 +33,8 @@ public class LaneNormJob { ...@@ -33,8 +33,8 @@ public class LaneNormJob {
//env.setRestartStrategy(RestartStrategies.noRestart()); //env.setRestartStrategy(RestartStrategies.noRestart());
//设置重启策略 //设置重启策略
env.setRestartStrategy( RestartStrategies.fixedDelayRestart(10, Time.seconds(10))); env.setRestartStrategy( RestartStrategies.fixedDelayRestart(10, Time.seconds(10)));
DataStream<String> cycleStringStream = FlinkUtils.createKafkaStream(args, env,"signal_cycle_data","laneNormJob"); DataStream<String> cycleStringStream = FlinkUtils.createKafkaStreamHuangzhun(args, env,"signal_cycle_data","laneNormJob1");
DataStream<String> carStringStream = FlinkUtils.createKafkaStream(args, env,"trips_event_info","laneNormJob"); DataStream<String> carStringStream = FlinkUtils.createKafkaStreamHuangzhun(args, env,"trips_event_info","laneNormJob1");
KeySelector<Cycle, String> cycleKeySelect = new KeySelector<Cycle, String>() { KeySelector<Cycle, String> cycleKeySelect = new KeySelector<Cycle, String>() {
@Override @Override
public String getKey(Cycle cycle) throws Exception { public String getKey(Cycle cycle) throws Exception {
...@@ -52,14 +52,14 @@ public class LaneNormJob { ...@@ -52,14 +52,14 @@ public class LaneNormJob {
public Cycle map(String cycle) throws Exception { public Cycle map(String cycle) throws Exception {
Cycle cycle1 = JSON.parseObject(cycle, Cycle.class); Cycle cycle1 = JSON.parseObject(cycle, Cycle.class);
cycle1 = getCycle(cycle1); cycle1 = getCycle(cycle1);
System.out.println("周期数据->"+cycle); //System.out.println("周期数据->"+cycle);
return cycle1; return cycle1;
} }
}; };
MapFunction<String, TravelEvent> getTravelEventFunction = new MapFunction<String, TravelEvent>() { MapFunction<String, TravelEvent> getTravelEventFunction = new MapFunction<String, TravelEvent>() {
@Override @Override
public TravelEvent map(String travelEvent) throws Exception { public TravelEvent map(String travelEvent) throws Exception {
System.out.println("旅行事件数据->"+travelEvent); //System.out.println("旅行事件数据->"+travelEvent);
return JSON.parseObject(travelEvent, TravelEvent.class); return JSON.parseObject(travelEvent, TravelEvent.class);
} }
}; };
...@@ -82,7 +82,7 @@ public class LaneNormJob { ...@@ -82,7 +82,7 @@ public class LaneNormJob {
ConnectedStreams<Cycle, TravelEvent> cycleTravelEventConnectedStreams = cycleDataStreamSource.connect(carStream).keyBy(cycle -> cycle.getCrossCode(), travelEvent -> travelEvent.getCrossId()); ConnectedStreams<Cycle, TravelEvent> cycleTravelEventConnectedStreams = cycleDataStreamSource.connect(carStream).keyBy(cycle -> cycle.getCrossCode(), travelEvent -> travelEvent.getCrossId());
SingleOutputStreamOperator<List<LaneNorm>> process = cycleTravelEventConnectedStreams.process(new LaneNormProcessFunction()).setParallelism(4); SingleOutputStreamOperator<List<LaneNorm>> process = cycleTravelEventConnectedStreams.process(new LaneNormProcessFunction()).setParallelism(4);
process.addSink(new LaneNormSink()).name("laneNormJobSink"); process.addSink(new LaneNormSink()).name(" ");
env.execute("laneNormJob"); env.execute("laneNormJob");
}catch (Exception e){ }catch (Exception e){
System.out.println("程序异常关闭->"+e); System.out.println("程序异常关闭->"+e);
......
...@@ -159,4 +159,36 @@ public class FlinkUtils { ...@@ -159,4 +159,36 @@ public class FlinkUtils {
DataStream<String> rawStringStream = env.addSource(kafkaConsumer); DataStream<String> rawStringStream = env.addSource(kafkaConsumer);
return rawStringStream; return rawStringStream;
} }
public static DataStream<String> createKafkaStreamHuangzhun(String[] args, StreamExecutionEnvironment environment, String topics, String groupId) {
ParameterTool kafkaTool = ParameterTool.fromArgs(args);
String servers = kafkaTool.get("bootstrap.servers", defaultBootstrapServers);
String topics2 = kafkaTool.get("kafka.input.topics", topics);
String groupId2 = kafkaTool.get("group.id", groupId);
String autoOffsetReset = kafkaTool.get("auto.offset.reset", defaultAutoOffsetReset);
String checkpointPath = kafkaTool.get("checkpoint.path", defaultCheckpointPath);
long checkpointInterval = kafkaTool.getLong("checkpoint.interval", defaultCheckpointInterval);
String enableAutoCommit = kafkaTool.get("enable.auto.commit", "false");
return buildKafkaDataStreamHuangzhun(environment, servers, topics2, groupId2, autoOffsetReset, checkpointPath, checkpointInterval, enableAutoCommit);
}
private static DataStream<String> buildKafkaDataStreamHuangzhun(StreamExecutionEnvironment env, String servers, String topics, String groupId,
String autoOffsetReset, String checkpointPath, long checkpointInterval, String enableAutoCommit) {
List<String> topicList = Arrays.asList(topics.split(","));
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", servers);
properties.setProperty("group.id", groupId);
properties.setProperty("enable.auto.commit", enableAutoCommit);
properties.setProperty("auto.offset.reset", autoOffsetReset);
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<>(topicList, new SimpleStringSchema(), properties);
kafkaConsumer.setStartFromLatest();
DataStream<String> rawStringStream = env.addSource(kafkaConsumer);
return rawStringStream;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment