Top Description Inners Fields Constructors Methods
org.apache.avro

public Class SchemaBuilder

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.Buffer, .ByteBuffer, java.nio.charset.StandardCharsets, java.util.ArrayList, .Arrays, .Collections, .HashMap, .HashSet, .List, .Map, .Objects, .Set, com.fasterxml.jackson.core.io.JsonStringEncoder, org.apache.avro.Schema.Field, org.apache.avro.generic.GenericData, .GenericRecord, org.apache.avro.util.internal.JacksonUtils, com.fasterxml.jackson.databind.JsonNode, .ObjectMapper, com.fasterxml.jackson.databind.node.NullNode, .TextNode

A fluent interface for building Schema instances. The flow of the API is designed to mimic the Avro Schema Specification

For example, the below JSON schema and the fluent builder code to create it are very similar:
{
  "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 TypeBuilder, FieldTypeBuilder, or 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, 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()
  • Example use of a named type with all optional parameters:
    .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 with ArrayBuilder#items() and MapBuilder#values(), respectively.
    Fields
    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.

    Unions
    Union types are built via 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()
    
    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 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.

    Nested and Inner Type Summary

    Modifier and TypeClass and Description
    public static class
    SchemaBuilder.ArrayBuilder<R>

    Builds an Avro Array type with optional properties.

    private static class
    public static class
    SchemaBuilder.ArrayDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.BaseFieldTypeBuilder<R>

    A special Builder for Record fields.

    public static class
    SchemaBuilder.BaseTypeBuilder<R>

    A common API for building types within a context.

    public static class
    SchemaBuilder.BooleanBuilder<R>

    Builds an Avro boolean type with optional properties.

    public static class
    SchemaBuilder.BooleanDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.BytesBuilder<R>

    Builds an Avro bytes type with optional properties.

    public static class
    SchemaBuilder.BytesDefault<R>

    Choose whether to use a default value for the field or not.

    private abstract static class
    SchemaBuilder.Completion<R>

    Completion<r> is for internal builder use, all subclasses are private.

    private abstract static class
    public static class
    SchemaBuilder.DoubleBuilder<R>

    Builds an Avro double type with optional properties.

    public static class
    SchemaBuilder.DoubleDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.EnumBuilder<R>

    Builds an Avro Enum type with optional properties, namespace, doc, and aliases.

    public static class
    SchemaBuilder.EnumDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    public static class
    SchemaBuilder.FieldBuilder<R>

    Builds a Field in the context of a FieldAssembler.

    public abstract static class
    SchemaBuilder.FieldDefault<R, S extends SchemaBuilder.FieldDefault<R, S>>

    Abstract base class for field defaults.

    public static class
    SchemaBuilder.FieldTypeBuilder<R>

    FieldTypeBuilder adds unionOf(), nullable(), and optional() to BaseFieldTypeBuilder.

    public static class
    SchemaBuilder.FixedBuilder<R>

    Builds an Avro Fixed type with optional properties, namespace, doc, and aliases.

    public static class
    SchemaBuilder.FixedDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.FloatBuilder<R>

    Builds an Avro float type with optional properties.

    public static class
    SchemaBuilder.FloatDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    public static class
    SchemaBuilder.IntBuilder<R>

    Builds an Avro int type with optional properties.

    public static class
    SchemaBuilder.IntDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.LongBuilder<R>

    Builds an Avro long type with optional properties.

    public static class
    SchemaBuilder.LongDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.MapBuilder<R>

    Builds an Avro Map type with optional properties.

    private static class
    public static class
    SchemaBuilder.MapDefault<R>

    Choose whether to use a default value for the field or not.

    private static class
    SchemaBuilder.NameContext

    internal class for passing the naming context around.

    public abstract static class
    SchemaBuilder.NamedBuilder<S extends SchemaBuilder.NamedBuilder<S>>

    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.NamespacedBuilder<R, S>>

    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
    SchemaBuilder.NullBuilder<R>

    Builds an Avro null type with optional properties.

    public static class
    SchemaBuilder.NullDefault<R>

    Choose whether to use a default value for the field or not.

    private static class
    private abstract static class
    SchemaBuilder.PrimitiveBuilder<R, P extends SchemaBuilder.PrimitiveBuilder<R, P>>

    An abstraction for sharing code amongst all primitive type builders.

    public abstract static class
    SchemaBuilder.PropBuilder<S extends SchemaBuilder.PropBuilder<S>>

    An abstract builder for all Avro types.

    public static class
    public static class
    SchemaBuilder.RecordDefault<R>

    Choose whether to use a default value for the field or not.

    private static class
    public static class
    SchemaBuilder.StringBldr<R>

    Builds an Avro string type with optional properties.

    public static class
    SchemaBuilder.StringDefault<R>

    Choose whether to use a default value for the field or not.

    public static class
    SchemaBuilder.TypeBuilder<R>

    A Builder for creating any Avro schema type.

    public static class
    SchemaBuilder.UnionAccumulator<R>

    Accumulates all of the types in a union.

    private static class
    SchemaBuilder.UnionBuilder<R>

    A special builder for unions.

    private static class
    public static class
    SchemaBuilder.UnionFieldTypeBuilder<R>

    Builder for a union field.

    Field Summary

    Modifier and TypeField and Description
    private static final Schema

    Constructor Summary

    AccessConstructor and Description
    private

    Method Summary

    Modifier and TypeMethod and Description
    public static SchemaBuilder.ArrayBuilder<Schema>
    array()

    Create a builder for an Avro array This is equivalent to:

    builder().array();
    
    public static SchemaBuilder.TypeBuilder<Schema>
    builder()

    Create a builder for Avro schemas.

    public static SchemaBuilder.TypeBuilder<Schema>
    builder(String namespace)

    Create a builder for Avro schemas with a default namespace.

    public static SchemaBuilder.EnumBuilder<Schema>
    enumeration(String
    the enum name
    name
    )

    Create a builder for an Avro enum with the specified name and symbols (values).

    public static SchemaBuilder.FixedBuilder<Schema>
    fixed(String
    the fixed name
    name
    )

    Create a builder for an Avro fixed type with the specified name and size.

    public static SchemaBuilder.MapBuilder<Schema>
    map()

    Create a builder for an Avro map This is equivalent to:

    builder().map();
    
    public static SchemaBuilder.BaseTypeBuilder<Schema>
    nullable()

    Create a builder for a union of a type and null.

    public static SchemaBuilder.RecordBuilder<Schema>
    record(String
    the record name
    name
    )

    Create a builder for an Avro record with the specified name.

    private static JsonNode
    public static SchemaBuilder.BaseTypeBuilder<SchemaBuilder.UnionAccumulator<Schema>>
    unionOf()

    Create a builder for an Avro union This is equivalent to:

    builder().unionOf();
    
    Inherited from java.lang.Object:
    cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait