简体   繁体   中英

How to generate unique sequenceId for your application across multiple instances in KafkaStreams?

I want to generate the unique ordered Id for what we are consuming from the topic, And it should be unique around multiple instances. (Not uuid)

Not sure why you don't wan to use a UUID. But you could use a combination of offset and partition number to compute a unique ID. Something like:

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM