public class Values
extends java.lang.Object
For example, a caller might expects a particular Header
to contain an Schema.Type.INT64
value, when in fact that header contains a string representation of a 32-bit integer. Here, the caller can use the methods in this
class to convert the value to the desired type:
Header header = ... long value = Values.convertToLong(header.schema(), header.value());
This class is able to convert any value to a string representation as well as parse those string representations back into most of
the types. The only exception is Struct
values that require a schema and thus cannot be parsed from a simple string.
Modifier and Type | Class | Description |
---|---|---|
protected static class |
Values.Parser |
|
protected static class |
Values.SchemaDetector |
Constructor | Description |
---|---|
Values() |
Modifier and Type | Method | Description |
---|---|---|
protected static java.util.List<java.lang.Object> |
alignListEntriesWithSchema(Schema schema,
java.util.List<java.lang.Object> input) |
|
protected static java.util.Map<java.lang.Object,java.lang.Object> |
alignMapKeysAndValuesWithSchema(Schema mapSchema,
java.util.Map<java.lang.Object,java.lang.Object> input) |
|
protected static void |
append(java.lang.StringBuilder sb,
java.lang.Object value,
boolean embedded) |
|
protected static void |
appendIterable(java.lang.StringBuilder sb,
java.util.Iterator<?> iter) |
|
protected static double |
asDouble(java.lang.Object value,
Schema schema,
java.lang.Throwable error) |
Convert the specified value with the desired floating point type.
|
protected static long |
asLong(java.lang.Object value,
Schema fromSchema,
java.lang.Throwable error) |
Convert the specified value to the desired scalar value type.
|
protected static Schema |
commonSchemaFor(Schema previous,
SchemaAndValue latest) |
|
protected static java.lang.Object |
convertTo(Schema toSchema,
Schema fromSchema,
java.lang.Object value) |
Convert the value to the desired type.
|
static java.lang.Boolean |
convertToBoolean(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.BOOLEAN value. |
static java.lang.Byte |
convertToByte(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.INT8 byte value. |
static java.util.Date |
convertToDate(Schema schema,
java.lang.Object value) |
Convert the specified value to an
date value. |
static java.math.BigDecimal |
convertToDecimal(Schema schema,
java.lang.Object value,
int scale) |
Convert the specified value to an
decimal value. |
static java.lang.Double |
convertToDouble(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.FLOAT64 double value. |
static java.lang.Float |
convertToFloat(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.FLOAT32 float value. |
static java.lang.Integer |
convertToInteger(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.INT32 int value. |
static java.util.List<?> |
convertToList(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.ARRAY value. |
static java.lang.Long |
convertToLong(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.INT64 long value. |
static java.util.Map<?,?> |
convertToMap(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.MAP value. |
static java.lang.Short |
convertToShort(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.INT16 short value. |
static java.lang.String |
convertToString(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.STRING value. |
static Struct |
convertToStruct(Schema schema,
java.lang.Object value) |
Convert the specified value to an
Schema.Type.STRUCT value. |
static java.util.Date |
convertToTime(Schema schema,
java.lang.Object value) |
Convert the specified value to an
time value. |
static java.util.Date |
convertToTimestamp(Schema schema,
java.lang.Object value) |
Convert the specified value to an
timestamp value. |
protected static java.text.DateFormat |
dateFormatFor(java.util.Date value) |
|
protected static java.lang.String |
escape(java.lang.String value) |
|
static Schema |
inferSchema(java.lang.Object value) |
If possible infer a schema for the given value.
|
protected static SchemaAndValue |
parse(Values.Parser parser,
boolean embedded) |
|
static SchemaAndValue |
parseString(java.lang.String value) |
Parse the specified string representation of a value into its schema and value.
|
public static java.lang.Boolean convertToBoolean(Schema schema, java.lang.Object value) throws DataException
Schema.Type.BOOLEAN
value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a boolean.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a booleanpublic static java.lang.Byte convertToByte(Schema schema, java.lang.Object value) throws DataException
Schema.Type.INT8
byte value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a byte.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a bytepublic static java.lang.Short convertToShort(Schema schema, java.lang.Object value) throws DataException
Schema.Type.INT16
short value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a short.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a shortpublic static java.lang.Integer convertToInteger(Schema schema, java.lang.Object value) throws DataException
Schema.Type.INT32
int value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to an integer.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to an integerpublic static java.lang.Long convertToLong(Schema schema, java.lang.Object value) throws DataException
Schema.Type.INT64
long value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a long.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a longpublic static java.lang.Float convertToFloat(Schema schema, java.lang.Object value) throws DataException
Schema.Type.FLOAT32
float value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a floating point number.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a floatpublic static java.lang.Double convertToDouble(Schema schema, java.lang.Object value) throws DataException
Schema.Type.FLOAT64
double value. The supplied schema is required if the value is a logical
type when the schema contains critical information that might be necessary for converting to a floating point number.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value could not be converted to a doublepublic static java.lang.String convertToString(Schema schema, java.lang.Object value)
Schema.Type.STRING
value.
Not supplying a schema may limit the ability to convert to the desired type.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullpublic static java.util.List<?> convertToList(Schema schema, java.lang.Object value)
Schema.Type.ARRAY
value. If the value is a string representation of an array, this method
will parse the string and its elements to infer the schemas for those elements. Thus, this method supports
arrays of other primitives and structured types. If the value is already an array (or list), this method simply casts and
returns it.
This method currently does not use the schema, though it may be used in the future.
schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a list valuepublic static java.util.Map<?,?> convertToMap(Schema schema, java.lang.Object value)
Schema.Type.MAP
value. If the value is a string representation of a map, this method
will parse the string and its entries to infer the schemas for those entries. Thus, this method supports
maps with primitives and structured keys and values. If the value is already a map, this method simply casts and returns it.
This method currently does not use the schema, though it may be used in the future.
schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a map valuepublic static Struct convertToStruct(Schema schema, java.lang.Object value)
Schema.Type.STRUCT
value. Structs cannot be converted from other types, so this method returns
a struct only if the supplied value is a struct. If not a struct, this method throws an exception.
This method currently does not use the schema, though it may be used in the future.
schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value is not a structpublic static java.util.Date convertToTime(Schema schema, java.lang.Object value)
time
value.
Not supplying a schema may limit the ability to convert to the desired type.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a time valuepublic static java.util.Date convertToDate(Schema schema, java.lang.Object value)
date
value.
Not supplying a schema may limit the ability to convert to the desired type.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a date valuepublic static java.util.Date convertToTimestamp(Schema schema, java.lang.Object value)
timestamp
value.
Not supplying a schema may limit the ability to convert to the desired type.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a timestamp valuepublic static java.math.BigDecimal convertToDecimal(Schema schema, java.lang.Object value, int scale)
decimal
value.
Not supplying a schema may limit the ability to convert to the desired type.schema
- the schema for the value; may be nullvalue
- the value to be converted; may be nullDataException
- if the value cannot be converted to a decimal valuepublic static Schema inferSchema(java.lang.Object value)
value
- the value whose schema is to be inferred; may be nullpublic static SchemaAndValue parseString(java.lang.String value)
value
- the string form of the valueconvertToString(org.apache.kafka.connect.data.Schema, java.lang.Object)
protected static java.lang.Object convertTo(Schema toSchema, Schema fromSchema, java.lang.Object value) throws DataException
toSchema
- the schema for the desired type; may not be nullfromSchema
- the schema for the supplied value; may be null if not knownDataException
- if the value could not be converted to the desired typeprotected static long asLong(java.lang.Object value, Schema fromSchema, java.lang.Throwable error)
value
- the value to be converted; may not be nullfromSchema
- the schema for the current value type; may not be nullerror
- any previous error that should be included in an exception message; may be nullDataException
- if the value could not be converted to a longprotected static double asDouble(java.lang.Object value, Schema schema, java.lang.Throwable error)
value
- the value to be converted; may not be nullschema
- the schema for the current value type; may not be nullerror
- any previous error that should be included in an exception message; may be nullDataException
- if the value could not be converted to a doubleprotected static void append(java.lang.StringBuilder sb, java.lang.Object value, boolean embedded)
protected static void appendIterable(java.lang.StringBuilder sb, java.util.Iterator<?> iter)
protected static java.lang.String escape(java.lang.String value)
protected static java.text.DateFormat dateFormatFor(java.util.Date value)
protected static SchemaAndValue parse(Values.Parser parser, boolean embedded) throws java.util.NoSuchElementException
java.util.NoSuchElementException
protected static Schema commonSchemaFor(Schema previous, SchemaAndValue latest)
protected static java.util.List<java.lang.Object> alignListEntriesWithSchema(Schema schema, java.util.List<java.lang.Object> input)
protected static java.util.Map<java.lang.Object,java.lang.Object> alignMapKeysAndValuesWithSchema(Schema mapSchema, java.util.Map<java.lang.Object,java.lang.Object> input)