*/
private Double min;
/\*\*
* 最大值
*/
private Double max;
}
##### 报警事件
/**
* 报警消息
*/
@Data
public class AlarmMessage {
/\*\*
* 设备
*/
private Integer deviceId;
/\*\*
* 报警时间
*/
private Long timestamp;
/**
* 触发报警的采集变量名称
*/
private String alarmVar;
/\*\*
* 触发报警的采集值
*/
private Number alarmValue;
}
#### 开始实现
public class IotMonitorJob {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
environment.setParallelism(1);
// 采集数据Stream
DataStreamSource<IotData> iotDataStream = getIotStream(environment);
// 报警规则Stream
DataStreamSource<AlarmRule> ruleConfig = getRuleConfig(environment);
// 缓存报警规则 并监控报警数据
SingleOutputStreamOperator<AlarmMessage> alarmStream = iotDataStream.connect(ruleConfig)
.keyBy(IotData::getDeviceId, AlarmRule::getDeviceId)
.process(new CoProcessFunction<IotData, AlarmRule, AlarmMessage>() {
// 用临时保存设备的报警规则 ,这里的状态交由flink维护
private MapState<Integer, AlarmRule> alarmRuleValueState;
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
// 初始化 ValueState
alarmRuleValueState = getRuntimeContext().getMapState(new MapStateDescriptor<>(