Avro Schema

www.altova.com Expand/Collapse All Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > Avro, Avro Schema >

Avro Schema

An Avro schema specifies the structure of an Avro data block. It specifies what data fields are expected and how the values are represented. Information about Avro schema and its specification is available here.

 

Note the following points about Avro schemas:

 

An  Avro schema is created in JSON format
An Avro schema can be: a JSON string, a JSON object, or a JSON array
An Avro schema can contain four attributes: name, namespace, type, and fields
There are eight primitive data types: null, boolean, int, long, float, double, bytes, and string
There are six complex types: records, enums, arrays, maps, unions, and fixed
Primitive types have no attributes; each complex type has its own set of attributes

 

For details and more information about Avro schema, see the Avro schema specification.

 

Examples

Given below are simple examples of Avro schemas, each with corresponding Avro data snippets in JSON format. Note that the schema defines a certain structure. In some cases, when the defined structure is instantiated multiple times, the resulting output might not be valid JSON. For example, a schema might define the structure of a JSON object. If the JSON object is instantiated multiple times, each object (separately) could be valid against the Avro schema, but the entire document would not be valid JSON—because there is no container object. If valid JSON is required, you might want to rewrite the Avro schema to validate an array of JSON objects. Compare Examples 4 and 5 below to see this point illustrated.

 

 

Click to exppand/collapse01: Avro schema as JSON string

This schema is a single string, and it specifies that the data block must contain a value that is of the Avro (int) primitive data type: "int"

 

Valid Avro: 2016

Invalid Avro: "2016"

 

 

Click to exppand/collapse02: Avro schema as JSON object

This schema specifies exactly the same thing as the previous schema, but it is a JSON object. The data block must contain one item that is a value of the Avro (int) primitive data type:

{

   "type": "int"

}

 

Valid Avro: 2016

Invalid Avro: "2016"

 

 

Click to exppand/collapse03: Avro schema as JSON object: Array of integers

This schema is a JSON object that specifies an array of integers:

{

   "type": "array",

   "items": "int"

}

 

Valid Avro: [2016, 2017]

Valid Avro: [2016]

Valid Avro: [2016]

Invalid Avro: 2016, 2017

 

 

Click to exppand/collapse04: Avro schema as JSON object: Records

This schema is a JSON object that specifies a single record:

{

   "type": "record",

   "name": "ages",

   "fields" : [

   {"name": "name", "type": "string"},

   {"name": "age", "type": "int"}

   ]

}

 

Valid Avro: {"name":"John", "age":35}

 

 

 

Click to exppand/collapse05: Avro schema as JSON object: Multiple records

This schema is a JSON object that specifies an array of record items, each of which must be a JSON object:

{

   "type": "array",

   "items": {

   "type": "record",

   "name": "ages",

   "fields" : [

   {"name": "name", "type": "string"},

   {"name": "age", "type": "int"}

   ]

    }

}

 

Valid Avro: [{"name":"Mary", "age":34}, {"name":"John", "age":35}]

 

 

 

Avro schema file types

If you wish to use XMLSpy's features for Avro-related editing and validating, then XMLSpy must be able to recognize a file as an Avro schema. A file is recognized as an Avro schema if the file's extension is defined as such in XMLSpy's Options dialog (Tools | Options | File types). XMLSpy's default settings define one file extension—the .avsc extension—as being that of an Avro schema file. If you wish to create other file extensions that specify Avro schema documents, add these file extensions as Avro schema extensions to the list in the Options dialog.

 

Creating and editing Avro schemas

In XMLSpy, you can create a new file as an Avro schema by specifying an Avro schema file extension as its file type. XMLSpy provides intelligent editing help as you type. This includes context-sensitive keyword suggestions, automatic entry of bracket-, brace-, and quote-pairs, syntax coloring, and auto-completion of keywords. Additionally, there are three entry helpers: JSON Properties, JSON Values, and JSON Entities. The entries that are available in them are context-sensitive. Double-click an entry to insert it at the current cursor location. You can then validate the file against the Avro schema specification with the Validate | Validate XML (F8) menu command.

 


© 2019 Altova GmbH