Background
- 不同类型的传感器数据频率不同,低频的有的几分钟一个数,高频的有的一秒几十个数、几百个数。低频数据可以使用传统的mysql进行数据的存储。但数据频率比较高时,对程序的计算能力和数据存储能力要求较高,还好有现成的轮子可以直接拿来使用。
- 本文介绍高频数据流的实时计算和存储,应用场景选用风电塔筒提升监测为例。
- 之前的博客中也有探索,这里算是总结下吧,写出来是希望和大家交流,哪里有问题,多多指点哈。
- 这里给出源码【170-tower-lift-processor】,但是你拿到肯定是起不来的,这里分享出来主要是看大体流程哈。
数据处理流程
private static void execStreamJob() {
log.info("****************** 获取 Flink 执行环境");
log.info("");
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
log.info("****************** 配置 RabbitMQ 数据源并解析");
log.info("");
SingleOutputStreamOperator<List<MultiDataEntity>> rawStream = env
.setParallelism(1)
.addSource(DefaultConfig.getRMQSource())
.process(new PrintSpeedFunc())
.filter(new NullFilter())
.process(new RMQPayloadParser());
log.info("****************** 原始数据存储【influx】");
log.info("");
rawStream
.addSink(new RawInfluxSink());
log.info("****************** 把需要进行特殊计算的类型分出来成一道侧流【分流】");
log.info("");
OutputTag<List<MultiDataEntity>> tag1 = new OutputTag<List<MultiDataEntity>>("stream-other") {
};
OutputTag<List<MultiDataEntity>> tag2 = new OutputTag<List<MultiDataEntity>>("stream-yb") {
};
OutputTag<List<MultiDataEntity>> tag3 = new OutputTag<List<MultiDataEntity>>("stream-we") {
};
OutputTag<List<MultiDataEntity>> tag4 = new OutputTag<List<MultiDataEntity>>("stream-wy") {
};
OutputTag<List<MultiDataEntity>> tag5 = new OutputTag<List<MultiDataEntity>>("stream-zd") {
};
OutputTag<List<MultiDataEntity>> tag6 = new OutputTag<List<MultiDataEntity>>("stream-qj") {
};
SingleOutputStreamOperator<List<MultiDataEntity>> splitStream = rawStream
.process(new TagStreamProcessFunc(tag1, tag2, tag3, tag4, tag5, tag6));
DataStream<List<MultiDataEntity>> otherStream = splitStream.getSideOutput(tag1);
DataStream<List<MultiDataEntity>> ybStream = splitStream.getSideOutput(tag2);
DataStream<List<MultiDataEntity>> weStream = splitStream.getSideOutput(tag3);
DataStream<List<MultiDataEntity>> wyStream = splitStream.getSideOutput(tag4);
DataStream<List<MultiDataEntity>> zdStream = splitStream.getSideOutput(tag5);
DataStream<List<MultiDataEntity>> qjStream = splitStream.getSideOutput(tag6);
log.info("****************** 振动原始数据推送 mqtt【振动】");
log.info("");
zdStream
.addSink(new RawMqttSink());
SingleOutputStreamOperator<List<MultiDataEntity>> processedZDStream = zdStream
.assignTimestampsAndWatermarks(new OriginFrequencyWatermark())
.keyBy(new ProjectIdAndSensorTypeSelector())
.window(TumblingEventTimeWindows.of(Time.seconds(5)))
.process(new MaxAndMinProcessor());
processedZDStream
.addSink(new MaxAndMinMqttSink());
log.info("****************** 应变参考点缓存 redis【应变】");
log.info("");
ybStream
.addSink(new YBToRedisSink());
SingleOutputStreamOperator<List<MultiDataEntity>> ybPFStream = ybStream
.process(new YBProcessor());
SingleOutputStreamOperator<List<MultiDataEntity>> ybPSStream = ybPFStream
.assignTimestampsAndWatermarks(new OriginFrequencyWatermark())
.keyBy(new ProjectIdAndSensorTypeSelector())
.window(TumblingEventTimeWindows.of(Time.seconds(10)))
.process(new RemoveMaxAndMinProcessor());
ybPSStream
.addSink(new YBMqttSink());
SingleOutputStreamOperator<List<MultiDataEntity>> processedYBStream = ybPFStream
.assignTimestampsAndWatermarks(new OriginFrequencyWatermark())
.keyBy(new ProjectIdAndSensorTypeSelector())
.window(SlidingEventTimeWindows.of(Time.seconds(10), Time.milliseconds(500)))
.apply(new TenSecMeanApplyFunc());
log.info("****************** 风/叶夹角实时计算【风环境】");
log.info("");
SingleOutputStreamOperator<List<MultiDataEntity>> processedWEStream = weStream
.process(new WEProcessor());
log.info("****************** 位移类型数据处理【位移】");
log.info("");
SingleOutputStreamOperator<List<MultiDataEntity>> processedWYStream = wyStream
.process(new WYProcessor());
wyStream
.assignTimestampsAndWatermarks(new OriginFrequencyWatermark())
.keyBy(new ProjectIdAndSensorTypeSelector())
.window(TumblingEventTimeWindows.of(Time.seconds(2)))
.process(new EvennessProcessor());
log.info("****************** 倾角数据处理【倾角】");
log.info("");
SingleOutputStreamOperator<List<MultiDataEntity>> processedQJStream = qjStream
.assignTimestampsAndWatermarks(new OriginFrequencyWatermark())
.keyBy(new ProjectIdAndSensorTypeSelector())
.window(TumblingEventTimeWindows.of(Time.seconds(1)))
.process(new RemoveMaxProcessor());
processedQJStream
.addSink(new QJRedisSink());
log.info("****************** 合并所有侧流,存储并进行阈值告警判断【合并流】");
log.info("");
DataStream<List<MultiDataEntity>> unionStream = zdStream
.union(processedYBStream)
.union(processedWEStream)
.union(processedWYStream)
.union(processedQJStream);
log.info("****************** 计算结果存储【influxDB】");
log.info("");
unionStream.addSink(new MeanInfluxSink());
log.info("****************** 【阈值告警判断】");
log.info("");
unionStream.addSink(new WarnDetector());
log.info("****************** 任务配置完毕,流计算开始 . . . ");
log.info("");
try {
env.execute(DefaultConfig.getJobName());
} catch (Exception e) {
log.error("流计算任务执行失败!");
log.info("errMsg: {}", e.getMessage());
e.printStackTrace();
}
}
|