Use buffer.remaining() instead of buffer.array().
Kafka's message.payload() returns a ByteBuffer, and if you need access to the raw bytes, it's tempting to just call message.payload().array(), but that would be the wrong thing to do. That returns the backing array to the ByteBuffer, which may (and in the case of Kafka usually is) larger than the data it represents, with offsets and limits maintained by ByteBuffer.
Sadly, there is no single-line solution. Below is the most concise way to get the raw bytes out of a Kafka message and into a plain vanilla byte array.
ByteBuffer buffer = message.payload(); byte bytes = new byte[buffer.remaining()]; buffer.get(bytes);
If sending Avro over Kafka, then serialize using EncoderFactory instead of DataFileWriter with ByteArrayOutputStream, for two reasons:
- DataFileWriter writes out an entire Avro file, complete with schema. Sending the schema with every Kafka message is a waste.
- If you plan to consume Kafka messages with Camus, then you need to use EncoderFactory because Camus uses DecoderFactory.
If sending Avro over Kafka and using EncoderFactory, then don't forget to call encoder.flush() after writing, as in this Avro Hello World example.
Thursday, March 21, 2013
Three tips on using Kafka: