package process;
import kb11.beans.SensorReading;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
/**
* @Author Xulihua
* @Date2021/7/7
* @Description
*/
public class testProcess_KeyProcessFunction2 {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> inputStream = env.socketTextStream("192.168.107.103", 7777);
SingleOutputStreamOperator<SensorReading> mapStream = inputStream.map(x -> {
String[] split = x.split(",");
return new SensorReading(split[0], Long.parseLong(split[1]), Double.parseDouble(split[2]));
});
mapStream.keyBy("id")
.process(new MyKeyProcessFunctions2(10)).print("process");
env.execute("testProcess_KeyProcessFunction2");
}
private static class MyKeyProcessFunctions2 extends KeyedProcessFunction<Tuple, SensorReading, String> {
ValueState<Long> isTimeState = null;
ValueState<Double> isLastTemp = null;
private Integer time;
public MyKeyProcessFunctions2(Integer second) {
this.time=second;
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
String warningStr="warning:温度连续上升";
System.out.println(warningStr);
isTimeState.clear(); // 触发成功也要clear
out.collect(warningStr);
}
@Override
public void open(Configuration parameters) throws Exception {
isTimeState = getRuntimeContext().getState(
new ValueStateDescriptor<Long>("is-timer", Long.class));
isLastTemp = getRuntimeContext().getState(
new ValueStateDescriptor<Double>("is-temp", Double.class, Double.MIN_VALUE));
}
@Override
public void close() throws Exception {
isTimeState.clear();
isLastTemp.clear();
}
// 当 10 内 连续输入的温度大于37 触发定时器 且只触发一次 ,如果10秒内温度下降则删除触发器
@Override
public void processElement(SensorReading value, Context ctx, Collector<String> out) throws Exception {
Tuple currentKey = ctx.getCurrentKey();
Long timestamp = ctx.timestamp();
long l = ctx.timerService().currentProcessingTime();
System.out.println(currentKey + "," + timestamp + "," + l);
// 这边 isLastTemp 要更新 用来给下次输入的值 进行判断
//isTimeState 在没有把定时器删除的时候 也要update 一旦删除之后就直接 clear 因为要重新开始判断了
if (isTimeState.value() == null && value.getTemperature() > isLastTemp.value()) {
ctx.timerService().registerProcessingTimeTimer(l + time*1000);
isTimeState.update(l + time*1000);
System.out.println("设置成功");
} else if (isTimeState.value() != null && value.getTemperature() < isLastTemp.value()) {
ctx.timerService().deleteProcessingTimeTimer(isTimeState.value());
isTimeState.clear(); // 删除成功要clear
System.out.println("删除成功");
}
isLastTemp.update(value.getTemperature());
}
}
}