Package org.apache.avro
Class SchemaBuilder
java.lang.Object
org.apache.avro.SchemaBuilder
A fluent interface for building Schema
instances. The flow of the API
is designed to mimic the
Avro Schema
Specification
{ "type": "record", "name": "HandshakeRequest", "namespace":"org.apache.avro.ipc", "fields": [ {"name": "clientHash", "type": {"type": "fixed", "name": "MD5", "size": 16}}, {"name": "clientProtocol", "type": ["null", "string"]}, {"name": "serverHash", "type": "MD5"}, {"name": "meta", "type": ["null", {"type": "map", "values": "bytes"}]} ] }
Schema schema = SchemaBuilder.record("HandshakeRequest").namespace("org.apache.avro.ipc").fields().name("clientHash") .type().fixed("MD5").size(16).noDefault().name("clientProtocol").type().nullable().stringType().noDefault() .name("serverHash").type("MD5").noDefault().name("meta").type().nullable().map().values().bytesType().noDefault() .endRecord();
Usage Guide
SchemaBuilder chains together many smaller builders and maintains nested context in order to mimic the Avro Schema specification. Every Avro type in JSON has required and optional JSON properties, as well as user-defined properties.Selecting and Building an Avro Type
The API analogy for the right hand side of the Avro Schema JSON"type":is a
SchemaBuilder.TypeBuilder
, SchemaBuilder.FieldTypeBuilder
, or
SchemaBuilder.UnionFieldTypeBuilder
, depending on the context. These types all
share a similar API for selecting and building types.
Primitive Types
All Avro primitive types are trivial to configure. A primitive type in Avro JSON can be declared two ways, one that supports custom properties and one that does not:{"type":"int"} {"type":{"name":"int"}} {"type":{"name":"int", "customProp":"val"}}The analogous code form for the above three JSON lines are the below three lines:
.intType() .intBuilder().endInt() .intBuilder().prop("customProp", "val").endInt()Every primitive type has a shortcut to create the trivial type, and a builder when custom properties are required. The first line above is a shortcut for the second, analogous to the JSON case.
Named Types
Avro named types have names, namespace, aliases, and doc. In this API these share a common parent,SchemaBuilder.NamespacedBuilder
. The
builders for named types require a name to be constructed, and optional
configuration via:
SchemaBuilder.NamedBuilder.doc()
SchemaBuilder.NamespacedBuilder.namespace(String)
SchemaBuilder.NamedBuilder.aliases(String...)
SchemaBuilder.PropBuilder.prop(String, String)
SchemaBuilder.FixedBuilder.size(int)
SchemaBuilder.EnumBuilder.symbols(String...)
SchemaBuilder.RecordBuilder.fields()
.enumeration("Suit").namespace("org.apache.test") .aliases("org.apache.test.OldSuit") .doc("CardSuits") .prop("customProp", "val") .symbols("SPADES", "HEARTS", "DIAMONDS", "CLUBS")Which is equivalent to the JSON:
{ "type":"enum", "name":"Suit", "namespace":"org.apache.test", "aliases":["org.apache.test.OldSuit"], "doc":"Card Suits", "customProp":"val", "symbols":["SPADES", "HEARTS", "DIAMONDS", "CLUBS"] }
Nested Types
The Avro nested types, map and array, can have custom properties like all avro types, are not named, and must specify a nested type. After configuration of optional properties, an array or map builds or selects its nested type withSchemaBuilder.ArrayBuilder.items()
and
SchemaBuilder.MapBuilder.values()
, respectively.
Fields
SchemaBuilder.RecordBuilder.fields()
returns a
SchemaBuilder.FieldAssembler
for defining the fields of the record and completing
it. Each field must have a name, specified via
SchemaBuilder.FieldAssembler.name(String)
, which returns a SchemaBuilder.FieldBuilder
for
defining aliases, custom properties, and documentation of the field. After
configuring these optional values for a field, the type is selected or built
with SchemaBuilder.FieldBuilder.type()
.
Fields have default values that must be specified to complete the field.
SchemaBuilder.FieldDefault.noDefault()
is available for all field types, and a
specific method is available for each type to use a default, for example
SchemaBuilder.IntDefault.intDefault(int)
There are field shortcut methods on SchemaBuilder.FieldAssembler
for primitive
types. These shortcuts create required, optional, and nullable fields, but do
not support field aliases, doc, or custom properties.
Unions
Union types are built viaSchemaBuilder.TypeBuilder.unionOf()
or
SchemaBuilder.FieldTypeBuilder.unionOf()
in the context of type selection. This
chains together multiple types, in union order. For example:
.unionOf() .fixed("IPv4").size(4).and() .fixed("IPv6").size(16).and() .nullType().endUnion()is equivalent to the Avro schema JSON:
[ {"type":"fixed", "name":"IPv4", "size":4}, {"type":"fixed", "name":"IPv6", "size":16}, "null" ]In a field context, the first type of a union defines what default type is allowed. Unions have two shortcuts for common cases. nullable() creates a union of a type and null. In a field type context, optional() is available and creates a union of null and a type, with a null default. The below two are equivalent:
.unionOf().intType().and().nullType().endUnion() .nullable().intType()The below two field declarations are equivalent:
.name("f").type().unionOf().nullType().and().longType().endUnion().nullDefault() .name("f").type().optional().longType()
Explicit Types and Types by Name
Types can also be specified explicitly by passing in a Schema, or by name:.type(Schema.create(Schema.Type.INT)) // explicitly specified .type("MD5") // reference by full name or short name .type("MD5", "org.apache.avro.test") // reference by name and namespaceWhen a type is specified by name, and the namespace is absent or null, the namespace is inherited from the enclosing context. A namespace will propagate as a default to child fields, nested types, or later defined types in a union. To specify a name that has no namespace and ignore the inherited namespace, set the namespace to "".
builder(String)
returns a type builder with a default
namespace. builder()
returns a type builder with no
default namespace.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Builds an Avro Array type with optional properties.static class
Choose whether to use a default value for the field or not.static class
A special Builder for Record fields.static class
A common API for building types within a context.static final class
Builds an Avro boolean type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro bytes type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro double type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro Enum type with optional properties, namespace, doc, and aliases.static class
Choose whether to use a default value for the field or not.static final class
static final class
Builds a Field in the context of aSchemaBuilder.FieldAssembler
.static class
SchemaBuilder.FieldDefault<R,
S extends SchemaBuilder.FieldDefault<R, S>> Abstract base class for field defaults.static final class
FieldTypeBuilder addsSchemaBuilder.FieldTypeBuilder.unionOf()
,SchemaBuilder.FieldTypeBuilder.nullable()
, andSchemaBuilder.FieldTypeBuilder.optional()
to BaseFieldTypeBuilder.static final class
Builds an Avro Fixed type with optional properties, namespace, doc, and aliases.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro float type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
static final class
Builds an Avro int type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro long type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro Map type with optional properties.static class
Choose whether to use a default value for the field or not.static class
An abstract type that provides builder methods for configuring the name, doc, and aliases of all Avro types that have names (fields, Fixed, Record, and Enum).static class
An abstract type that provides builder methods for configuring the namespace for all Avro types that have namespaces (Fixed, Record, and Enum).static final class
Builds an Avro null type with optional properties.static class
Choose whether to use a default value for the field or not.static class
SchemaBuilder.PropBuilder<S extends SchemaBuilder.PropBuilder<S>>
An abstract builder for all Avro types.static final class
static class
Choose whether to use a default value for the field or not.static final class
Builds an Avro string type with optional properties.static class
Choose whether to use a default value for the field or not.static final class
A Builder for creating any Avro schema type.static final class
Accumulates all of the types in a union.static final class
Builder for a union field. -
Method Summary
Modifier and TypeMethodDescriptionstatic SchemaBuilder.ArrayBuilder
<Schema> array()
Create a builder for an Avro array This is equivalent to:static SchemaBuilder.TypeBuilder
<Schema> builder()
Create a builder for Avro schemas.static SchemaBuilder.TypeBuilder
<Schema> Create a builder for Avro schemas with a default namespace.static SchemaBuilder.EnumBuilder
<Schema> enumeration
(String name) Create a builder for an Avro enum with the specified name and symbols (values).static SchemaBuilder.FixedBuilder
<Schema> Create a builder for an Avro fixed type with the specified name and size.static SchemaBuilder.MapBuilder
<Schema> map()
Create a builder for an Avro map This is equivalent to:static SchemaBuilder.BaseTypeBuilder
<Schema> nullable()
Create a builder for a union of a type and null.static SchemaBuilder.RecordBuilder
<Schema> Create a builder for an Avro record with the specified name.unionOf()
Create a builder for an Avro union This is equivalent to:
-
Method Details
-
builder
Create a builder for Avro schemas. -
builder
Create a builder for Avro schemas with a default namespace. Types created without namespaces will inherit the namespace provided. -
record
Create a builder for an Avro record with the specified name. This is equivalent to:builder().record(name);
- Parameters:
name
- the record name
-
enumeration
Create a builder for an Avro enum with the specified name and symbols (values). This is equivalent to:builder().enumeration(name);
- Parameters:
name
- the enum name
-
fixed
Create a builder for an Avro fixed type with the specified name and size. This is equivalent to:builder().fixed(name);
- Parameters:
name
- the fixed name
-
array
Create a builder for an Avro array This is equivalent to:builder().array();
-
map
Create a builder for an Avro map This is equivalent to:builder().map();
-
unionOf
Create a builder for an Avro union This is equivalent to:builder().unionOf();
-
nullable
Create a builder for a union of a type and null. This is a shortcut for:builder().nullable();
and the following two lines are equivalent:nullable().intType();
unionOf().intType().and().nullType().endUnion();
-