Debian Codegraph @

doc/content/en/docs/1.12.0/Specification/_index.md

raw
1 ---
2 title: "Specification"
3 linkTitle: "Specification"
4 weight: 4
5 date: 2021-10-25
6 aliases:
7 - spec.html
8 ---
9
10 <!--
11
12 Licensed to the Apache Software Foundation (ASF) under one
13 or more contributor license agreements. See the NOTICE file
14 distributed with this work for additional information
15 regarding copyright ownership. The ASF licenses this file
16 to you under the Apache License, Version 2.0 (the
17 "License"); you may not use this file except in compliance
18 with the License. You may obtain a copy of the License at
19
20 https://www.apache.org/licenses/LICENSE-2.0
21
22 Unless required by applicable law or agreed to in writing,
23 software distributed under the License is distributed on an
24 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25 KIND, either express or implied. See the License for the
26 specific language governing permissions and limitations
27 under the License.
28
29 -->
30
31 ## Introduction
32 This document defines Apache Avro. It is intended to be the authoritative specification. Implementations of Avro must adhere to this document.
33
34 ## Schema Declaration {#schema-declaration}
35 A Schema is represented in [JSON](https://www.json.org/) by one of:
36
37 * A JSON string, naming a defined type.
38 * A JSON object, of the form:
39 ```js
40 {"type": "typeName", ...attributes...}
41 ```
42 where _typeName_ is either a primitive or derived type name, as defined below. Attributes not defined in this document are permitted as metadata, but must not affect the format of serialized data.
43 * A JSON array, representing a union of embedded types.
44
45 ## Primitive Types
46 The set of primitive type names is:
47
48 * _null_: no value
49 * _boolean_: a binary value
50 * _int_: 32-bit signed integer
51 * _long_: 64-bit signed integer
52 * _float_: single precision (32-bit) IEEE 754 floating-point number
53 * _double_: double precision (64-bit) IEEE 754 floating-point number
54 * _bytes_: sequence of 8-bit unsigned bytes
55 * _string_: unicode character sequence
56
57 Primitive types have no specified attributes.
58
59 Primitive type names are also defined type names. Thus, for example, the schema "string" is equivalent to:
60 ```json
61 {"type": "string"}
62 ```
63
64 ## Complex Types
65 Avro supports six kinds of complex types: _records_, _enums_, _arrays_, _maps_, _unions_ and _fixed_.
66
67 ### Records {#schema-record}
68 Records use the type name "record" and support the following attributes:
69
70 * _name_: a JSON string providing the name of the record (required).
71 * _namespace_, a JSON string that qualifies the name (optional);
72 * _doc_: a JSON string providing documentation to the user of this schema (optional).
73 * _aliases_: a JSON array of strings, providing alternate names for this record (optional).
74 * _fields_: a JSON array, listing fields (required). Each field is a JSON object with the following attributes:
75 * _name_: a JSON string providing the name of the field (required), and
76 * _doc_: a JSON string describing this field for users (optional).
77 * _type_: a [schema]({{< ref "#schema-declaration" >}} "Schema declaration"), as defined above
78 * _order_: specifies how this field impacts sort ordering of this record (optional). Valid values are "ascending" (the default), "descending", or "ignore". For more details on how this is used, see the sort order section below.
79 * _aliases_: a JSON array of strings, providing alternate names for this field (optional).
80 * _default_: A default value for this field, only used when reading instances that lack the field for schema evolution purposes. The presence of a default value does not make the field optional at encoding time. Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema that matches in the union. Default values for bytes and fixed fields are JSON strings, where Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255. Avro encodes a field even if its value is equal to its default.
81
82 *field default values*
83
84 | **avro type** | **json type** | **example** |
85 |---------------|----------------|-------------|
86 | null | null | `null` |
87 | boolean | boolean | `true` |
88 | int,long | integer | `1` |
89 | float,double | number | `1.1` |
90 | bytes | string | `"\u00FF"` |
91 | string | string | `"foo"` |
92 | record | object | `{"a": 1}` |
93 | enum | string | `"FOO"` |
94 | array | array | `[1]` |
95 | map | object | `{"a": 1}` |
96 | fixed | string | `"\u00ff"` |
97
98 For example, a linked-list of 64-bit values may be defined with:
99 ```jsonc
100 {
101 "type": "record",
102 "name": "LongList",
103 "aliases": ["LinkedLongs"], // old name for this
104 "fields" : [
105 {"name": "value", "type": "long"}, // each element has a long
106 {"name": "next", "type": ["null", "LongList"]} // optional next element
107 ]
108 }
109 ```
110
111 ### Enums
112 Enums use the type name "enum" and support the following attributes:
113
114 * _name_: a JSON string providing the name of the enum (required).
115 * _namespace_, a JSON string that qualifies the name (optional);
116 * _aliases_: a JSON array of strings, providing alternate names for this enum (optional).
117 * _doc_: a JSON string providing documentation to the user of this schema (optional).
118 * _symbols_: a JSON array, listing symbols, as JSON strings (required). All symbols in an enum must be unique; duplicates are prohibited. Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]* (the same requirement as for [names]({{< ref "#names" >}} "Names")).
119 * _default_: A default value for this enumeration, used during resolution when the reader encounters a symbol from the writer that isn't defined in the reader's schema (optional). The value provided here must be a JSON string that's a member of the symbols array. See documentation on schema resolution for how this gets used.
120
121 For example, playing card suits might be defined with:
122 ```json
123 {
124 "type": "enum",
125 "name": "Suit",
126 "symbols" : ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
127 }
128 ```
129
130 ### Arrays
131 Arrays use the type name "array" and support a single attribute:
132
133 * _items_: the schema of the array's items.
134
135 For example, an array of strings is declared with:
136 ```json
137 {
138 "type": "array",
139 "items" : "string",
140 "default": []
141 }
142 ```
143
144 ### Maps
145 Maps use the type name "map" and support one attribute:
146
147 * _values_: the schema of the map's values.
148
149 Map keys are assumed to be strings.
150
151 For example, a map from string to long is declared with:
152 ```json
153 {
154 "type": "map",
155 "values" : "long",
156 "default": {}
157 }
158 ```
159
160 ### Unions
161 Unions, as mentioned above, are represented using JSON arrays. For example, `["null", "string"]` declares a schema which may be either a null or string.
162
163 (Note that when a [default value]({{< ref "#schema-record" >}} "Schema record") is specified for a record field whose type is a union, the type of the default value must match with one element of the union.
164
165 Unions may not contain more than one schema with the same type, except for the named types record, fixed and enum. For example, unions containing two array types or two map types are not permitted, but two types with different names are permitted. (Names permit efficient resolution when reading and writing unions.)
166
167 Unions may not immediately contain other unions.
168
169 ### Fixed
170 Fixed uses the type name "fixed" and supports the following attributes:
171
172 * _name_: a string naming this fixed (required).
173 * _namespace_, a string that qualifies the name (optional);
174 * _aliases_: a JSON array of strings, providing alternate names for this enum (optional).
175 * _size_: an integer, specifying the number of bytes per value (required).
176
177 For example, 16-byte quantity may be declared with:
178 ```json
179 {"type": "fixed", "size": 16, "name": "md5"}
180 ```
181
182 ### Names
183 Record, enums and fixed are named types. Each has a fullname that is composed of two parts: a name and a namespace, separated by a dot. Equality of names is defined on the fullname &ndash; it is an error to specify two different types with the same name.
184
185 Record fields and enum symbols have names as well (but no namespace). Equality of field names and enum symbols is defined within their scope (the record/enum that defines them). It is an error to define multiple fields or enum symbols with the same name in a single type. Fields and enum symbols across scopes are never equal, so field names and enum symbols can be reused in a different type.
186
187 The name portion of the fullname of named types, record field names, and enum symbols must:
188
189 * start with [A-Za-z_]
190 * subsequently contain only [A-Za-z0-9_]
191
192 A namespace is a dot-separated sequence of such names. The empty string may also be used as a namespace to indicate the null namespace. Equality of names (including field names and enum symbols) as well as fullnames is case-sensitive.
193
194 The null namespace may not be used in a dot-separated sequence of names. So the grammar for a namespace is:
195 ```
196 <empty> | <name>[(<dot><name>)*]
197 ```
198
199 In record, enum and fixed definitions, the fullname is determined according to the algorithm below the example:
200
201 ```
202 {
203 "type": "record",
204 "name": "Example",
205 "doc": "A simple name (attribute) and no namespace attribute: use the null namespace (\"\"); the fullname is 'Example'.",
206 "fields": [
207 {
208 "name": "inheritNull",
209 "type": {
210 "type": "enum",
211 "name": "Simple",
212 "doc": "A simple name (attribute) and no namespace attribute: inherit the null namespace of the enclosing type 'Example'. The fullname is 'Simple'.",
213 "symbols": ["a", "b"]
214 }
215 }, {
216 "name": "explicitNamespace",
217 "type": {
218 "type": "fixed",
219 "name": "Simple",
220 "namespace": "explicit",
221 "doc": "A simple name (attribute) and a namespace (attribute); the fullname is 'explicit.Simple' (this is a different type than of the 'inheritNull' field).",
222 "size": 12
223 }
224 }, {
225 "name": "fullName",
226 "type": {
227 "type": "record",
228 "name": "a.full.Name",
229 "namespace": "ignored",
230 "doc": "A name attribute with a fullname, so the namespace attribute is ignored. The fullname is 'a.full.Name', and the namespace is 'a.full'.",
231 "fields": [
232 {
233 "name": "inheritNamespace",
234 "type": {
235 "type": "enum",
236 "name": "Understanding",
237 "doc": "A simple name (attribute) and no namespace attribute: inherit the namespace of the enclosing type 'a.full.Name'. The fullname is 'a.full.Understanding'.",
238 "symbols": ["d", "e"]
239 }
240 }
241 ]
242 }
243 }
244 ]
245 }
246 ```
247
248 The fullname of a record, enum or fixed definition is determined by the required `name` and optional `namespace` attributes like this:
249
250 * A fullname is specified. If the name specified contains a dot, then it is assumed to be a fullname, and any namespace also specified is ignored. For example, use "name": "org.foo.X" to indicate the fullname org.foo.X.
251 * A simple name (a name that contains no dots) and namespace are both specified. For example, one might use "name": "X", "namespace": "org.foo" to indicate the fullname org.foo.X.
252 * A simple name only is specified (a name that contains no dots). In this case the namespace is taken from the most tightly enclosing named schema or protocol, and the fullname is constructed from that namespace and the name. For example, if "name": "X" is specified, and this occurs within a field of the record definition of org.foo.Y, then the fullname is org.foo.X. This also happens if there is no enclosing namespace (i.e., the enclosing schema definition has the null namespace).
253
254 References to previously defined names are as in the latter two cases above: if they contain a dot they are a fullname, if they do not contain a dot, the namespace is the namespace of the enclosing definition.
255
256 Primitive type names (`null`, `boolean`, `int`, `long`, `float`, `double`, `bytes`, `string`) have no namespace and their names may not be defined in any namespace.
257
258 Complex types (`record`, `enum`, `array`, `map`, `fixed`) have no namespace, but their names (as well as `union`) are permitted to be reused as type names. This can be confusing to the human reader, but is always unambiguous for binary serialization. Due to the limitations of JSON encoding, it is a best practice to use a namespace when using these names.
259
260 A schema or protocol may not contain multiple definitions of a fullname. Further, a name must be defined before it is used ("before" in the depth-first, left-to-right traversal of the JSON parse tree, where the types attribute of a protocol is always deemed to come "before" the messages attribute.)
261
262 ### Aliases
263 Named types and fields may have aliases. An implementation may optionally use aliases to map a writer's schema to the reader's. This facilitates both schema evolution as well as processing disparate datasets.
264
265 Aliases function by re-writing the writer's schema using aliases from the reader's schema. For example, if the writer's schema was named "Foo" and the reader's schema is named "Bar" and has an alias of "Foo", then the implementation would act as though "Foo" were named "Bar" when reading. Similarly, if data was written as a record with a field named "x" and is read as a record with a field named "y" with alias "x", then the implementation would act as though "x" were named "y" when reading.
266
267 A type alias may be specified either as a fully namespace-qualified, or relative to the namespace of the name it is an alias for. For example, if a type named "a.b" has aliases of "c" and "x.y", then the fully qualified names of its aliases are "a.c" and "x.y".
268
269 Aliases are alternative names, and thus subject to the same uniqueness constraints as names. Aliases should be valid names, but this is not required: any string is accepted as an alias. When aliases are used "to map a writer's schema to the reader's" (see above), this allows schema evolution to correct illegal names in old schemata.
270
271 ## Fixing an invalid, but previously accepted, schema
272 Over time, rules and validations on schemas have changed. It is therefore possible that a schema used to work with an older version of Avro, but now fails to parse.
273
274 This can have several reasons, as listed below. Each reason also describes a fix, which can be applied using [schema resolution]({{< ref "#schema-resolution" >}}): you fix the problems in the schema in a way that is compatible, and then you can use the new schema to read the old data.
275
276 ### Invalid names
277 Invalid names of types and fields can be corrected by renaming (using an [alias]({{< ref "#aliases" >}})). This works for simple names, namespaces and fullnames.
278
279 This fix is twofold: first, you add the invalid name as an alias to the type/field. Then, you change the name to any valid name.
280
281 ### Invalid defaults
282 Default values are only used to fill in missing data when reading. Invalid defaults create invalid values in these cases. The fix is to correct the default values.
283
284
285 ## Data Serialization and Deserialization
286 Binary encoded Avro data does not include type information or field names. The benefit is that the serialized data is small, but as a result a schema must always be used in order to read Avro data correctly. The best way to ensure that the schema is structurally identical to the one used to write the data is to use the exact same schema.
287
288 Therefore, files or systems that store Avro data should always include the writer's schema for that data. Avro-based remote procedure call (RPC) systems must also guarantee that remote recipients of data have a copy of the schema used to write that data. In general, it is advisable that any reader of Avro data should use a schema that is the same (as defined more fully in [Parsing Canonical Form for Schemas]({{< ref "#parsing-canonical-form-for-schemas" >}} "Parsing Canonical Form for Schemas")) as the schema that was used to write the data in order to deserialize it correctly. Deserializing data into a newer schema is accomplished by specifying an additional schema, the results of which are described in [Schema Resolution]({{< ref "#schema-resolution" >}}).
289
290 In general, both serialization and deserialization proceed as a depth-first, left-to-right traversal of the schema, serializing or deserializing primitive types as they are encountered. Therefore, it is possible, though not advisable, to read Avro data with a schema that does not have the same Parsing Canonical Form as the schema with which the data was written. In order for this to work, the serialized primitive values must be compatible, in order value by value, with the items in the deserialization schema. For example, int and long are always serialized the same way, so an int could be deserialized as a long. Since the compatibility of two schemas depends on both the data and the serialization format (eg. binary is more permissive than JSON because JSON includes field names, eg. a long that is too large will overflow an int), it is simpler and more reliable to use schemas with identical Parsing Canonical Form.
291
292 ### Encodings
293 Avro specifies two serialization encodings: binary and JSON. Most applications will use the binary encoding, as it is smaller and faster. But, for debugging and web-based applications, the JSON encoding may sometimes be appropriate.
294
295 ### Binary Encoding {#binary-encoding}
296 Binary encoding does not include field names, self-contained information about the types of individual bytes, nor field or record separators. Therefore readers are wholly reliant on the schema used when the data was encoded.
297
298 #### Primitive Types
299 Primitive types are encoded in binary as follows:
300
301 * _null_ is written as zero bytes.
302 * a _boolean_ is written as a single byte whose value is either 0 (false) or 1 (true).
303 * _int_ and _long_ values are written using [variable-length](https://lucene.apache.org/java/3_5_0/fileformats.html#VInt) [zig-zag](https://code.google.com/apis/protocolbuffers/docs/encoding.html#types) coding. Some examples:
304
305 | *value* | *hex* |
306 |---|---|
307 | 0 | 00 |
308 |-1 | 01 |
309 | 1 | 02 |
310 |-2 | 03 |
311 | 2 | 04 |
312 |...|...|
313 |-64 | 7f |
314 |64 | 80 01|
315 |...|...|
316
317 * a _float_ is written as 4 bytes. The float is converted into a 32-bit integer using a method equivalent to Java's [floatToRawIntBits](https://docs.oracle.com/javase/8/docs/api/java/lang/Float.html#floatToRawIntBits-float-) and then encoded in little-endian format.
318 * a _double_ is written as 8 bytes. The double is converted into a 64-bit integer using a method equivalent to Java's [doubleToRawLongBits](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#doubleToRawLongBits-double-) and then encoded in little-endian format.
319 * _bytes_ are encoded as a long followed by that many bytes of data.
320 * a _string_ is encoded as a long followed by that many bytes of UTF-8 encoded character data.
321 For example, the three-character string "foo" would be encoded as the long value 3 (encoded as hex 06) followed by the UTF-8 encoding of 'f', 'o', and 'o' (the hex bytes 66 6f 6f):
322 ```
323 06 66 6f 6f
324 ```
325
326 ### Complex Types
327 Complex types are encoded in binary as follows:
328
329 #### Records
330 A record is encoded by encoding the values of its fields in the order that they are declared. In other words, a record is encoded as just the concatenation of the encodings of its fields. Field values are encoded per their schema.
331
332 For example, the record schema
333 ```json
334 {
335 "type": "record",
336 "name": "test",
337 "fields" : [
338 {"name": "a", "type": "long"},
339 {"name": "b", "type": "string"}
340 ]
341 }
342 ```
343
344 An instance of this record whose a field has value 27 (encoded as hex 36) and whose b field has value "foo" (encoded as hex bytes 06 66 6f 6f), would be encoded simply as the concatenation of these, namely the hex byte sequence:
345 ```
346 36 06 66 6f 6f
347 ```
348
349 #### Enums
350 An enum is encoded by a int, representing the zero-based position of the symbol in the schema.
351
352 For example, consider the enum:
353 ```json
354 {"type": "enum", "name": "Foo", "symbols": ["A", "B", "C", "D"] }
355 ```
356
357 This would be encoded by an int between zero and three, with zero indicating "A", and 3 indicating "D".
358
359 #### Arrays
360 Arrays are encoded as a series of blocks. Each block consists of a long count value, followed by that many array items. A block with count zero indicates the end of the array. Each item is encoded per the array's item schema.
361
362 If a block's count is negative, its absolute value is used, and the count is followed immediately by a long block size indicating the number of bytes in the block. This block size permits fast skipping through data, e.g., when projecting a record to a subset of its fields.
363
364 For example, the array schema
365 ```json
366 {"type": "array", "items": "long"}
367 ```
368 an array containing the items 3 and 27 could be encoded as the long value 2 (encoded as hex 04) followed by long values 3 and 27 (encoded as hex 06 36) terminated by zero:
369 ```
370 04 06 36 00
371 ```
372
373 The blocked representation permits one to read and write arrays larger than can be buffered in memory, since one can start writing items without knowing the full length of the array.
374
375 #### Maps {#schema-maps}
376 Maps are encoded as a series of _blocks_. Each block consists of a `long` _count_ value, followed by that many key/value pairs. A block with count zero indicates the end of the map. Each item is encoded per the map's value schema.
377
378 If a block's count is negative, its absolute value is used, and the count is followed immediately by a `long` block size indicating the number of bytes in the block. This block size permits fast skipping through data, e.g., when projecting a record to a subset of its fields.
379
380 The blocked representation permits one to read and write maps larger than can be buffered in memory, since one can start writing items without knowing the full length of the map.
381
382 #### Unions
383 A union is encoded by first writing an `int` value indicating the zero-based position within the union of the schema of its value. The value is then encoded per the indicated schema within the union.
384
385 For example, the union schema `["null","string"]` would encode:
386
387 * _null_ as zero (the index of "null" in the union):
388 `00`
389 * the string "a" as one (the index of "string" in the union, 1, encoded as hex 02), followed by the serialized string:
390 `02 02 61`
391 NOTE: Currently for C/C++ implementations, the positions are practically an int, but theoretically a long. In reality, we don't expect unions with 215M members
392
393 #### Fixed
394 Fixed instances are encoded using the number of bytes declared in the schema.
395
396 ### JSON Encoding
397 Except for unions, the JSON encoding is the same as is used to encode [field default values]({{< ref "#schema-record" >}}).
398
399 The value of a union is encoded in JSON as follows:
400
401 * if its type is _null_, then it is encoded as a JSON _null_;
402 * otherwise it is encoded as a JSON object with one name/value pair whose name is the type's name and whose value is the recursively encoded value. For Avro's named types (record, fixed or enum) the user-specified name is used, for other types the type name is used.
403
404 For example, the union schema `["null","string","Foo"]`, where Foo is a record name, would encode:
405
406 * _null_ as _null_;
407 * the string "a" as `{"string": "a"}` and
408 * a Foo instance as `{"Foo": {...}}`, where `{...}` indicates the JSON encoding of a Foo instance.
409
410 Note that the original schema is still required to correctly process JSON-encoded data. For example, the JSON encoding does not distinguish between _int_ and _long_, _float_ and _double_, records and maps, enums and strings, etc.
411
412 ### Single-object encoding
413 In some situations a single Avro serialized object is to be stored for a longer period of time. One very common example is storing Avro records for several weeks in an [Apache Kafka](https://kafka.apache.org/) topic.
414
415 In the period after a schema change this persistence system will contain records that have been written with different schemas. So the need arises to know which schema was used to write a record to support schema evolution correctly. In most cases the schema itself is too large to include in the message, so this binary wrapper format supports the use case more effectively.
416
417 #### Single object encoding specification
418 Single Avro objects are encoded as follows:
419
420 1. A two-byte marker, `C3 01`, to show that the message is Avro and uses this single-record format (version 1).
421 1. The 8-byte little-endian CRC-64-AVRO [fingerprint]({{< ref "#schema-fingerprints" >}} "Schema fingerprints") of the object's schema.
422 1. The Avro object encoded using [Avro's binary encoding]({{< ref "#binary-encoding" >}}).
423
424 Implementations use the 2-byte marker to determine whether a payload is Avro. This check helps avoid expensive lookups that resolve the schema from a fingerprint, when the message is not an encoded Avro payload.
425
426 ## Sort Order
427 Avro defines a standard sort order for data. This permits data written by one system to be efficiently sorted by another system. This can be an important optimization, as sort order comparisons are sometimes the most frequent per-object operation. Note also that Avro binary-encoded data can be efficiently ordered without deserializing it to objects.
428
429 Data items may only be compared if they have identical schemas. Pairwise comparisons are implemented recursively with a depth-first, left-to-right traversal of the schema. The first mismatch encountered determines the order of the items.
430
431 Two items with the same schema are compared according to the following rules.
432
433 * _null_ data is always equal.
434 * _boolean_ data is ordered with false before true.
435 * _int_, _long_, _float_ and _double_ data is ordered by ascending numeric value.
436 * _bytes_ and fixed data are compared lexicographically by unsigned 8-bit values.
437 * _string_ data is compared lexicographically by Unicode code point. Note that since UTF-8 is used as the binary encoding for strings, sorting of bytes and string binary data is identical.
438 * _array_ data is compared lexicographically by element.
439 * _enum_ data is ordered by the symbol's position in the enum schema. For example, an enum whose symbols are `["z", "a"]` would sort "z" values before "a" values.
440 * _union_ data is first ordered by the branch within the union, and, within that, by the type of the branch. For example, an `["int", "string"]` union would order all int values before all string values, with the ints and strings themselves ordered as defined above.
441 * _record_ data is ordered lexicographically by field. If a field specifies that its order is:
442 * "ascending", then the order of its values is unaltered.
443 * "descending", then the order of its values is reversed.
444 * "ignore", then its values are ignored when sorting.
445 * _map_ data may not be compared. It is an error to attempt to compare data containing maps unless those maps are in an `"order":"ignore"` record field.
446
447 ## Object Container Files
448 Avro includes a simple object container file format. A file has a schema, and all objects stored in the file must be written according to that schema, using binary encoding. Objects are stored in blocks that may be compressed. Syncronization markers are used between blocks to permit efficient splitting of files for MapReduce processing.
449
450 Files may include arbitrary user-specified metadata.
451
452 A file consists of:
453
454 * A file header, followed by
455 * one or more file data blocks.
456
457 A file header consists of:
458
459 * Four bytes, ASCII 'O', 'b', 'j', followed by 1.
460 * file metadata, including the schema.
461 * The 16-byte, randomly-generated sync marker for this file.
462
463 File metadata is written as if defined by the following [map]({{< ref "#schema-maps" >}}) schema:
464 ```json
465 {"type": "map", "values": "bytes"}
466 ```
467 All metadata properties that start with "avro." are reserved. The following file metadata properties are currently used:
468
469 * **avro.schema** contains the schema of objects stored in the file, as JSON data (required).
470 * **avro.codec** the name of the compression codec used to compress blocks, as a string. Implementations are required to support the following codecs: "null" and "deflate". If codec is absent, it is assumed to be "null". The codecs are described with more detail below.
471
472 A file header is thus described by the following schema:
473 ```json
474 {"type": "record", "name": "org.apache.avro.file.Header",
475 "fields" : [
476 {"name": "magic", "type": {"type": "fixed", "name": "Magic", "size": 4}},
477 {"name": "meta", "type": {"type": "map", "values": "bytes"}},
478 {"name": "sync", "type": {"type": "fixed", "name": "Sync", "size": 16}}
479 ]
480 }
481 ```
482
483 A file data block consists of:
484
485 * A long indicating the count of objects in this block.
486 * A long indicating the size in bytes of the serialized objects in the current block, after any codec is applied
487 * The serialized objects. If a codec is specified, this is compressed by that codec.
488 * The file's 16-byte sync marker.
489
490 A file data block is thus described by the following schema:
491 ```json
492 {"type": "record", "name": "org.apache.avro.file.DataBlock",
493 "fields" : [
494 {"name": "count", "type": "long"},
495 {"name": "data", "type": "bytes"},
496 {"name": "sync", "type": {"type": "fixed", "name": "Sync", "size": 16}}
497 ]
498 }
499 ```
500
501 Each block's binary data can be efficiently extracted or skipped without deserializing the contents. The combination of block size, object counts, and sync markers enable detection of corrupt blocks and help ensure data integrity.
502
503 ### Required Codecs
504
505 _null_
506
507 The "null" codec simply passes through data uncompressed.
508
509 _deflate_
510
511 The "deflate" codec writes the data block using the deflate algorithm as specified in [RFC 1951](https://www.isi.edu/in-notes/rfc1951.txt), and typically implemented using the zlib library. Note that this format (unlike the "zlib format" in RFC 1950) does not have a checksum.
512
513 ### Optional Codecs
514 _bzip2_
515
516 The "bzip2" codec uses the [bzip2](https://sourceware.org/bzip2/) compression library.
517
518 _snappy_
519
520 The "snappy" codec uses Google's [Snappy](https://code.google.com/p/snappy/) compression library. Each compressed block is followed by the 4-byte, big-endian CRC32 checksum of the uncompressed data in the block.
521
522 _xz_
523
524 The "xz" codec uses the [XZ](https://tukaani.org/xz/) compression library.
525
526 _zstandard_
527
528 The "zstandard" codec uses Facebook's [Zstandard](https://facebook.github.io/zstd/) compression library.
529
530 ### Protocol Declaration
531 Avro protocols describe RPC interfaces. Like schemas, they are defined with JSON text.
532
533 A protocol is a JSON object with the following attributes:
534
535 * _protocol_, a string, the name of the protocol (required);
536 * _namespace_, an optional string that qualifies the name (optional);
537 * _doc_, an optional string describing this protocol;
538 * _types_, an optional list of definitions of named types (records, enums, fixed and errors). An error definition is just like a record definition except it uses "error" instead of "record". Note that forward references to named types are not permitted.
539 * _messages_, an optional JSON object whose keys are message names and whose values are objects whose attributes are described below. No two messages may have the same name.
540
541 The name and namespace qualification rules defined for schema objects apply to protocols as well.
542
543 ### Messages
544 A message has attributes:
545
546 * a _doc_, an optional description of the message,
547 * a _request_, a list of named, typed parameter schemas (this has the same form as the fields of a record declaration);
548 * a _response_ schema;
549 * an optional union of declared error schemas. The effective union has "string" prepended to the declared union, to permit transmission of undeclared "system" errors. For example, if the declared error union is `["AccessError"]`, then the effective union is `["string", "AccessError"]`. When no errors are declared, the effective error union is `["string"]`. Errors are serialized using the effective union; however, a protocol's JSON declaration contains only the declared union.
550 * an optional one-way boolean parameter.
551
552 A request parameter list is processed equivalently to an anonymous record. Since record field lists may vary between reader and writer, request parameters may also differ between the caller and responder, and such differences are resolved in the same manner as record field differences.
553
554 The one-way parameter may only be true when the response type is `"null"` and no errors are listed.
555
556 ### Sample Protocol
557 For example, one may define a simple HelloWorld protocol with:
558 ```json
559 {
560 "namespace": "com.acme",
561 "protocol": "HelloWorld",
562 "doc": "Protocol Greetings",
563
564 "types": [
565 {"name": "Greeting", "type": "record", "fields": [
566 {"name": "message", "type": "string"}]},
567 {"name": "Curse", "type": "error", "fields": [
568 {"name": "message", "type": "string"}]}
569 ],
570
571 "messages": {
572 "hello": {
573 "doc": "Say hello.",
574 "request": [{"name": "greeting", "type": "Greeting" }],
575 "response": "Greeting",
576 "errors": ["Curse"]
577 }
578 }
579 }
580 ```
581
582 ## Protocol Wire Format
583
584 ### Message Transport
585 Messages may be transmitted via different transport mechanisms.
586
587 To the transport, a _message_ is an opaque byte sequence.
588
589 A transport is a system that supports:
590
591 * **transmission of request messages**
592 * **receipt of corresponding response messages**
593 Servers may send a response message back to the client corresponding to a request message. The mechanism of correspondence is transport-specific. For example, in HTTP it is implicit, since HTTP directly supports requests and responses. But a transport that multiplexes many client threads over a single socket would need to tag messages with unique identifiers.
594
595 Transports may be either stateless or stateful. In a stateless transport, messaging assumes no established connection state, while stateful transports establish connections that may be used for multiple messages. This distinction is discussed further in the [handshake](#handshake) section below.
596
597 #### HTTP as Transport
598 When [HTTP](https://www.w3.org/Protocols/rfc2616/rfc2616.html) is used as a transport, each Avro message exchange is an HTTP request/response pair. All messages of an Avro protocol should share a single URL at an HTTP server. Other protocols may also use that URL. Both normal and error Avro response messages should use the 200 (OK) response code. The chunked encoding may be used for requests and responses, but, regardless the Avro request and response are the entire content of an HTTP request and response. The HTTP Content-Type of requests and responses should be specified as "avro/binary". Requests should be made using the POST method.
599
600 HTTP is used by Avro as a stateless transport.
601
602 ### Message Framing
603 Avro messages are _framed_ as a list of buffers.
604
605 Framing is a layer between messages and the transport. It exists to optimize certain operations.
606
607 The format of framed message data is:
608
609 * a series of buffers, where each buffer consists of:
610 * a four-byte, big-endian _buffer length_, followed by
611 * that many bytes of _buffer_ data.
612 * a message is always terminated by a zero-length buffer.
613
614 Framing is transparent to request and response message formats (described below). Any message may be presented as a single or multiple buffers.
615
616 Framing can permit readers to more efficiently get different buffers from different sources and for writers to more efficiently store different buffers to different destinations. In particular, it can reduce the number of times large binary objects are copied. For example, if an RPC parameter consists of a megabyte of file data, that data can be copied directly to a socket from a file descriptor, and, on the other end, it could be written directly to a file descriptor, never entering user space.
617
618 A simple, recommended, framing policy is for writers to create a new segment whenever a single binary object is written that is larger than a normal output buffer. Small objects are then appended in buffers, while larger objects are written as their own buffers. When a reader then tries to read a large object the runtime can hand it an entire buffer directly, without having to copy it.
619
620 ### Handshake
621 The purpose of the handshake is to ensure that the client and the server have each other's protocol definition, so that the client can correctly deserialize responses, and the server can correctly deserialize requests. Both clients and servers should maintain a cache of recently seen protocols, so that, in most cases, a handshake will be completed without extra round-trip network exchanges or the transmission of full protocol text.
622
623 RPC requests and responses may not be processed until a handshake has been completed. With a stateless transport, all requests and responses are prefixed by handshakes. With a stateful transport, handshakes are only attached to requests and responses until a successful handshake response has been returned over a connection. After this, request and response payloads are sent without handshakes for the lifetime of that connection.
624
625 The handshake process uses the following record schemas:
626 ```json
627 {
628 "type": "record",
629 "name": "HandshakeRequest", "namespace":"org.apache.avro.ipc",
630 "fields": [
631 {"name": "clientHash",
632 "type": {"type": "fixed", "name": "MD5", "size": 16}},
633 {"name": "clientProtocol", "type": ["null", "string"]},
634 {"name": "serverHash", "type": "MD5"},
635 {"name": "meta", "type": ["null", {"type": "map", "values": "bytes"}]}
636 ]
637 }
638 {
639 "type": "record",
640 "name": "HandshakeResponse", "namespace": "org.apache.avro.ipc",
641 "fields": [
642 {"name": "match",
643 "type": {"type": "enum", "name": "HandshakeMatch",
644 "symbols": ["BOTH", "CLIENT", "NONE"]}},
645 {"name": "serverProtocol",
646 "type": ["null", "string"]},
647 {"name": "serverHash",
648 "type": ["null", {"type": "fixed", "name": "MD5", "size": 16}]},
649 {"name": "meta",
650 "type": ["null", {"type": "map", "values": "bytes"}]}
651 ]
652 }
653 ```
654
655 * A client first prefixes each request with a `HandshakeRequest` containing just the hash of its protocol and of the server's protocol (`clientHash!=null, clientProtocol=null, serverHash!=null`), where the hashes are 128-bit MD5 hashes of the JSON protocol text. If a client has never connected to a given server, it sends its hash as a guess of the server's hash, otherwise it sends the hash that it previously obtained from this server.
656 The server responds with a HandshakeResponse containing one of:
657 * `match=BOTH, serverProtocol=null, serverHash=null` if the client sent the valid hash of the server's protocol and the server knows what protocol corresponds to the client's hash. In this case, the request is complete and the response data immediately follows the HandshakeResponse.
658 * `match=CLIENT, serverProtocol!=null, serverHash!=null` if the server has previously seen the client's protocol, but the client sent an incorrect hash of the server's protocol. The request is complete and the response data immediately follows the HandshakeResponse. The client must use the returned protocol to process the response and should also cache that protocol and its hash for future interactions with this server.
659 * `match=NONE` if the server has not previously seen the client's protocol. The serverHash and serverProtocol may also be non-null if the server's protocol hash was incorrect.
660 In this case the client must then re-submit its request with its protocol text (`clientHash!=null, clientProtocol!=null, serverHash!=null`) and the server should respond with a successful match (match=BOTH, serverProtocol=null, serverHash=null) as above.
661
662 The meta field is reserved for future handshake enhancements.
663
664 ### Call Format
665 A _call_ consists of a request message paired with its resulting response or error message. Requests and responses contain extensible metadata, and both kinds of messages are framed as described above.
666
667 The format of a call request is:
668
669 * _request metadata_, a map with values of type bytes
670 * the _message name_, an Avro string, followed by
671 * the _message parameters_. Parameters are serialized according to the message's request declaration.
672 When the empty string is used as a message name a server should ignore the parameters and return an empty response. A client may use this to ping a server or to perform a handshake without sending a protocol message.
673
674 When a message is declared one-way and a stateful connection has been established by a successful handshake response, no response data is sent. Otherwise the format of the call response is:
675
676 * _response metadata_, a map with values of type bytes
677 * a one-byte error _flag_ boolean, followed by either:
678 * if the error flag is false, the message _response_, serialized per the message's response schema.
679 * if the error flag is true, the _error_, serialized per the message's effective error union schema.
680
681 ### Schema Resolution {#schema-resolution}
682 A reader of Avro data, whether from an RPC or a file, can always parse that data because the original schema must be provided along with the data. However, the reader may be programmed to read data into a different schema. For example, if the data was written with a different version of the software than it is read, then fields may have been added or removed from records. This section specifies how such schema differences should be resolved.
683
684 We refer to the schema used to write the data as the writer's schema, and the schema that the application expects the reader's schema. Differences between these should be resolved as follows:
685
686 * It is an error if the two schemas do not _match_.
687 To match, one of the following must hold:
688 * both schemas are arrays whose item types match
689 * both schemas are maps whose value types match
690 * both schemas are enums whose (unqualified) names match
691 * both schemas are fixed whose sizes and (unqualified) names match
692 * both schemas are records with the same (unqualified) name
693 * either schema is a union
694 * both schemas have same primitive type
695 * the writer's schema may be promoted to the reader's as follows:
696 * int is promotable to long, float, or double
697 * long is promotable to float or double
698 * float is promotable to double
699 * string is promotable to bytes
700 * bytes is promotable to string
701 * **if both are records**:
702 * the ordering of fields may be different: fields are matched by name.
703 * schemas for fields with the same name in both records are resolved recursively.
704 * if the writer's record contains a field with a name not present in the reader's record, the writer's value for that field is ignored.
705 * if the reader's record schema has a field that contains a default value, and writer's schema does not have a field with the same name, then the reader should use the default value from its field.
706 * if the reader's record schema has a field with no default value, and writer's schema does not have a field with the same name, an error is signalled.
707 * **if both are enums**:
708 if the writer's symbol is not present in the reader's enum and the reader has a default value, then that value is used, otherwise an error is signalled.
709
710 * **if both are arrays**:
711 This resolution algorithm is applied recursively to the reader's and writer's array item schemas.
712
713 * **if both are maps**:
714 This resolution algorithm is applied recursively to the reader's and writer's value schemas.
715
716 * **if both are unions**:
717 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.
718
719 * **if reader's is a union, but writer's is not**
720 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.
721
722 * **if writer's is a union, but reader's is not**
723 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.
724
725 A schema's _doc_ fields are ignored for the purposes of schema resolution. Hence, the _doc_ portion of a schema may be dropped at serialization.
726
727 ### Parsing Canonical Form for Schemas {#parsing-canonical-form-for-schemas}
728 One of the defining characteristics of Avro is that a reader must use the schema used by the writer of the data in order to know how to read the data. This assumption results in a data format that's compact and also amenable to many forms of schema evolution. However, the specification so far has not defined what it means for the reader to have the "same" schema as the writer. Does the schema need to be textually identical? Well, clearly adding or removing some whitespace to a JSON expression does not change its meaning. At the same time, reordering the fields of records clearly does change the meaning. So what does it mean for a reader to have "the same" schema as a writer?
729
730 Parsing Canonical Form is a transformation of a writer's schema that let's us define what it means for two schemas to be "the same" for the purpose of reading data written against the schema. It is called Parsing Canonical Form because the transformations strip away parts of the schema, like "doc" attributes, that are irrelevant to readers trying to parse incoming data. It is called Canonical Form because the transformations normalize the JSON text (such as the order of attributes) in a way that eliminates unimportant differences between schemas. If the Parsing Canonical Forms of two different schemas are textually equal, then those schemas are "the same" as far as any reader is concerned, i.e., there is no serialized data that would allow a reader to distinguish data generated by a writer using one of the original schemas from data generated by a writing using the other original schema. (We sketch a proof of this property in a companion document.)
731
732 The next subsection specifies the transformations that define Parsing Canonical Form. But with a well-defined canonical form, it can be convenient to go one step further, transforming these canonical forms into simple integers ("fingerprints") that can be used to uniquely identify schemas. The subsection after next recommends some standard practices for generating such fingerprints.
733
734 #### Transforming into Parsing Canonical Form
735 Assuming an input schema (in JSON form) that's already UTF-8 text for a _valid_ Avro schema (including all quotes as required by JSON), the following transformations will produce its Parsing Canonical Form:
736
737 * [PRIMITIVES] Convert primitive schemas to their simple form (e.g., int instead of `{"type":"int"}`).
738 * [FULLNAMES] Replace short names with fullnames, using applicable namespaces to do so. Then eliminate namespace attributes, which are now redundant.
739 * [STRIP] Keep only attributes that are relevant to parsing data, which are: _type_, _name_, _fields_, _symbols_, _items_, _values_, _size_. Strip all others (e.g., _doc_ and _aliases_).
740 * [ORDER] Order the appearance of fields of JSON objects as follows: _name_, _type_, _fields_, _symbols_, _items_, _values_, _size_. For example, if an object has _type_, _name_, and _size_ fields, then the _name_ field should appear first, followed by the _type_ and then the _size_ fields.
741 * [STRINGS] For all JSON string literals in the schema text, replace any escaped characters (e.g., \uXXXX escapes) with their UTF-8 equivalents.
742 * [INTEGERS] Eliminate quotes around and any leading zeros in front of JSON integer literals (which appear in the _size_ attributes of _fixed_ schemas).
743 * [WHITESPACE] Eliminate all whitespace in JSON outside of string literals.
744
745 #### Schema Fingerprints {#schema-fingerprints}
746 "[A] fingerprinting algorithm is a procedure that maps an arbitrarily large data item (such as a computer file) to a much shorter bit string, its fingerprint, that uniquely identifies the original data for all practical purposes" (quoted from [Wikipedia](https://en.wikipedia.org/wiki/Fingerprint_(computing))). In the Avro context, fingerprints of Parsing Canonical Form can be useful in a number of applications; for example, to cache encoder and decoder objects, to tag data items with a short substitute for the writer's full schema, and to quickly negotiate common-case schemas between readers and writers.
747
748 In designing fingerprinting algorithms, there is a fundamental trade-off between the length of the fingerprint and the probability of collisions. To help application designers find appropriate points within this trade-off space, while encouraging interoperability and ease of implementation, we recommend using one of the following three algorithms when fingerprinting Avro schemas:
749
750 * When applications can tolerate longer fingerprints, we recommend using the [SHA-256 digest algorithm](https://en.wikipedia.org/wiki/SHA-2) to generate 256-bit fingerprints of Parsing Canonical Forms. Most languages today have SHA-256 implementations in their libraries.
751 * At the opposite extreme, the smallest fingerprint we recommend is a 64-bit [Rabin fingerprint](https://en.wikipedia.org/wiki/Rabin_fingerprint). Below, we provide pseudo-code for this algorithm that can be easily translated into any programming language. 64-bit fingerprints should guarantee uniqueness for schema caches of up to a million entries (for such a cache, the chance of a collision is 3E-8). We don't recommend shorter fingerprints, as the chances of collisions is too great (for example, with 32-bit fingerprints, a cache with as few as 100,000 schemas has a 50% chance of having a collision).
752 * Between these two extremes, we recommend using the [MD5 message digest](https://en.wikipedia.org/wiki/MD5) to generate 128-bit fingerprints. These make sense only where very large numbers of schemas are being manipulated (tens of millions); otherwise, 64-bit fingerprints should be sufficient. As with SHA-256, MD5 implementations are found in most libraries today.
753
754 These fingerprints are not meant to provide any security guarantees, even the longer SHA-256-based ones. Most Avro applications should be surrounded by security measures that prevent attackers from writing random data and otherwise interfering with the consumers of schemas. We recommend that these surrounding mechanisms be used to prevent collision and pre-image attacks (i.e., "forgery") on schema fingerprints, rather than relying on the security properties of the fingerprints themselves.
755
756 Rabin fingerprints are [cyclic redundancy checks](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) computed using irreducible polynomials. In the style of the Appendix of [RFC 1952](https://www.ietf.org/rfc/rfc1952.txt) (pg 10), which defines the CRC-32 algorithm, here's our definition of the 64-bit AVRO fingerprinting algorithm:
757 ```java
758 long fingerprint64(byte[] buf) {
759 if (FP_TABLE == null) initFPTable();
760 long fp = EMPTY;
761 for (int i = 0; i < buf.length; i++)
762 fp = (fp >>> 8) ^ FP_TABLE[(int)(fp ^ buf[i]) & 0xff];
763 return fp;
764 }
765
766 static long EMPTY = 0xc15d213aa4d7a795L;
767 static long[] FP_TABLE = null;
768
769 void initFPTable() {
770 FP_TABLE = new long[256];
771 for (int i = 0; i < 256; i++) {
772 long fp = i;
773 for (int j = 0; j < 8; j++)
774 fp = (fp >>> 1) ^ (EMPTY & -(fp & 1L));
775 FP_TABLE[i] = fp;
776 }
777 }
778 ```
779
780 Readers interested in the mathematics behind this algorithm may want to read [Chapter 14 of the Second Edition of Hacker's Delight](https://books.google.com/books?id=XD9iAwAAQBAJ&pg=PA319). (Unlike RFC-1952 and the book chapter, we prepend a single one bit to messages. We do this because CRCs ignore leading zero bits, which can be problematic. Our code prepends a one-bit by initializing fingerprints using EMPTY, rather than initializing using zero as in RFC-1952 and the book chapter.)
781
782 ## Logical Types
783 A logical type is an Avro primitive or complex type with extra attributes to represent a derived type. The attribute `logicalType` must always be present for a logical type, and is a string with the name of one of the logical types listed later in this section. Other attributes may be defined for particular logical types.
784
785 A logical type is always serialized using its underlying Avro type so that values are encoded in exactly the same way as the equivalent Avro type that does not have a `logicalType` attribute. Language implementations may choose to represent logical types with an appropriate native type, although this is not required.
786
787 Language implementations must ignore unknown logical types when reading, and should use the underlying Avro type. If a logical type is invalid, for example a decimal with scale greater than its precision, then implementations should ignore the logical type and use the underlying Avro type.
788
789 ### Decimal
790 The `decimal` logical type represents an arbitrary-precision signed decimal number of the form _unscaled × 10<sup>-scale</sup>_.
791
792 A `decimal` logical type annotates Avro _bytes_ or _fixed_ types. The byte array must contain the two's-complement representation of the unscaled integer value in big-endian byte order. The scale is fixed, and is specified using an attribute.
793
794 The following attributes are supported:
795
796 * _scale_, a JSON integer representing the scale (optional). If not specified the scale is 0.
797 * _precision_, a JSON integer representing the (maximum) precision of decimals stored in this type (required).
798 For example, the following schema represents decimal numbers with a maximum precision of 4 and a scale of 2:
799 ```json
800 {
801 "type": "bytes",
802 "logicalType": "decimal",
803 "precision": 4,
804 "scale": 2
805 }
806 ```
807 Precision must be a positive integer greater than zero. If the underlying type is a _fixed_, then the precision is limited by its size. An array of length n can store at most _floor(log<sub>10</sub>(2<sup>8 × n - 1</sup> - 1))_ base-10 digits of precision.
808
809 Scale must be zero or a positive integer less than or equal to the precision.
810
811 For the purposes of schema resolution, two schemas that are `decimal` logical types _match_ if their scales and precisions match.
812
813 **alternative**
814
815 As it's not always possible to fix scale and precision in advance for a decimal field, `big-decimal` is another `decimal` logical type restrict to Avro _bytes_.
816
817 _Currently only available in Java and Rust_.
818
819 ```json
820 {
821 "type": "bytes",
822 "logicalType": "big-decimal"
823 }
824 ```
825 Here, as scale property is stored in value itself it needs more bytes than preceding `decimal` type, but it allows more flexibility.
826
827 ### UUID
828
829 The `uuid` logical type represents a random generated universally unique identifier (UUID).
830
831 A `uuid` logical type annotates an Avro `string` or `fixed` of length 16. Both the string and `fixed` byte layout have to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt).
832
833 The following schemas represent a uuid:
834
835 ```json
836 {
837 "type": "string",
838 "logicalType": "uuid"
839 }
840 ```
841
842 ```json
843 {
844 "type": "fixed",
845 "size": 16,
846 "logicalType": "uuid"
847 }
848 ```
849
850 ### Date
851 The `date` logical type represents a date within the calendar, with no reference to a particular time zone or time of day.
852
853 A `date` logical type annotates an Avro `int`, where the int stores the number of days from the unix epoch, 1 January 1970 (ISO calendar).
854
855 The following schema represents a date:
856 ```json
857 {
858 "type": "int",
859 "logicalType": "date"
860 }
861 ```
862
863 ### Time (millisecond precision) {#time_ms}
864 The `time-millis` logical type represents a time of day, with no reference to a particular calendar, time zone or date, with a precision of one millisecond.
865
866 A `time-millis` logical type annotates an Avro `int`, where the int stores the number of milliseconds after midnight, 00:00:00.000.
867
868 ### Time (microsecond precision)
869 The `time-micros` logical type represents a time of day, with no reference to a particular calendar, time zone or date, with a precision of one microsecond.
870
871 A `time-micros` logical type annotates an Avro `long`, where the long stores the number of microseconds after midnight, 00:00:00.000000.
872
873 ### Timestamps {#timestamps}
874
875 The `timestamp-{millis,micros,nanos}` logical type represents an instant on the global timeline, independent of a particular time zone or calendar. Upon reading a value back, we can only reconstruct the instant, but not the original representation. In practice, such timestamps are typically displayed to users in their local time zones, therefore they may be displayed differently depending on the execution environment.
876
877 - `timestamp-millis`: logical type annotates an Avro `long`, where the long stores the number of milliseconds from the unix epoch, 1 January 1970 00:00:00.000.
878 - `timestamp-micros`: logical type annotates an Avro `long`, where the long stores the number of microseconds from the unix epoch, 1 January 1970 00:00:00.000000.
879 - `timestamp-nanos`: logical type annotates an Avro `long`, where the long stores the number of nanoseconds from the unix epoch, 1 January 1970 00:00:00.000000000.
880
881 Example: Given an event at noon local time (12:00) on January 1, 2000, in Helsinki where the local time was two hours east of UTC (UTC+2). The timestamp is first shifted to UTC 2000-01-01T10:00:00 and that is then converted to Avro long 946720800000 (milliseconds) and written.
882
883 ### Local Timestamps {#local_timestamp}
884
885 The `local-timestamp-{millis,micros,nanos}` logical type represents a timestamp in a local timezone, regardless of what specific time zone is considered local.
886
887 - `local-timestamp-millis`: logical type annotates an Avro `long`, where the long stores the number of milliseconds, from 1 January 1970 00:00:00.000.
888 - `local-timestamp-micros`: logical type annotates an Avro `long`, where the long stores the number of microseconds, from 1 January 1970 00:00:00.000000.
889 - `local-timestamp-nanos`: logical type annotates an Avro `long`, where the long stores the number of nanoseconds, from 1 January 1970 00:00:00.000000000.
890
891 Example: Given an event at noon local time (12:00) on January 1, 2000, in Helsinki where the local time was two hours east of UTC (UTC+2). The timestamp is converted to Avro long 946728000000 (milliseconds) and then written.
892
893 ### Duration
894 The `duration` logical type represents an amount of time defined by a number of months, days and milliseconds. This is not equivalent to a number of milliseconds, because, depending on the moment in time from which the duration is measured, the number of days in the month and number of milliseconds in a day may differ. Other standard periods such as years, quarters, hours and minutes can be expressed through these basic periods.
895
896 A `duration` logical type annotates Avro `fixed` type of size 12, which stores three little-endian unsigned integers that represent durations at different granularities of time. The first stores a number in months, the second stores a number in days, and the third stores a number in milliseconds.