如何在KafkaStreams中为你的应用在多个实例中生成唯一的sequenceId?

问题描述 投票:0回答:1

我想为我们从主题中消耗的东西生成唯一的有序Id,而且它应该在多个实例中是唯一的。(不是uuid)

apache-kafka apache-kafka-streams confluent-kafka
1个回答
0
投票

不知道为什么你不想使用UUID。但你可以使用偏移量和分区号的组合来计算一个唯一的ID。比如说:

// you need to know upfront how many partitions the input topis has
private final static int NUMBER_OF_PARTITIONS = ...

// within `Transformer#transform()` using `KStream#transform()`
// (also consider to use #transformValues() instead).

// `context` is given via `init()`

long id = context.offset() * NUMBER_OF_PARTITIONS + context.partition();
© www.soinside.com 2019 - 2024. All rights reserved.