从Avro GenericRecord获取类型化的值 [英] Get a typed value from an Avro GenericRecord
问题描述
给出 GenericRecord ,建议使用什么方法来检索类型化的值(而不是对象)?我们是否应强制转换值,如果是,则 Utf8 .
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 Avro types to Java types? For example, Avro Array == Java Collection; and Avro String == Java Utf8.
由于每个GenericRecord都包含其架构,所以我希望使用一种类型安全的方法来检索值.
Since every GenericRecord contains its schema, I was hoping for a type-safe way to retrieve values.
推荐答案
Avro具有八种原始类型和五种复杂类型( unions 除外,后者是其他类型的组合).下表将这13种Avro类型映射到它们的输入接口(可以是put
到GenericRecord
的Java类型)及其输出实现(由GenericRecord
返回的具体Java类型). ).这些值适用于Avro 1.7.7.
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
╔═══════════╦════════════════════════╦═══════════════════════════╗
║ 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
在Avro 1.8.0中,enum
类型需要 a GenericEnumSymbol
.它不再接受CharSequence
.
In Avro 1.8.0, the enum
type requires a GenericEnumSymbol
. It no longer accepts CharSequence
.
这篇关于从Avro GenericRecord获取类型化的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!