Created
March 11, 2018 11:45
-
-
Save rohithreddy/818206d0e2e0c428667ca63c050ff178 to your computer and use it in GitHub Desktop.
Avro deserializer for Flink's Data Stream API Kafka Source
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
import org.apache.avro.io.BinaryDecoder; | |
import org.apache.avro.io.DatumReader; | |
import org.apache.avro.io.DecoderFactory; | |
import org.apache.avro.reflect.ReflectDatumReader; | |
import org.apache.avro.specific.SpecificDatumReader; | |
import org.apache.flink.api.common.typeinfo.TypeInformation; | |
import org.apache.flink.api.java.typeutils.TypeExtractor; | |
public class AvroDeserializationSchema<T> implements DeserializationSchema<T> { | |
private final Class<T> avroType; | |
private transient DatumReader<T> reader; | |
private transient BinaryDecoder decoder; | |
public AvroDeserializationSchema(Class<T> avroType) { | |
this.avroType = avroType; | |
} | |
@Override | |
public T deserialize(byte[] message) { | |
ensureInitialized(); | |
try { | |
decoder = DecoderFactory.get().binaryDecoder(message, decoder); | |
return reader.read(null, decoder); | |
} | |
catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public boolean isEndOfStream(T nextElement) { | |
return false; | |
} | |
@Override | |
public TypeInformation<T> getProducedType() { | |
return TypeExtractor.getForClass(avroType); | |
} | |
private void ensureInitialized() { | |
if (reader == null) { | |
if (org.apache.avro.specific.SpecificRecordBase.class.isAssignableFrom(avroType)) { | |
reader = new SpecificDatumReader<T>(avroType); | |
} else { | |
reader = new ReflectDatumReader<T>(avroType); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment