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();
"type":is a
TypeBuilder, FieldTypeBuilder, or
UnionFieldTypeBuilder, depending on the context. These types all
share a similar API for selecting and building types.
{"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.
NamespacedBuilder. The
builders for named types require a name to be constructed, and optional
configuration via:
NamespacedBuilder#doc()NamespacedBuilder#namespace(String)NamespacedBuilder#aliases(String...) PropBuilder#prop(String, String)Each named type completes configuration of the optional properties with its own method:
FixedBuilder#size(int)EnumBuilder#symbols(String...) 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"]
}
ArrayBuilder#items() and
MapBuilder#values(), respectively.
RecordBuilder#fields() returns a
FieldAssembler for defining the fields of the record and completing
it. Each field must have a name, specified via
FieldAssembler#name(String), which returns a 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 FieldBuilder#type().
Fields have default values that must be specified to complete the field.
FieldDefault#noDefault() is available for all field types, and a
specific method is available for each type to use a default, for example
IntDefault#intDefault(int)
There are field shortcut methods on FieldAssembler for primitive
types. These shortcuts create required, optional, and nullable fields, but do
not support field aliases, doc, or custom properties.
TypeBuilder#unionOf() or
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()
.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 namespace
When 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 "".
SchemaBuilder#builder(String) returns a type builder with a default
namespace. SchemaBuilder#builder() returns a type builder with no
default namespace.
| Modifier and Type | Class and Description |
|---|---|
| public static class | |
| private static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| private abstract static class | |
| private abstract static class | |
| public static class | |
| public static class | |
| public static class | SchemaBuilder.EnumBuilder<R>
Builds an Avro Enum type with optional properties, namespace, doc, and aliases. |
| public static class | |
| public static class | |
| public static class | |
| public abstract static class | SchemaBuilder.FieldDefault<R, S extends SchemaBuilder.
Abstract base class for field defaults. |
| public static class | SchemaBuilder.FieldTypeBuilder<R>
FieldTypeBuilder adds |
| public static class | SchemaBuilder.FixedBuilder<R>
Builds an Avro Fixed type with optional properties, namespace, doc, and aliases. |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| private static class | |
| public static class | |
| private static class | SchemaBuilder.NameContext
internal class for passing the naming context around. |
| public abstract static class | SchemaBuilder.NamedBuilder<S extends SchemaBuilder.
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). |
| public abstract static class | SchemaBuilder.NamespacedBuilder<R, S extends SchemaBuilder.
An abstract type that provides builder methods for configuring the namespace for all Avro types that have namespaces (Fixed, Record, and Enum). |
| private abstract static class | |
| private static class | |
| private static class | |
| public static class | |
| public static class | |
| private static class | |
| private abstract static class | SchemaBuilder.PrimitiveBuilder<R, P extends SchemaBuilder.
An abstraction for sharing code amongst all primitive type builders. |
| public abstract static class | SchemaBuilder.PropBuilder<S extends SchemaBuilder.
An abstract builder for all Avro types. |
| public static class | |
| public static class | |
| private static class | |
| public static class | |
| public static class | |
| public static class | |
| public static class | |
| private static class | |
| private static class | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final Schema |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static SchemaBuilder. | |
| public static SchemaBuilder. | |
| public static SchemaBuilder. | |
| public static SchemaBuilder. | enumeration(String
the enum name name)Create a builder for an Avro enum with the specified name and symbols (values). |
| public static SchemaBuilder. | |
| public static SchemaBuilder. | |
| public static SchemaBuilder. | |
| public static SchemaBuilder. | |
| private static JsonNode | |
| public static SchemaBuilder. |