Created
October 21, 2016 16:23
-
-
Save esammer/664e44bf8d3f3af275f225d8c01f4abf to your computer and use it in GitHub Desktop.
An example Kafka encoder implementation for Osso events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Note that this class is a slightly hacked up version of an internal | |
* class used at Rocana. There may be some mistakes in the code when I | |
* removed some Rocana-specific bits from it. I didn't test that it | |
* compiles cleanly. | |
*/ | |
import kafka.serializer.Encoder; | |
import kafka.utils.VerifiableProperties; | |
import org.apache.avro.io.DatumWriter; | |
import org.apache.avro.io.EncoderFactory; | |
import org.apache.avro.specific.SpecificData; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
public class EventEncoder implements Encoder<Event> { | |
private static final int INITIAL_BUFFER_SIZE = 4096; | |
private DatumWriter<Event> eventWriter; | |
private ByteArrayOutputStream outputStream; | |
private org.apache.avro.io.Encoder encoder; | |
public EventEncoder(VerifiableProperties properties) { | |
// There's nothing to configure, so properties isn't used. -esammer | |
eventWriter = SpecificData.get().createDatumWriter(Event.SCHEMA$); | |
outputStream = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE); | |
encoder = EncoderFactory.get().directBinaryEncoder(outputStream, null); | |
} | |
@Override | |
public byte[] toBytes(Event event) { | |
outputStream.reset(); | |
try { | |
eventWriter.write(event, encoder); | |
encoder.flush(); | |
} catch (IOException e) { | |
throw new EncoderException(String.format("Unable to encode event:%s - Exception follows.", event), e); | |
} | |
return outputStream.toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is released under the Apache License version 2.0.
This uses Kafka 0.8.x APIs, but it should be trivial to make it work with later versions.