我有两个大小相同但值类型不同的映射。现在我需要迭代它们并生成新的映射。我尝试了流,但无法解析Map.Entry
。我使用的是JDK 11;
Map<TopicPartition, OffsetAndMetadata> consumerGroupOffsets = getConsumerGroupOffsets(groupId);
Map<TopicPartition, Long> topicEndOffsets = getTopicEndOffsets(groupId, consumerGroupOffsets.keySet());
Map<Object, Object> consumerGroupLag = consumerGroupOffsets.entrySet().stream()
.map(entry -> mapEntry(entry.getKey(), new OffsetAndLag(topicEndOffsets.get(entry.getKey()), entry.getValue().offset())))
滞后的来源有点令人困惑,因为您似乎只检索偏移量。
但这就是获取长值的结构,而不管它们被称为什么(我在OffsetAndLag
类的构造函数中猜测(
Map<TopicPartition, OffsetAndMetadata> consumerGroupOffsets = ...;
Map<TopicPartition, Long> topicEndOffsets = ...;
- 使用
consumerGroupOffsets
中的密钥作为目标密钥 - 使用该键从
topicEndOffsets
检索滞后(或偏移( - 使用该键的值(应该是
OffsetAndMetadata
(来获得偏移(或滞后(
Map<TopicPartition, OffsetAndLag> consumerGroupLag =
consumerGroupOffsets.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey,
entry -> new OffsetAndLag(
topicEndOffsets
.get(entry.getKey()),
entry.getValue().offset())));
预期OffsetAndLag
类(或类似(
static class OffsetAndLag {
public long offset;
public long lag;
public OffsetAndLag(long offset, long lag) {
this.offset = offset;
this.lag = lag;
}
}