Package org.apache.kafka.connect.data
Class SchemaBuilder
- java.lang.Object
-
- org.apache.kafka.connect.data.SchemaBuilder
-
- All Implemented Interfaces:
Schema
public class SchemaBuilder extends 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 fromSchema
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();
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.kafka.connect.data.Schema
Schema.Type
-
-
Field Summary
-
Fields inherited from interface org.apache.kafka.connect.data.Schema
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 Summary
Constructors Constructor Description SchemaBuilder(Schema.Type type)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SchemaBuilder
array(Schema valueSchema)
static SchemaBuilder
bool()
Schema
build()
Build the Schema using the current settingsstatic SchemaBuilder
bytes()
Object
defaultValue()
SchemaBuilder
defaultValue(Object value)
Set the default value for this schema.String
doc()
SchemaBuilder
doc(String doc)
Set the documentation for this schema.Field
field(String fieldName)
Get a field for this Schema by name.SchemaBuilder
field(String fieldName, Schema fieldSchema)
Add a field to this struct schema.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)
String
name()
SchemaBuilder
name(String name)
Set the name of this schema.SchemaBuilder
optional()
Set this schema as optional.SchemaBuilder
parameter(String propertyName, String propertyValue)
Set a schema parameter.Map<String,String>
parameters()
Get a map of schema parameters.SchemaBuilder
parameters(Map<String,String> props)
Set schema parameters.SchemaBuilder
required()
Set this schema as required.Schema
schema()
Return a concrete instance of theSchema
specified by this builderstatic 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.Integer
version()
Get the optional version of the schema.SchemaBuilder
version(Integer version)
Set the version of this schema.
-
-
-
Constructor Detail
-
SchemaBuilder
public SchemaBuilder(Schema.Type type)
-
-
Method Detail
-
isOptional
public boolean isOptional()
- Specified by:
isOptional
in interfaceSchema
- Returns:
- true if this field is optional, false otherwise
-
optional
public SchemaBuilder optional()
Set this schema as optional.- Returns:
- the SchemaBuilder
-
required
public SchemaBuilder required()
Set this schema as required. This is the default, but this method can be used to make this choice explicit.- Returns:
- the SchemaBuilder
-
defaultValue
public Object defaultValue()
- Specified by:
defaultValue
in interfaceSchema
- Returns:
- the default value for this schema
-
defaultValue
public SchemaBuilder defaultValue(Object value)
Set the default value for this schema. The value is validated against the schema type, throwing aSchemaBuilderException
if it does not match.- Parameters:
value
- the default value- Returns:
- the SchemaBuilder
-
name
public SchemaBuilder name(String name)
Set the name of this schema.- Parameters:
name
- the schema name- Returns:
- the SchemaBuilder
-
version
public Integer version()
Description copied from interface:Schema
Get the optional version of the schema. If a version is included, newer versions *must* be larger than older ones.
-
version
public SchemaBuilder version(Integer version)
Set the version of this schema. Schema versions are integers which, if provided, must indicate which schema is newer and which is older by their ordering.- Parameters:
version
- the schema version- Returns:
- the SchemaBuilder
-
doc
public String doc()
-
doc
public SchemaBuilder doc(String doc)
Set the documentation for this schema.- Parameters:
doc
- the documentation- Returns:
- the SchemaBuilder
-
parameters
public Map<String,String> parameters()
Description copied from interface:Schema
Get a map of schema parameters.- Specified by:
parameters
in interfaceSchema
- Returns:
- Map containing parameters for this schema, or null if there are no parameters
-
parameter
public SchemaBuilder parameter(String propertyName, String propertyValue)
Set a schema parameter.- Parameters:
propertyName
- name of the schema property to definepropertyValue
- value of the schema property to define, as a String- Returns:
- the SchemaBuilder
-
parameters
public SchemaBuilder parameters(Map<String,String> props)
Set schema parameters. This operation is additive; it does not remove existing parameters that do not appear in the set of properties pass to this method.- Parameters:
props
- Map of properties to set- Returns:
- the SchemaBuilder
-
type
public Schema.Type type()
-
type
public static SchemaBuilder type(Schema.Type type)
Create a SchemaBuilder for the specified type. Usually it will be simpler to use one of the variants likestring()
orstruct()
, but this form can be useful when generating schemas dynamically.- Parameters:
type
- the schema type- Returns:
- a new SchemaBuilder
-
int8
public static SchemaBuilder int8()
- Returns:
- a new
Schema.Type.INT8
SchemaBuilder
-
int16
public static SchemaBuilder int16()
- Returns:
- a new
Schema.Type.INT16
SchemaBuilder
-
int32
public static SchemaBuilder int32()
- Returns:
- a new
Schema.Type.INT32
SchemaBuilder
-
int64
public static SchemaBuilder int64()
- Returns:
- a new
Schema.Type.INT64
SchemaBuilder
-
float32
public static SchemaBuilder float32()
- Returns:
- a new
Schema.Type.FLOAT32
SchemaBuilder
-
float64
public static SchemaBuilder float64()
- Returns:
- a new
Schema.Type.FLOAT64
SchemaBuilder
-
bool
public static SchemaBuilder bool()
- Returns:
- a new
Schema.Type.BOOLEAN
SchemaBuilder
-
string
public static SchemaBuilder string()
- Returns:
- a new
Schema.Type.STRING
SchemaBuilder
-
bytes
public static SchemaBuilder bytes()
- Returns:
- a new
Schema.Type.BYTES
SchemaBuilder
-
struct
public static SchemaBuilder struct()
- Returns:
- a new
Schema.Type.STRUCT
SchemaBuilder
-
field
public SchemaBuilder field(String fieldName, Schema fieldSchema)
Add a field to this struct schema. Throws a SchemaBuilderException if this is not a struct schema.- Parameters:
fieldName
- the name of the field to addfieldSchema
- the Schema for the field's value- Returns:
- the SchemaBuilder
-
fields
public List<Field> fields()
Get the list of fields for this Schema. Throws a DataException if this schema is not a struct.
-
field
public Field field(String fieldName)
Description copied from interface:Schema
Get a field for this Schema by name. Throws a DataException if this schema is not a struct.
-
array
public static SchemaBuilder array(Schema valueSchema)
- Parameters:
valueSchema
- the schema for elements of the array- Returns:
- a new
Schema.Type.ARRAY
SchemaBuilder
-
map
public static SchemaBuilder map(Schema keySchema, Schema valueSchema)
- Parameters:
keySchema
- the schema for keys in the mapvalueSchema
- the schema for values in the map- Returns:
- a new
Schema.Type.MAP
SchemaBuilder
-
keySchema
public Schema keySchema()
Description copied from interface:Schema
Get the key schema for this map schema. Throws a DataException if this schema is not a map.
-
valueSchema
public Schema valueSchema()
Description copied from interface:Schema
Get the value schema for this map or array schema. Throws a DataException if this schema is not a map or array.- Specified by:
valueSchema
in interfaceSchema
- Returns:
- the value schema
-
-