【发布时间】:2019-11-26 22:05:05
【问题描述】:
我正在尝试在 Flink 作业的窗口函数中使用 HashMap。是否可以将所有并行算子的所有元素都存储在一个算子上的 HashMap 中?
public class SeewoUserWindowFunction implements WindowFunction<ObjectNode, LabelInfo, String, TimeWindow> {
private static final Logger logger = LoggerFactory.getLogger(SeewoUserWindowFunction.class);
@Override
public void apply(String s, TimeWindow timeWindow, Iterable<ObjectNode> iterable, Collector<LabelInfo> collector) throws Exception {
try {
HashMap<String, LabelInfo> result = new HashMap<>();
iterable.forEach(e -> {
String key = e.get("value").get("$tid").toString() + "/" + e.get("value").get("$code").toString();
if (result.containsKey(key)) {
result.put(key, result.get(key).update(e, timeWindow.getEnd()));
} else {
result.put(key, LabelInfo.of(e, timeWindow.getEnd()));
}
});
result.values().stream().forEach(labelInfo -> collector.collect(labelInfo));
} catch (Exception exception) {
logger.error("parse exception!", exception);
}
}
}
【问题讨论】:
-
嘿,我想我不明白你的问题。我在您的代码中没有看到任何单例。
-
嘿,对不起我的英语。我的意思是,如果我为 n (n > 1) 设置 flink 运算符的并行性,并且我为计数数据定义一个哈希图。这个 hashmap 可以记录所有数据,而不是只记录 n 个并行算子数据中的一个。换句话说,我的问题是并行算子如何交换数据?谢谢
标签: java parallel-processing apache-flink