我正在用Java实现Flink程序来使用MapStateDescriptor
处理状态。我将基于此source进行实施。由于某些原因,MapState
保留了先前的值,因此我无法计算所需的平均值。在调试averageTemp
时始终为空,并且在其中找不到任何密钥。我在实现过程中缺少什么?
import java.util.Map;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.sense.flink.mqtt.MqttTemperature;
import org.sense.flink.mqtt.TemperatureMqttConsumer;
public class SensorsMultipleReadingMqttEdgentQEP {
public SensorsMultipleReadingMqttEdgentQEP() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
DataStream<MqttTemperature> temperatureStream01 = env.addSource(new TemperatureMqttConsumer("topic-edgent-01"));
DataStream<MqttTemperature> temperatureStream02 = env.addSource(new TemperatureMqttConsumer("topic-edgent-02"));
DataStream<MqttTemperature> temperatureStream03 = env.addSource(new TemperatureMqttConsumer("topic-edgent-03"));
DataStream<MqttTemperature> temperatureStreams = temperatureStream01.union(temperatureStream02)
.union(temperatureStream03);
DataStream<Tuple2<String, Double>> average = temperatureStreams.keyBy(new TemperatureKeySelector())
.map(new AverageTempMapper());
average.print();
env.execute("SensorsMultipleReadingMqttEdgentQEP");
}
public static class TemperatureKeySelector implements KeySelector<MqttTemperature, Integer> {
private static final long serialVersionUID = 5905504239899133953L;
@Override
public Integer getKey(MqttTemperature value) throws Exception {
return value.getId();
}
}
public static class AverageTempMapper extends RichMapFunction<MqttTemperature, Tuple2<String, Double>> {
private static final long serialVersionUID = -5489672634096634902L;
private MapState<String, Double> averageTemp;
@Override
public void open(Configuration parameters) throws Exception {
averageTemp = getRuntimeContext()
.getMapState(new MapStateDescriptor<>("average-temperature", String.class, Double.class));
}
@Override
public Tuple2<String, Double> map(MqttTemperature value) throws Exception {
String key = "no-room";
Double temp = value.getTemp();
if (value.getId().equals(1) || value.getId().equals(2) || value.getId().equals(3)) {
key = "room-A";
} else if (value.getId().equals(4) || value.getId().equals(5) || value.getId().equals(6)) {
key = "room-B";
} else if (value.getId().equals(7) || value.getId().equals(8) || value.getId().equals(9)) {
key = "room-C";
}
// NEVER ITERATES ON THE averageTemp
for (Map.Entry<String, Double> entry: averageTemp.entries()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
System.out.println("value: " + value);
if (averageTemp.contains(key)) { // NEVER CONTAINS A KEY
System.out.println("yes: " + key);
temp = (averageTemp.get(key) + value.getTemp()) / 2;
} else {
averageTemp.put(key, temp);
}
return Tuple2.of(key, temp);
}
}
}
**编辑:**好。我误解了这个问题。该代码将先前的状态保存在MapState上。我错了,因为我正在调试代码。但是实际上我遇到的问题是,它启动了多个线程,并且在开始计算平均值之前,它至少覆盖了我地图的值三倍。
答案 0 :(得分:2)
地图功能中的状态基于每个键。因此,当您调用map函数并获得map状态时,它将用于MqttTemperature
记录中正在处理的id。
鉴于您想要每个房间的平均温度,我的处理方式如下:
TemperatureKeySelector
以返回room-A
,room-B
或room-C
。AverageTempMapper
中,有两个ValueState
变量-一个是温度的总和(一个Double),另一个是一个计数。调用您的map()
方法时,如果这两个ValueState
变量之一为空,则将其初始化为0,然后求和/递增。