| 1 |
--- |
| 2 |
title: "IDL Language" |
| 3 |
linkTitle: "IDL Language" |
| 4 |
weight: 201 |
| 5 |
--- |
| 6 |
|
| 7 |
<!-- |
| 8 |
|
| 9 |
Licensed to the Apache Software Foundation (ASF) under one |
| 10 |
or more contributor license agreements. See the NOTICE file |
| 11 |
distributed with this work for additional information |
| 12 |
regarding copyright ownership. The ASF licenses this file |
| 13 |
to you under the Apache License, Version 2.0 (the |
| 14 |
"License"); you may not use this file except in compliance |
| 15 |
with the License. You may obtain a copy of the License at |
| 16 |
|
| 17 |
https://www.apache.org/licenses/LICENSE-2.0 |
| 18 |
|
| 19 |
Unless required by applicable law or agreed to in writing, |
| 20 |
software distributed under the License is distributed on an |
| 21 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 22 |
KIND, either express or implied. See the License for the |
| 23 |
specific language governing permissions and limitations |
| 24 |
under the License. |
| 25 |
|
| 26 |
--> |
| 27 |
|
| 28 |
## Introduction |
| 29 |
This document defines Avro IDL, a higher-level language for authoring Avro schemata. Before reading this document, you should have familiarity with the concepts of schemata and protocols, as well as the various primitive and complex types available in Avro. |
| 30 |
|
| 31 |
## Overview |
| 32 |
|
| 33 |
### Purpose |
| 34 |
The aim of the Avro IDL language is to enable developers to author schemata in a way that feels more similar to common programming languages like Java, C++, or Python. Additionally, the Avro IDL language may feel more familiar for those users who have previously used the interface description languages (IDLs) in other frameworks like Thrift, Protocol Buffers, or CORBA. |
| 35 |
|
| 36 |
### Usage |
| 37 |
Each Avro IDL file defines a single Avro Protocol, and thus generates as its output a JSON-format Avro Protocol file with extension .avpr. |
| 38 |
|
| 39 |
To convert a _.avdl_ file into a _.avpr_ file, it may be processed by the `idl` tool. For example: |
| 40 |
```shell |
| 41 |
$ java -jar avro-tools.jar idl src/test/idl/input/namespaces.avdl /tmp/namespaces.avpr |
| 42 |
$ head /tmp/namespaces.avpr |
| 43 |
{ |
| 44 |
"protocol" : "TestNamespace", |
| 45 |
"namespace" : "avro.test.protocol", |
| 46 |
``` |
| 47 |
The `idl` tool can also process input to and from _stdin_ and _stdout_. See `idl --help` for full usage information. |
| 48 |
|
| 49 |
A Maven plugin is also provided to compile .avdl files. To use it, add something like the following to your pom.xml: |
| 50 |
```xml |
| 51 |
<build> |
| 52 |
<plugins> |
| 53 |
<plugin> |
| 54 |
<groupId>org.apache.avro</groupId> |
| 55 |
<artifactId>avro-maven-plugin</artifactId> |
| 56 |
<executions> |
| 57 |
<execution> |
| 58 |
<goals> |
| 59 |
<goal>idl-protocol</goal> |
| 60 |
</goals> |
| 61 |
</execution> |
| 62 |
</executions> |
| 63 |
</plugin> |
| 64 |
</plugins> |
| 65 |
</build> |
| 66 |
``` |
| 67 |
|
| 68 |
## Defining a Protocol in Avro IDL |
| 69 |
An Avro IDL file consists of exactly one protocol definition. The minimal protocol is defined by the following code: |
| 70 |
```java |
| 71 |
protocol MyProtocol { |
| 72 |
} |
| 73 |
``` |
| 74 |
This is equivalent to (and generates) the following JSON protocol definition: |
| 75 |
```json |
| 76 |
{ |
| 77 |
"protocol" : "MyProtocol", |
| 78 |
"types" : [ ], |
| 79 |
"messages" : { |
| 80 |
} |
| 81 |
} |
| 82 |
``` |
| 83 |
The namespace of the protocol may be changed using the @namespace annotation: |
| 84 |
```java |
| 85 |
@namespace("mynamespace") |
| 86 |
protocol MyProtocol { |
| 87 |
} |
| 88 |
``` |
| 89 |
This notation is used throughout Avro IDL as a way of specifying properties for the annotated element, as will be described later in this document. |
| 90 |
|
| 91 |
Protocols in Avro IDL can contain the following items: |
| 92 |
|
| 93 |
* Imports of external protocol and schema files. |
| 94 |
* Definitions of named schemata, including records, errors, enums, and fixeds. |
| 95 |
* Definitions of RPC messages |
| 96 |
|
| 97 |
## Imports |
| 98 |
Files may be imported in one of three formats: |
| 99 |
|
| 100 |
* An IDL file may be imported with a statement like: |
| 101 |
|
| 102 |
`import idl "foo.avdl";` |
| 103 |
|
| 104 |
* A JSON protocol file may be imported with a statement like: |
| 105 |
|
| 106 |
`import protocol "foo.avpr";` |
| 107 |
|
| 108 |
* A JSON schema file may be imported with a statement like: |
| 109 |
|
| 110 |
`import schema "foo.avsc";` |
| 111 |
|
| 112 |
Messages and types in the imported file are added to this file's protocol. |
| 113 |
|
| 114 |
Imported file names are resolved relative to the current IDL file. |
| 115 |
|
| 116 |
## Defining an Enumeration |
| 117 |
Enums are defined in Avro IDL using a syntax similar to C or Java. An Avro Enum supports optional default values. In the case that a reader schema is unable to recognize a symbol written by the writer, the reader will fall back to using the defined default value. This default is only used when an incompatible symbol is read. It is not used if the enum field is missing. |
| 118 |
|
| 119 |
Example Writer Enum Definition |
| 120 |
```java |
| 121 |
enum Shapes { |
| 122 |
SQUARE, TRIANGLE, CIRCLE, OVAL |
| 123 |
} |
| 124 |
``` |
| 125 |
Example Reader Enum Definition |
| 126 |
```java |
| 127 |
enum Shapes { |
| 128 |
SQUARE, TRIANGLE, CIRCLE |
| 129 |
} = CIRCLE; |
| 130 |
``` |
| 131 |
In the above example, the reader will use the default value of `CIRCLE` whenever reading data written with the `OVAL` symbol of the writer. Also note that, unlike the JSON format, anonymous enums cannot be defined. |
| 132 |
|
| 133 |
## Defining a Fixed Length Field |
| 134 |
Fixed fields are defined using the following syntax: |
| 135 |
``` |
| 136 |
fixed MD5(16); |
| 137 |
``` |
| 138 |
This example defines a fixed-length type called MD5 which contains 16 bytes. |
| 139 |
|
| 140 |
## Defining Records and Errors |
| 141 |
Records are defined in Avro IDL using a syntax similar to a struct definition in C: |
| 142 |
```java |
| 143 |
record Employee { |
| 144 |
string name; |
| 145 |
boolean active = true; |
| 146 |
long salary; |
| 147 |
} |
| 148 |
``` |
| 149 |
The above example defines a record with the name “Employee” with three fields. |
| 150 |
|
| 151 |
To define an error, simply use the keyword _error_ instead of _record_. For example: |
| 152 |
```java |
| 153 |
error Kaboom { |
| 154 |
string explanation; |
| 155 |
int result_code = -1; |
| 156 |
} |
| 157 |
``` |
| 158 |
Each field in a record or error consists of a type and a name, optional property annotations and an optional default value. |
| 159 |
|
| 160 |
A type reference in Avro IDL must be one of: |
| 161 |
|
| 162 |
* A primitive type |
| 163 |
* A logical type |
| 164 |
* A named schema defined prior to this usage in the same Protocol |
| 165 |
* A complex type (array, map, or union) |
| 166 |
|
| 167 |
### Primitive Types |
| 168 |
The primitive types supported by Avro IDL are the same as those supported by Avro's JSON format. This list includes _int_, _long_, _string_, _boolean_, _float_, _double_, _null_, and _bytes_. |
| 169 |
|
| 170 |
### Logical Types |
| 171 |
Some of the logical types supported by Avro's JSON format are also supported by Avro IDL. The currently supported types are: |
| 172 |
|
| 173 |
* _decimal_ (logical type [decimal]({{< relref "../specification#decimal" >}})) |
| 174 |
* _date_ (logical type [date]({{< relref "../specification#date" >}})) |
| 175 |
* _time_ms_ (logical type [time-millis]({{< relref "../specification#time-millisecond-precision" >}})) |
| 176 |
* _timestamp_ms_ (logical type [timestamp-millis]({{< relref "../specification#timestamp-millisecond-precision" >}})) |
| 177 |
|
| 178 |
For example: |
| 179 |
```java |
| 180 |
record Job { |
| 181 |
string jobid; |
| 182 |
date submitDate; |
| 183 |
time_ms submitTime; |
| 184 |
timestamp_ms finishTime; |
| 185 |
decimal(9,2) finishRatio; |
| 186 |
} |
| 187 |
``` |
| 188 |
|
| 189 |
Logical types can also be specified via an annotation, which is useful for logical types for which a keyword does not exist: |
| 190 |
|
| 191 |
```java |
| 192 |
record Job { |
| 193 |
string jobid; |
| 194 |
@logicalType("timestamp-micros") |
| 195 |
long finishTime; |
| 196 |
} |
| 197 |
``` |
| 198 |
|
| 199 |
### References to Named Schemata |
| 200 |
If a named schema has already been defined in the same Avro IDL file, it may be referenced by name as if it were a primitive type: |
| 201 |
```java |
| 202 |
record Card { |
| 203 |
Suit suit; // refers to the enum Card defined above |
| 204 |
int number; |
| 205 |
} |
| 206 |
``` |
| 207 |
|
| 208 |
### Default Values |
| 209 |
Default values for fields may be optionally specified by using an equals sign after the field name followed by a JSON expression indicating the default value. This JSON is interpreted as described in the [spec]({{< relref "../specification#schema-record" >}}). |
| 210 |
|
| 211 |
### Complex Types |
| 212 |
|
| 213 |
#### Arrays |
| 214 |
Array types are written in a manner that will seem familiar to C++ or Java programmers. An array of any type t is denoted `array<t>`. For example, an array of strings is denoted `array<string>`, and a multidimensional array of Foo records would be `array<array<Foo>>`. |
| 215 |
|
| 216 |
#### Maps |
| 217 |
Map types are written similarly to array types. An array that contains values of type t is written `map<t>`. As in the JSON schema format, all maps contain `string`-type keys. |
| 218 |
|
| 219 |
#### Unions |
| 220 |
Union types are denoted as `union { typeA, typeB, typeC, ... }`. For example, this record contains a string field that is optional (unioned with null), and a field containing either a precise or a imprecise number: |
| 221 |
```java |
| 222 |
record RecordWithUnion { |
| 223 |
union { null, string } optionalString; |
| 224 |
union { decimal(12, 6), float } number; |
| 225 |
} |
| 226 |
``` |
| 227 |
Note that the same restrictions apply to Avro IDL unions as apply to unions defined in the JSON format; namely, a record may not contain multiple elements of the same type. Also, fields/parameters that use the union type and have a default parameter must specify a default value of the same type as the **first** union type. |
| 228 |
|
| 229 |
Because it occurs so often, there is a special shorthand to denote a union of `null` with another type. In the following snippet, the first three fields have identical types: |
| 230 |
|
| 231 |
```java |
| 232 |
record RecordWithUnion { |
| 233 |
union { null, string } optionalString1 = null; |
| 234 |
string? optionalString2 = null; |
| 235 |
string? optionalString3; // No default value |
| 236 |
string? optionalString4 = "something"; |
| 237 |
} |
| 238 |
``` |
| 239 |
|
| 240 |
Note that unlike explicit unions, the position of the `null` type is fluid; it will be the first or last type depending on the default value (if any). So in the example above, all fields are valid. |
| 241 |
|
| 242 |
## Defining RPC Messages |
| 243 |
The syntax to define an RPC message within a Avro IDL protocol is similar to the syntax for a method declaration within a C header file or a Java interface. To define an RPC message add which takes two arguments named _foo_ and _bar_, returning an _int_, simply include the following definition within the protocol: |
| 244 |
```java |
| 245 |
int add(int foo, int bar = 0); |
| 246 |
``` |
| 247 |
Message arguments, like record fields, may specify default values. |
| 248 |
|
| 249 |
To define a message with no response, you may use the alias _void_, equivalent to the Avro _null_ type: |
| 250 |
```java |
| 251 |
void logMessage(string message); |
| 252 |
``` |
| 253 |
If you have previously defined an error type within the same protocol, you may declare that a message can throw this error using the syntax: |
| 254 |
```java |
| 255 |
void goKaboom() throws Kaboom; |
| 256 |
``` |
| 257 |
To define a one-way message, use the keyword `oneway` after the parameter list, for example: |
| 258 |
```java |
| 259 |
void fireAndForget(string message) oneway; |
| 260 |
``` |
| 261 |
|
| 262 |
## Other Language Features |
| 263 |
|
| 264 |
### Comments |
| 265 |
All Java-style comments are supported within a Avro IDL file. Any text following _//_ on a line is ignored, as is any text between _/*_ and _*/_, possibly spanning multiple lines. |
| 266 |
|
| 267 |
Comments that begin with _/**_ are used as the documentation string for the type or field definition that follows the comment. |
| 268 |
|
| 269 |
### Escaping Identifiers |
| 270 |
Occasionally, one will need to use a reserved language keyword as an identifier. In order to do so, backticks (`) may be used to escape the identifier. For example, to define a message with the literal name error, you may write: |
| 271 |
```java |
| 272 |
void `error`(); |
| 273 |
``` |
| 274 |
This syntax is allowed anywhere an identifier is expected. |
| 275 |
|
| 276 |
### Annotations for Ordering and Namespaces |
| 277 |
Java-style annotations may be used to add additional properties to types and fields throughout Avro IDL. |
| 278 |
|
| 279 |
For example, to specify the sort order of a field within a record, one may use the `@order` annotation before the field name as follows: |
| 280 |
```java |
| 281 |
record MyRecord { |
| 282 |
string @order("ascending") myAscendingSortField; |
| 283 |
string @order("descending") myDescendingField; |
| 284 |
string @order("ignore") myIgnoredField; |
| 285 |
} |
| 286 |
``` |
| 287 |
A field's type (with the exception of type references) may also be preceded by annotations, e.g.: |
| 288 |
```java |
| 289 |
record MyRecord { |
| 290 |
@java-class("java.util.ArrayList") array<string> myStrings; |
| 291 |
} |
| 292 |
``` |
| 293 |
This can be used to support java classes that can be serialized/deserialized via their `toString`/`String constructor`, e.g.: |
| 294 |
```java |
| 295 |
record MyRecord { |
| 296 |
@java-class("java.math.BigDecimal") string value; |
| 297 |
@java-key-class("java.io.File") map<string> fileStates; |
| 298 |
array<@java-class("java.math.BigDecimal") string> weights; |
| 299 |
} |
| 300 |
``` |
| 301 |
Similarly, a `@namespace` annotation may be used to modify the namespace when defining a named schema. For example: |
| 302 |
```java |
| 303 |
@namespace("org.apache.avro.firstNamespace") |
| 304 |
protocol MyProto { |
| 305 |
@namespace("org.apache.avro.someOtherNamespace") |
| 306 |
record Foo {} |
| 307 |
|
| 308 |
record Bar {} |
| 309 |
} |
| 310 |
``` |
| 311 |
will define a protocol in the _firstNamespace_ namespace. The record _Foo_ will be defined in _someOtherNamespace_ and _Bar_ will be defined in _firstNamespace_ as it inherits its default from its container. |
| 312 |
|
| 313 |
Type and field aliases are specified with the `@aliases` annotation as follows: |
| 314 |
```java |
| 315 |
@aliases(["org.old.OldRecord", "org.ancient.AncientRecord"]) |
| 316 |
record MyRecord { |
| 317 |
string @aliases(["oldField", "ancientField"]) myNewField; |
| 318 |
} |
| 319 |
``` |
| 320 |
Some annotations like those listed above are handled specially. All other annotations are added as properties to the protocol, message, schema or field. |
| 321 |
|
| 322 |
## Complete Example |
| 323 |
The following is an example of an Avro IDL file that shows most of the above features: |
| 324 |
```java |
| 325 |
/* |
| 326 |
* Header with license information. |
| 327 |
*/ |
| 328 |
|
| 329 |
/** |
| 330 |
* An example protocol in Avro IDL |
| 331 |
*/ |
| 332 |
@namespace("org.apache.avro.test") |
| 333 |
protocol Simple { |
| 334 |
/** Documentation for the enum type Kind */ |
| 335 |
@aliases(["org.foo.KindOf"]) |
| 336 |
enum Kind { |
| 337 |
FOO, |
| 338 |
BAR, // the bar enum value |
| 339 |
BAZ |
| 340 |
} = FOO; // For schema evolution purposes, unmatched values do not throw an error, but are resolved to FOO. |
| 341 |
|
| 342 |
/** MD5 hash; good enough to avoid most collisions, and smaller than (for example) SHA256. */ |
| 343 |
fixed MD5(16); |
| 344 |
|
| 345 |
record TestRecord { |
| 346 |
/** Record name; has no intrinsic order */ |
| 347 |
string @order("ignore") name; |
| 348 |
|
| 349 |
Kind @order("descending") kind; |
| 350 |
|
| 351 |
MD5 hash; |
| 352 |
|
| 353 |
/* |
| 354 |
Note that 'null' is the first union type. Just like .avsc / .avpr files, the default value must be of the first union type. |
| 355 |
*/ |
| 356 |
union { null, MD5 } /** Optional field */ @aliases(["hash"]) nullableHash = null; |
| 357 |
|
| 358 |
array<long> arrayOfLongs; |
| 359 |
} |
| 360 |
|
| 361 |
/** Errors are records that can be thrown from a method */ |
| 362 |
error TestError { |
| 363 |
string message; |
| 364 |
} |
| 365 |
|
| 366 |
string hello(string greeting); |
| 367 |
/** Return what was given. Demonstrates the use of backticks to name types/fields/messages/parameters after keywords */ |
| 368 |
TestRecord echo(TestRecord `record`); |
| 369 |
int add(int arg1, int arg2); |
| 370 |
bytes echoBytes(bytes data); |
| 371 |
void `error`() throws TestError; |
| 372 |
// The oneway keyword forces the method to return null. |
| 373 |
void ping() oneway; |
| 374 |
} |
| 375 |
``` |
| 376 |
Additional examples may be found in the Avro source tree under the `src/test/idl/input` directory. |
| 377 |
|
| 378 |
## IDE support |
| 379 |
|
| 380 |
There are several editors and IDEs that support Avro IDL files, usually via plugins. |
| 381 |
|
| 382 |
### JetBrains |
| 383 |
|
| 384 |
Apache Avro IDL Schema Support 203.1.2 was released in 9 December 2021. |
| 385 |
|
| 386 |
Features: |
| 387 |
* Syntax Highlighting |
| 388 |
* Code Completion |
| 389 |
* Code Formatting |
| 390 |
* Error Highlighting |
| 391 |
* Inspections & quick fixes |
| 392 |
* JSON schemas for .avpr and .avsc files |
| 393 |
|
| 394 |
It's available via the [JetBrains Marketplace](https://plugins.jetbrains.com/plugin/15728-apache-avro-idl-schema-support) |
| 395 |
and on [GitHub](https://github.com/opwvhk/avro-schema-support). |
| 396 |
|
| 397 |
The plugin supports almost the all JetBrains products: IntelliJ IDEA, PyCharm, WebStorm, Android Studio, AppCode, GoLand, Rider, CLion, RubyMine, PhpStorm, DataGrip, DataSpell, MPS, Code With Me Guest and JetBrains Client. |
| 398 |
|
| 399 |
Only JetBrains Gateway does not support this plugin directly. But the backend (JetBrains) IDE that it connects to does. |
| 400 |
|
| 401 |
### Eclipse |
| 402 |
|
| 403 |
Avroclipse 0.0.11 was released on 4 December 2019. |
| 404 |
|
| 405 |
Features: |
| 406 |
* Syntax Highlighting |
| 407 |
* Error Highlighting |
| 408 |
* Code Completion |
| 409 |
|
| 410 |
It is available on the [Eclipse Marketplace](https://marketplace.eclipse.org/content/avroclipse) |
| 411 |
and [GitHub](https://github.com/dvdkruk/avroclipse). |
| 412 |
|
| 413 |
### Visual Studio Code |
| 414 |
|
| 415 |
avro-idl 0.5.0 was released on 16 June 2021. It provides syntax highlighting. |
| 416 |
|
| 417 |
It is available on the [VisualStudio Marketplace](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.avro) |
| 418 |
and [GitHub](https://github.com/Jason3S/vscode-avro-ext) |
| 419 |
|
| 420 |
### Atom.io |
| 421 |
|
| 422 |
atom-language-avro 0.0.13 was released on 14 August 2015. It provides syntax highlighting. |
| 423 |
|
| 424 |
It is available as [Atom.io package](https://atom.io/packages/atom-language-avro) |
| 425 |
and [GitHub](https://github.com/jonesetc/atom-language-avro) |
| 426 |
|
| 427 |
### Vim |
| 428 |
|
| 429 |
A `.avdl` detecting plugin by Gurpreet Atwal on [GitHub](https://github.com/gurpreetatwal/vim-avro) (Last change in December 2016) |
| 430 |
|
| 431 |
[avro-idl.vim](https://github.com/apache/avro/blob/master/share/editors/avro-idl.vim) in the Avro repository `share/editors` directory (last change in September 2010) |
| 432 |
|
| 433 |
Both provide syntax highlighting. |