-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DatumReader Cache to improve de-serialization performance (#65)
* Add DatumReader Cache to improve de-serialization performance * Fix exception message Co-authored-by: Ravindranath Kakarla <[email protected]>
- Loading branch information
1 parent
c3159ce
commit d8ec6a9
Showing
3 changed files
with
104 additions
and
113 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...in/java/com/amazonaws/services/schemaregistry/deserializers/avro/DatumReaderInstance.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.amazonaws.services.schemaregistry.deserializers.avro; | ||
|
||
import com.amazonaws.services.schemaregistry.utils.AVROUtils; | ||
import com.amazonaws.services.schemaregistry.utils.AvroRecordType; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.io.DatumReader; | ||
import org.apache.avro.specific.SpecificData; | ||
import org.apache.avro.specific.SpecificDatumReader; | ||
import org.apache.avro.specific.SpecificRecord; | ||
|
||
@Slf4j | ||
public class DatumReaderInstance { | ||
private static final AVROUtils AVRO_UTILS = AVROUtils.getInstance(); | ||
|
||
/** | ||
* This method is used to create Avro datum reader for deserialization. By | ||
* default, it is GenericDatumReader; SpecificDatumReader will only be created | ||
* if the user specifies. In this case, the program will check if the user have | ||
* those specific code-generated schema class locally. ReaderSchema will be | ||
* supplied if the user wants to use a specific schema to deserialize the | ||
* message. (Compatibility check will be invoked) | ||
* | ||
* @param writerSchemaDefinition Avro record writer schema. | ||
* @return Avro datum reader for de-serialization | ||
* @throws InstantiationException can be thrown for readerClass.newInstance() | ||
* from java.lang.Class implementation | ||
* @throws IllegalAccessException can be thrown readerClass.newInstance() from | ||
* java.lang.Class implementation | ||
*/ | ||
public static DatumReader<Object> from(String writerSchemaDefinition, AvroRecordType avroRecordType) | ||
throws InstantiationException, IllegalAccessException { | ||
|
||
Schema writerSchema = AVRO_UTILS.parseSchema(writerSchemaDefinition); | ||
|
||
switch (avroRecordType) { | ||
case SPECIFIC_RECORD: | ||
@SuppressWarnings("unchecked") | ||
Class<SpecificRecord> readerClass = SpecificData.get().getClass(writerSchema); | ||
|
||
Schema readerSchema = readerClass.newInstance().getSchema(); | ||
log.debug("Using SpecificDatumReader for de-serializing Avro message, schema: {})", | ||
readerSchema.toString()); | ||
return new SpecificDatumReader<>(writerSchema, readerSchema); | ||
|
||
case GENERIC_RECORD: | ||
log.debug("Using GenericDatumReader for de-serializing Avro message, schema: {})", | ||
writerSchema.toString()); | ||
return new GenericDatumReader<>(writerSchema); | ||
|
||
default: | ||
String message = String.format("Unsupported AvroRecordType: %s", | ||
avroRecordType.getName()); | ||
throw new UnsupportedOperationException(message); | ||
} | ||
} | ||
} |
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