Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify data type for key when producing a message #464

Open
canique opened this issue Aug 29, 2021 · 1 comment
Open

Specify data type for key when producing a message #464

canique opened this issue Aug 29, 2021 · 1 comment

Comments

@canique
Copy link

canique commented Aug 29, 2021

  • PHP version: 7.4
  • librdkafka version: 0.11.6
  • php-rdkafka version:
  • kafka version:

When producing a record like this:

$topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($payload), intval($recordId));

I'd need the key ($recordId) to be an 8 byte long value.
I think it is treated as string? Is there a way to tell the library to produce a long?

@arnaud-lb
Copy link
Owner

Hi @canique

I'm not sure that Kafka can store different key types: I believe that keys are just byte arrays.

If you pass an int to ->produce(), it will be converted to its decimal representation as a string. You can convert that back with intval($key) when consuming messages.

If you prefer an 8 byte long value, you can use pack('J', $key). You can convert that back with unpack('J', $key) when consuming messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants