关于Avro中的Unions类型

Avro中的复杂类型

Avro支持六种复杂类型:

  • records
  • enums
  • arrays
  • maps
  • unions
  • fixed

Unions类型介绍

Unions使用JSON数组表示。
例如,[“null”,“string”]声明一个字段的类型可以是null或string。

注意
当替类型为union的字段指定默认值时,默认值的类型必须与union的第一个元素匹配。比如,在与对于包含“null”的union(例如,[“null”,“string”]),通常会列出“null” 首先,因为此类联合的默认值通常为null。

除了包含record,fixed和enum类型之外,union中不能包含多个相同类型, 例如,union不允许包含两种array类型或两种map类型, 即[“array”,“array”]和[“map”,“map”]都是非法的。
Unions中也不能包含其他unions.

Unions类型的排序规则

union类型的数据根据其包含的数据类型得顺序进行排序。 例如,定义为[“int”,“string”] 的union将首先对所有int类型的值进行排序,然后对所有字符串值行排序。

Union在读/写数据时的规则 [TO-DO]

比较写入字段数据时的类型(writer’s schema)与读取数据时的类型(reader’s schema):

  • 如果它们都是unions:
    The first schema in the reader’s union that matches the selected writer’s union schema is recursively resolved against it. if none match, an error is signalled.

  • 如果读取时使用union, 但写入的时候不是
    The first schema in the reader’s union that matches the writer’s schema is recursively resolved against it. If none match, an error is signalled.

  • 如果写入时使用union, 但读取的时候不是
    If the reader’s schema matches the selected writer’s schema, it is recursively resolved against it. If they do not match, an error is signalled.

原文地址:https://www.cnblogs.com/lestatzhang/p/10611329.html