Azure Event Hubs supports three protocols for consumers and producers: AMQP, Kafka, and HTTPS : Azure Event Hubs - Exchange events using different protocols - Azure Event Hubs | Microsoft Docs
Event Body
All of the Microsoft AMQP clients represent the event body as an uninterpreted bag of bytes.
Kafka byte[] producer
Code Block |
---|
|
final Properties properties = new Properties();
// add other properties
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
ByteArraySerializer.class.getName());
final KafkaProducer<Long, byte[]> producer = new KafkaProducer<Long, byte[]>(properties);
final byte[] eventBody = new byte[] { 0x01, 0x02, 0x03, 0x04 };
ProducerRecord<Long, byte[]> pr =
new ProducerRecord<Long, byte[]>(myTopic, myPartitionId, myTimeStamp, eventBody); |
Kafka byte[] consumer
Code Block |
---|
|
final Properties properties = new Properties();
// add other properties
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
ByteArrayDeserializer.class.getName());
final KafkaConsumer<Long, byte[]> consumer = new KafkaConsumer<Long, byte[]>(properties);
ConsumerRecord<Long, byte[]> cr = /* receive event */
// cr.value() is a byte[] with values { 0x01, 0x02, 0x03, 0x04 } |
...
Code Block |
---|
|
final Properties properties = new Properties();
// add other properties
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());
final KafkaProducer<Long, String> producer = new KafkaProducer<Long, String>(properties);
final String exampleJson = "{\"name\":\"John\", \"number\":9001}";
ProducerRecord<Long, String> pr =
new ProducerRecord<Long, String>(myTopic, myPartitionId, myTimeStamp, exampleJson); |
...
Code Block |
---|
|
final Properties properties = new Properties();
// add other properties
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class.getName());
final KafkaConsumer<Long, String> consumer = new KafkaConsumer<Long, String>(properties);
ConsumerRecord<Long, Bytes> cr = /* receive event */
final String receivedJson = cr.value(); |
For the AMQP side, both Java and .NET provide built-in ways to convert strings to and from UTF-8 byte sequences. The Microsoft AMQP clients represent events as a class named EventData.
Java AMPQ UTF-8 string producer
Code Block |
---|
|
final String exampleJson = "{\"name\":\"John\", \"number\":9001}";
final EventData ed = EventData.create(exampleJson.getBytes(StandardCharsets.UTF_8)); |
Java AMPQ UTF-8 string consumer
Code Block |
---|
|
EventData ed = /* receive event */
String receivedJson = new String(ed.getBytes(), StandardCharsets.UTF_8); |