4

There is an org.apache.kafka.common.serialization.ByteArraySerializer class in Kafka's Producer API for Java and a org.apache.kafka.common.serialization.ByteArrayDeserializer for the Consumer API.

Are there equivalent classes if you're working with Kafka in Python?

kellanburket
  • 12,250
  • 3
  • 46
  • 73
  • This looks like what you're looking for https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3 – pjames Jan 17 '20 at 17:59

1 Answers1

5

There's no built-in byte-array serializer/deserializer for Python, but it's not hard to implement. If, for instance, you want to convert a string to its underlying hexadecimal representation in your producer you can implement it this way:

KafkaProducer(value_serializer=lambda v: binascii.hexlify(v.encode('utf-8')))

If, on the other hand, you had incoming bytes in your consumer and wanted to convert them into their string representation, you could do it like this.

KafkaConsumer(value_deserializer=lambda v: binascii.unhexlify(a).decode('utf-8'))

Answers are for Python 3+.

kellanburket
  • 12,250
  • 3
  • 46
  • 73