Class Values

java.lang.Object
org.apache.kafka.connect.data.Values

public class Values extends Object
Utility for converting from one Connect value to a different form. This is useful when the caller expects a value of a particular type but is uncertain whether the actual value is one that isn't directly that type but can be converted into that type.

For example, a caller might expect a particular Header to contain a 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.

  • Constructor Details

    • Values

      public Values()
  • Method Details

    • convertToBoolean

      public static Boolean convertToBoolean(Schema schema, Object value) throws DataException
      Convert the specified value to a 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a boolean, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a boolean
    • convertToByte

      public static Byte convertToByte(Schema schema, Object value) throws DataException
      Convert the specified value to an 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a byte, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a byte
    • convertToShort

      public static Short convertToShort(Schema schema, Object value) throws DataException
      Convert the specified value to an 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a short, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a short
    • convertToInteger

      public static Integer convertToInteger(Schema schema, Object value) throws DataException
      Convert the specified value to an 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as an integer, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to an integer
    • convertToLong

      public static Long convertToLong(Schema schema, Object value) throws DataException
      Convert the specified value to an 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a long, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a long
    • convertToFloat

      public static Float convertToFloat(Schema schema, Object value) throws DataException
      Convert the specified value to a 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a float, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a float
    • convertToDouble

      public static Double convertToDouble(Schema schema, Object value) throws DataException
      Convert the specified value to a 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.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a double, or null if the supplied value was null
      Throws:
      DataException - if the value could not be converted to a double
    • convertToString

      public static String convertToString(Schema schema, Object value)
      Convert the specified value to a Schema.Type.STRING value. Not supplying a schema may limit the ability to convert to the desired type.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a string, or null if the supplied value was null
    • convertToList

      public static List<?> convertToList(Schema schema, Object value)
      Convert the specified value to an 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.

      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a list, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a list value
    • convertToMap

      public static Map<?,?> convertToMap(Schema schema, Object value)
      Convert the specified value to a 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.

      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a map, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a map value
    • convertToStruct

      public static Struct convertToStruct(Schema schema, Object value)
      Convert the specified value to a 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.

      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a struct, or null if the supplied value was null
      Throws:
      DataException - if the value is not a struct
    • convertToTime

      public static Date convertToTime(Schema schema, Object value)
      Convert the specified value to a time value. Not supplying a schema may limit the ability to convert to the desired type.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a time, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a time value
    • convertToDate

      public static Date convertToDate(Schema schema, Object value)
      Convert the specified value to a date value. Not supplying a schema may limit the ability to convert to the desired type.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a date, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a date value
    • convertToTimestamp

      public static Date convertToTimestamp(Schema schema, Object value)
      Convert the specified value to a timestamp value. Not supplying a schema may limit the ability to convert to the desired type.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a timestamp, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a timestamp value
    • convertToDecimal

      public static BigDecimal convertToDecimal(Schema schema, Object value, int scale)
      Convert the specified value to a decimal value. Not supplying a schema may limit the ability to convert to the desired type.
      Parameters:
      schema - the schema for the value; may be null
      value - the value to be converted; may be null
      Returns:
      the representation as a decimal, or null if the supplied value was null
      Throws:
      DataException - if the value cannot be converted to a decimal value
    • inferSchema

      public static Schema inferSchema(Object value)
      If possible infer a schema for the given value.
      Parameters:
      value - the value whose schema is to be inferred; may be null
      Returns:
      the inferred schema, or null if the value is null or no schema could be inferred
    • parseString

      public static SchemaAndValue parseString(String value)
      Parse the specified string representation of a value into its schema and value.
      Parameters:
      value - the string form of the value
      Returns:
      the schema and value; never null, but whose schema and value may be null
      See Also:
    • dateFormatFor

      public static DateFormat dateFormatFor(Date value)