键控状态数据结构
- 1.值状态( Value State ) new ValueStateDescriptor(“last-temp”, Types.of[T])
• 将状态表示为单个的值 - 2.列表状态( List State )
• 将状态表示为⼀组数据的列表 - 3.映射状态( Map State ,字典状态,哈希表状态) new MapStateDescriptor
• 将状态表示为⼀组 Key-Value 对 - 4.聚合状态( Reducing State & Aggregating State )
• 将状态表示为⼀个⽤于聚合操作的列表
package com.claroja;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
public class ListState {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
env
.addSource(new SensorSource())
.filter(r -> r.id.equals("sensor_1"))
.keyBy(r -> r.id)
.process(new KeyedProcessFunction<String, SensorReading, String>() {
private org.apache.flink.api.common.state.ListState<SensorReading> readings;
private ValueState<Long> timerTs;
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
readings = getRuntimeContext().getListState(
new ListStateDescriptor<SensorReading>("readings", SensorReading.class)
);
timerTs = getRuntimeContext().getState(
new ValueStateDescriptor<Long>("ts", Types.LONG)
);
}
@Override
public void processElement(SensorReading sensorReading, Context context, Collector<String> collector) throws Exception {
readings.add(sensorReading);
if (timerTs.value() == null) {
context.timerService().registerProcessingTimeTimer(context.timerService().currentProcessingTime() + 10 * 1000L);
timerTs.update(context.timerService().currentProcessingTime() + 10 * 1000L);
}
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
super.onTimer(timestamp, ctx, out);
long count = 0L;
for(SensorReading r : readings.get()) {
count++;
}
out.collect("there are " + count + " readings");
timerTs.clear();
}
})
.print();
env.execute();
}
}
3831

被折叠的 条评论
为什么被折叠?



