public class SchemaBuilder extends java.lang.Object implements Schema
SchemaBuilder provides a fluent API for constructing Schema
objects. It allows you to set each of the
properties for the schema and each call returns the SchemaBuilder so the calls can be chained. When nested types
are required, use one of the predefined schemas from Schema
or use a second SchemaBuilder inline.
Here is an example of building a struct schema:
Schema dateSchema = SchemaBuilder.struct() .name("com.example.CalendarDate").version(2).doc("A calendar date including month, day, and year.") .field("month", Schema.STRING_SCHEMA) .field("day", Schema.INT8_SCHEMA) .field("year", Schema.INT16_SCHEMA) .build();
Here is an example of using a second SchemaBuilder to construct complex, nested types:
Schema userListSchema = SchemaBuilder.array( SchemaBuilder.struct().name("com.example.User").field("username", Schema.STRING_SCHEMA).field("id", Schema.INT64_SCHEMA).build() ).build();
Schema.Type
BOOLEAN_SCHEMA, BYTES_SCHEMA, FLOAT32_SCHEMA, FLOAT64_SCHEMA, INT16_SCHEMA, INT32_SCHEMA, INT64_SCHEMA, INT8_SCHEMA, OPTIONAL_BOOLEAN_SCHEMA, OPTIONAL_BYTES_SCHEMA, OPTIONAL_FLOAT32_SCHEMA, OPTIONAL_FLOAT64_SCHEMA, OPTIONAL_INT16_SCHEMA, OPTIONAL_INT32_SCHEMA, OPTIONAL_INT64_SCHEMA, OPTIONAL_INT8_SCHEMA, OPTIONAL_STRING_SCHEMA, STRING_SCHEMA
Constructor and Description |
---|
SchemaBuilder(Schema.Type type) |
Modifier and Type | Method and Description |
---|---|
static SchemaBuilder |
array(Schema valueSchema) |
static SchemaBuilder |
bool() |
Schema |
build()
Build the Schema using the current settings
|
static SchemaBuilder |
bytes() |
java.lang.Object |
defaultValue() |
SchemaBuilder |
defaultValue(java.lang.Object value)
Set the default value for this schema.
|
java.lang.String |
doc() |
SchemaBuilder |
doc(java.lang.String doc)
Set the documentation for this schema.
|
Field |
field(java.lang.String fieldName)
Get a field for this Schema by name.
|
SchemaBuilder |
field(java.lang.String fieldName,
Schema fieldSchema)
Add a field to this struct schema.
|
java.util.List<Field> |
fields()
Get the list of fields for this Schema.
|
static SchemaBuilder |
float32() |
static SchemaBuilder |
float64() |
static SchemaBuilder |
int16() |
static SchemaBuilder |
int32() |
static SchemaBuilder |
int64() |
static SchemaBuilder |
int8() |
boolean |
isOptional() |
Schema |
keySchema()
Get the key schema for this map schema.
|
static SchemaBuilder |
map(Schema keySchema,
Schema valueSchema) |
java.lang.String |
name() |
SchemaBuilder |
name(java.lang.String name)
Set the name of this schema.
|
SchemaBuilder |
optional()
Set this schema as optional.
|
SchemaBuilder |
parameter(java.lang.String propertyName,
java.lang.String propertyValue)
Set a schema parameter.
|
java.util.Map<java.lang.String,java.lang.String> |
parameters()
Get a map of schema parameters.
|
SchemaBuilder |
parameters(java.util.Map<java.lang.String,java.lang.String> props)
Set schema parameters.
|
SchemaBuilder |
required()
Set this schema as required.
|
Schema |
schema()
Return a concrete instance of the
Schema specified by this builder |
static SchemaBuilder |
string() |
static SchemaBuilder |
struct() |
Schema.Type |
type() |
static SchemaBuilder |
type(Schema.Type type)
Create a SchemaBuilder for the specified type.
|
Schema |
valueSchema()
Get the value schema for this map or array schema.
|
java.lang.Integer |
version()
Get the optional version of the schema.
|
SchemaBuilder |
version(java.lang.Integer version)
Set the version of this schema.
|
public SchemaBuilder(Schema.Type type)
public boolean isOptional()
isOptional
in interface Schema
public SchemaBuilder optional()
public SchemaBuilder required()
public java.lang.Object defaultValue()
defaultValue
in interface Schema
public SchemaBuilder defaultValue(java.lang.Object value)
SchemaBuilderException
if it does not match.value
- the default valuepublic java.lang.String name()
public SchemaBuilder name(java.lang.String name)
name
- the schema namepublic java.lang.Integer version()
Schema
public SchemaBuilder version(java.lang.Integer version)
version
- the schema versionpublic java.lang.String doc()
public SchemaBuilder doc(java.lang.String doc)
doc
- the documentationpublic java.util.Map<java.lang.String,java.lang.String> parameters()
Schema
parameters
in interface Schema
public SchemaBuilder parameter(java.lang.String propertyName, java.lang.String propertyValue)
propertyName
- name of the schema property to definepropertyValue
- value of the schema property to define, as a Stringpublic SchemaBuilder parameters(java.util.Map<java.lang.String,java.lang.String> props)
props
- Map of properties to setpublic Schema.Type type()
public static SchemaBuilder type(Schema.Type type)
string()
or struct()
, but this form
can be useful when generating schemas dynamically.type
- the schema typepublic static SchemaBuilder int8()
Schema.Type.INT8
SchemaBuilderpublic static SchemaBuilder int16()
Schema.Type.INT16
SchemaBuilderpublic static SchemaBuilder int32()
Schema.Type.INT32
SchemaBuilderpublic static SchemaBuilder int64()
Schema.Type.INT64
SchemaBuilderpublic static SchemaBuilder float32()
Schema.Type.FLOAT32
SchemaBuilderpublic static SchemaBuilder float64()
Schema.Type.FLOAT64
SchemaBuilderpublic static SchemaBuilder bool()
Schema.Type.BOOLEAN
SchemaBuilderpublic static SchemaBuilder string()
Schema.Type.STRING
SchemaBuilderpublic static SchemaBuilder bytes()
Schema.Type.BYTES
SchemaBuilderpublic static SchemaBuilder struct()
Schema.Type.STRUCT
SchemaBuilderpublic SchemaBuilder field(java.lang.String fieldName, Schema fieldSchema)
fieldName
- the name of the field to addfieldSchema
- the Schema for the field's valuepublic java.util.List<Field> fields()
public Field field(java.lang.String fieldName)
Schema
public static SchemaBuilder array(Schema valueSchema)
valueSchema
- the schema for elements of the arraySchema.Type.ARRAY
SchemaBuilderpublic static SchemaBuilder map(Schema keySchema, Schema valueSchema)
keySchema
- the schema for keys in the mapvalueSchema
- the schema for values in the mapSchema.Type.MAP
SchemaBuilderpublic Schema keySchema()
Schema
public Schema valueSchema()
Schema
valueSchema
in interface Schema