Get a typed value from an Avro GenericRecord

前端 未结 2 377
故里飘歌
故里飘歌 2021-02-01 11:12

Given a GenericRecord, what is the recommended way to retrieve a typed value, as opposed to an Object? Are we expected to cast the values, and if so what is the mapping from Avr

相关标签:
2条回答
  • 2021-02-01 11:34

    Avro has eight primitive types and five complex types (excluding unions which are a combination of other types). The following table maps these 13 Avro types to their input interfaces (the Java types which can be put into a GenericRecord) and their output implementations (the concrete Java types which are returned by a get from a GenericRecord). The values apply to Avro 1.7.7.

    ╔═══════════╦════════════════════════╦═══════════════════════════╗ ║ Avro Type ║ Input Interface ║ Output Implementation ║ ╠═══════════╬════════════════════════╬═══════════════════════════╣ ║ null ║ ║ null ║ ║ boolean ║ java.lang.Boolean ║ java.lang.Boolean ║ ║ int ║ java.lang.Integer ║ java.lang.Integer ║ ║ long ║ java.lang.Long ║ java.lang.Long ║ ║ float ║ java.lang.Float ║ java.lang.Float ║ ║ double ║ java.lang.Double ║ java.lang.Double ║ ║ bytes ║ java.nio.ByteBuffer ║ java.nio.HeapByteBuffer ║ ║ string ║ java.lang.CharSequence ║ org.apache.avro.util.Utf8 ║ ║ record ║ *.GenericRecord ║ *.GenericData$Record ║ ║ enum ║ java.lang.CharSequence ║ *.GenericData$EnumSymbol ║ ║ array ║ java.util.Collection ║ *.GenericData$Array ║ ║ map ║ java.util.Map ║ java.util.HashMap ║ ║ fixed ║ *.GenericFixed ║ *.GenericData$Fixed ║ ╚═══════════╩════════════════════════╩═══════════════════════════╝ * == org.apache.avro.generic


    In Avro 1.8.0, the enum type requires a GenericEnumSymbol. It no longer accepts CharSequence.

    0 讨论(0)
  • 2021-02-01 11:41

    GenericRecords in Avro won't give you a type safe way since it is all figured out at runtime. You can add wrappers that help, but at the end of the day your code needs to implicitly know what it is working with.

    If you want a type safe way, you need to use serialize into a Java SpecificRecord class which can be generated with the Avro maven plugin.

    0 讨论(0)
提交回复
热议问题