Package org.apache.kafka.connect.data
Class SchemaBuilder
java.lang.Object
org.apache.kafka.connect.data.SchemaBuilder
- All Implemented Interfaces:
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();
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.kafka.connect.data.Schema
Schema.TypeField 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_SCHEMAConstructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic SchemaBuilderstatic SchemaBuilderbool()build()Build the Schema using the current settingsstatic SchemaBuilderbytes()defaultValue(Object value) Set the default value for this schema.doc()Set the documentation for this schema.Get aFieldfor this Schema by name.Add a field to thisSchema.Type.STRUCTschema.fields()Get the list of fields for this Schema.static SchemaBuilderfloat32()static SchemaBuilderfloat64()static SchemaBuilderint16()static SchemaBuilderint32()static SchemaBuilderint64()static SchemaBuilderint8()booleanGet the key schema for this map schema.static SchemaBuildername()Set the name of this schema.optional()Set this schema as optional.Set a schema parameter.Get a map of schema parameters.parameters(Map<String, String> props) Set schema parameters.required()Set this schema as required.schema()Return a concrete instance of theSchemaspecified by this builderstatic SchemaBuilderstring()static SchemaBuilderstruct()type()static SchemaBuildertype(Schema.Type type) Create a SchemaBuilder for the specified type.Get the value schema for this map or array schema.version()Get the optional version of the schema.Set the version of this schema.
Constructor Details
SchemaBuilder
Method Details
isOptional
public boolean isOptional()- Specified by:
isOptionalin interfaceSchema- Returns:
- true if this field is optional, false otherwise
optional
Set this schema as optional.- Returns:
- the 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
- Specified by:
defaultValuein interfaceSchema- Returns:
- the default value for this schema
defaultValue
Set the default value for this schema. The value is validated against the schema type, throwing aSchemaBuilderExceptionif it does not match.- Parameters:
value- the default value- Returns:
- the SchemaBuilder
name
name
Set the name of this schema.- Parameters:
name- the schema name- Returns:
- the SchemaBuilder
version
Description copied from interface:SchemaGet the optional version of the schema. If a version is included, newer versions must be larger than older ones.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
doc
Set the documentation for this schema.- Parameters:
doc- the documentation- Returns:
- the SchemaBuilder
parameters
Description copied from interface:SchemaGet a map of schema parameters.- Specified by:
parametersin interfaceSchema- Returns:
- Map containing parameters for this schema, or null if there are no parameters
parameter
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
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
type
Create a SchemaBuilder for the specified type.Usually it will be simpler to use one of the variants like
string()orstruct(), but this form can be useful when generating schemas dynamically.- Parameters:
type- the schema type- Returns:
- a new SchemaBuilder
int8
- Returns:
- a new
Schema.Type.INT8SchemaBuilder
int16
- Returns:
- a new
Schema.Type.INT16SchemaBuilder
int32
- Returns:
- a new
Schema.Type.INT32SchemaBuilder
int64
- Returns:
- a new
Schema.Type.INT64SchemaBuilder
float32
- Returns:
- a new
Schema.Type.FLOAT32SchemaBuilder
float64
- Returns:
- a new
Schema.Type.FLOAT64SchemaBuilder
bool
- Returns:
- a new
Schema.Type.BOOLEANSchemaBuilder
string
- Returns:
- a new
Schema.Type.STRINGSchemaBuilder
bytes
- Returns:
- a new
Schema.Type.BYTESSchemaBuilder
struct
- Returns:
- a new
Schema.Type.STRUCTSchemaBuilder
field
Add a field to thisSchema.Type.STRUCTschema. Throws aSchemaBuilderExceptionif 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
Get the list of fields for this Schema. Throws aDataExceptionif this schema is not aSchema.Type.STRUCT.field
Description copied from interface:SchemaGet aFieldfor this Schema by name. Throws aDataExceptionif this schema is not aSchema.Type.STRUCT.array
- Parameters:
valueSchema- the schema for elements of the array- Returns:
- a new
Schema.Type.ARRAYSchemaBuilder
map
- Parameters:
keySchema- the schema for keys in the mapvalueSchema- the schema for values in the map- Returns:
- a new
Schema.Type.MAPSchemaBuilder
keySchema
Description copied from interface:SchemaGet the key schema for this map schema. Throws aDataExceptionif this schema is not a map.valueSchema
Description copied from interface:SchemaGet the value schema for this map or array schema. Throws aDataExceptionif this schema is not a map or array.- Specified by:
valueSchemain interfaceSchema- Returns:
- the value schema
build
Build the Schema using the current settings- Returns:
- the
Schema
schema
Return a concrete instance of theSchemaspecified by this builder