Package org.apache.avro
Class Schema
java.lang.Object
org.apache.avro.JsonProperties
org.apache.avro.Schema
- All Implemented Interfaces:
Serializable
An abstract data type.
A schema may be one of:
- A record, mapping field names to field value data;
- An enum, containing one of a small set of symbols;
- An array of values, all of the same schema;
- A map, containing string/value pairs, of a declared schema;
- A union of other schemas;
- A fixed sized binary object;
- A unicode string;
- A sequence of bytes;
- A 32-bit signed int;
- A 64-bit signed long;
- A 32-bit IEEE single-float; or
- A 64-bit IEEE double-float; or
- A boolean; or
- null.
SchemaBuilder
. The schema objects are
logically immutable. There are only two mutating methods -
setFields(List)
and addProp(String, String)
. The following
restrictions apply on these two methods.
setFields(List)
, can be called at most once. This method exists in order to enable clients to build recursive schemas.addProp(String, String)
can be called with property names that are not present already. It is not possible to change or delete an existing property.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A field within a record.static class
A parser for JSON-format schemas.static class
Useful as key ofMap
s when traversing two schemas at the same time and need to watch for recursion.static enum
The type of schema.Nested classes/interfaces inherited from class org.apache.avro.JsonProperties
JsonProperties.Null
-
Field Summary
Fields inherited from class org.apache.avro.JsonProperties
NULL_VALUE
-
Method Summary
Modifier and TypeMethodDescriptionvoid
If this is a record, enum or fixed, add an alias.void
If this is a record, enum or fixed, add an alias.void
void
Adds a property with the given name name and value value.static Schema
applyAliases
(Schema writer, Schema reader) Rewrite a writer's schema using the aliases from a reader's schema.static Schema
create
(Schema.Type type) Create a schema for a primitive type.static Schema
createArray
(Schema elementType) Create an array schema.static Schema
Create an enum schema.static Schema
Create an enum schema.static Schema
createFixed
(String name, String doc, String space, int size) Create a fixed schema.static Schema
Create a map schema.static Schema
createRecord
(String name, String doc, String namespace, boolean isError) Create a named record schema.static Schema
createRecord
(String name, String doc, String namespace, boolean isError, List<Schema.Field> fields) Create a named record schema with fields already set.static Schema
createRecord
(List<Schema.Field> fields) Deprecated.static Schema
createUnion
(List<Schema> types) Create a union schema.static Schema
createUnion
(Schema... types) Create a union schema.boolean
If this is a record, enum or fixed, return its aliases, if any.getDoc()
If this is a record, enum, or fixed, returns its docstring, if available.If this is an array, returns its element type.If this is an enum, return its default value.int
getEnumOrdinal
(String symbol) If this is an enum, return a symbol's ordinal value.If this is an enum, return its symbols.If this is a record, returns the Field with the given name fieldName.If this is a record, returns the fields in it.int
If this is fixed, returns its size.If this is a record, enum or fixed, returns its namespace-qualified name, otherwise returns the name of the primitive type.getIndexNamed
(String name) If this is a union, return the branch with the provided full name.getName()
If this is a record, enum or fixed, returns its name, otherwise the name of the primitive type.If this is a record, enum or fixed, returns its namespace, if any.static NameValidator
Deprecated.getType()
Return the type of this schema.getTypes()
If this is a union, returns its types.static boolean
Deprecated.If this is a map, returns its value type.boolean
hasEnumSymbol
(String symbol) If this is an enum, returns true if it contains given symbol.boolean
If this is a record, returns whether the fields have been set.final int
hashCode()
boolean
isError()
Returns true if this record is an error type.boolean
Returns true if this record is a union type containing null.boolean
isUnion()
Returns true if this record is a union type.boolean
isValidDefault
(com.fasterxml.jackson.databind.JsonNode jsonValue) Checks if a JSON value matches the schema.static Schema
Deprecated.useSchemaParser
instead.static Schema
parse
(InputStream in) Deprecated.useSchemaParser
instead.static Schema
Deprecated.useSchemaParser
instead.static Schema
Deprecated.useSchemaParser
instead.static Object
Parses the specified json string to an object.void
setFields
(List<Schema.Field> fields) If this is a record, set its fields.static void
setNameValidator
(NameValidator validator) Deprecated.static void
setValidateDefaults
(boolean validateDefaults) Deprecated.toString()
Render this as JSON.toString
(boolean pretty) Deprecated.toString
(Collection<Schema> referencedSchemas, boolean pretty) Deprecated.protected Object
Methods inherited from class org.apache.avro.JsonProperties
addAllProps, forEachProperty, getObjectProp, getObjectProp, getObjectProps, getProp, hasProps, propsContainsKey, putAll
-
Method Details
-
writeReplace
-
create
Create a schema for a primitive type. -
addProp
Description copied from class:JsonProperties
Adds a property with the given name name and value value. Neither name nor value can be null. It is illegal to add a property if another with the same name but different value already exists in this schema.- Overrides:
addProp
in classJsonProperties
- Parameters:
name
- The name of the property to addvalue
- The value for the property to add
-
addProp
- Overrides:
addProp
in classJsonProperties
-
getLogicalType
-
createRecord
Deprecated.This method allows to create Schema objects that cannot be parsed bySchemaParser.parse(CharSequence)
. It will be removed in a future version of Avro. Better usecreateRecord(String, String, String, boolean, List)
to produce a fully qualified Schema.Create an anonymous record schema. -
createRecord
Create a named record schema. -
createRecord
public static Schema createRecord(String name, String doc, String namespace, boolean isError, List<Schema.Field> fields) Create a named record schema with fields already set. -
createEnum
Create an enum schema. -
createEnum
public static Schema createEnum(String name, String doc, String namespace, List<String> values, String enumDefault) Create an enum schema. -
createArray
Create an array schema. -
createMap
Create a map schema. -
createUnion
Create a union schema. -
createUnion
Create a union schema. -
createFixed
Create a fixed schema. -
getType
Return the type of this schema. -
getField
If this is a record, returns the Field with the given name fieldName. If there is no field by that name, a null is returned. -
getFields
If this is a record, returns the fields in it. The returned list is in the order of their positions. -
hasFields
public boolean hasFields()If this is a record, returns whether the fields have been set. -
setFields
If this is a record, set its fields. The fields can be set only once in a schema. -
getEnumSymbols
If this is an enum, return its symbols. -
getEnumDefault
If this is an enum, return its default value. -
getEnumOrdinal
If this is an enum, return a symbol's ordinal value. -
hasEnumSymbol
If this is an enum, returns true if it contains given symbol. -
getName
If this is a record, enum or fixed, returns its name, otherwise the name of the primitive type. -
getDoc
If this is a record, enum, or fixed, returns its docstring, if available. Otherwise, returns null. -
getNamespace
If this is a record, enum or fixed, returns its namespace, if any. -
getFullName
If this is a record, enum or fixed, returns its namespace-qualified name, otherwise returns the name of the primitive type. -
addAlias
If this is a record, enum or fixed, add an alias. -
addAlias
If this is a record, enum or fixed, add an alias. -
getAliases
If this is a record, enum or fixed, return its aliases, if any. -
isError
public boolean isError()Returns true if this record is an error type. -
getElementType
If this is an array, returns its element type. -
getValueType
If this is a map, returns its value type. -
getTypes
If this is a union, returns its types. -
getIndexNamed
If this is a union, return the branch with the provided full name. -
getFixedSize
public int getFixedSize()If this is fixed, returns its size. -
toString
Render this as JSON.
This method is equivalent to:
SchemaFormatter.getInstance("json").format(this)
-
toString
Deprecated.Render this as JSON.- Parameters:
pretty
- if true, pretty-print JSON.
-
toString
Deprecated.Render this as JSON, but without inlining the referenced schemas.- Parameters:
referencedSchemas
- referenced schemaspretty
- if true, pretty-print JSON.
-
equals
-
hashCode
public final int hashCode() -
isUnion
public boolean isUnion()Returns true if this record is a union type. -
isNullable
public boolean isNullable()Returns true if this record is a union type containing null. -
parse
Deprecated.useSchemaParser
instead.Constructs a Schema object from JSON schema file file. The contents of file is expected to be in UTF-8 format.- Parameters:
file
- The file to read the schema from.- Returns:
- The freshly built Schema.
- Throws:
IOException
- if there was trouble reading the contents, or they are invalid
-
parse
Deprecated.useSchemaParser
instead.Constructs a Schema object from JSON schema stream in. The contents of in is expected to be in UTF-8 format.- Parameters:
in
- The input stream to read the schema from.- Returns:
- The freshly built Schema.
- Throws:
IOException
- if there was trouble reading the contents, or they are invalid
-
parse
Deprecated.useSchemaParser
instead.Construct a schema from JSON text. -
parse
Deprecated.useSchemaParser
instead.Construct a schema from JSON text.- Parameters:
validate
- true if names should be validated, false if not.
-
setNameValidator
Deprecated. -
getNameValidator
Deprecated. -
setValidateDefaults
Deprecated. -
getValidateDefaults
Deprecated. -
isValidDefault
public boolean isValidDefault(com.fasterxml.jackson.databind.JsonNode jsonValue) Checks if a JSON value matches the schema.- Parameters:
jsonValue
- a value to check against the schema- Returns:
- true if the value is valid according to this schema
-
parseJsonToObject
Parses the specified json string to an object. -
applyAliases
Rewrite a writer's schema using the aliases from a reader's schema. This permits reading records, enums and fixed schemas whose names have changed, and records whose field names have changed. The returned schema always contains the same data elements in the same order, but with possibly different names.
-
SchemaParser.parse(CharSequence)
.