Class Struct

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

public class Struct extends Object

A structured record containing a set of named fields with values, each field using an independent Schema. Struct objects must specify a complete Schema up front, and only fields specified in the Schema may be set.

The Struct's put(String, Object) method returns the Struct itself to provide a fluent API for constructing complete objects:

         Schema schema = SchemaBuilder.struct().name("com.example.Person")
             .field("name", Schema.STRING_SCHEMA).field("age", Schema.INT32_SCHEMA).build()
         Struct struct = new Struct(schema).put("name", "Bobby McGee").put("age", 21)
     

  • Constructor Details

    • Struct

      public Struct(Schema schema)
      Create a new Struct for this Schema
      Parameters:
      schema - the Schema for the Struct
  • Method Details

    • schema

      public Schema schema()
      Get the schema for this Struct.
      Returns:
      the Struct's schema
    • get

      public Object get(String fieldName)
      Get the value of a field, returning the default value if no value has been set yet and a default value is specified in the field's schema. Because this handles fields of all types, the value is returned as an Object and must be cast to a more specific type.
      Parameters:
      fieldName - the field name to lookup
      Returns:
      the value for the field
    • get

      public Object get(Field field)
      Get the value of a field, returning the default value if no value has been set yet and a default value is specified in the field's schema. Because this handles fields of all types, the value is returned as an Object and must be cast to a more specific type.
      Parameters:
      field - the field to lookup
      Returns:
      the value for the field
    • getWithoutDefault

      public Object getWithoutDefault(String fieldName)
      Get the underlying raw value for the field without accounting for default values.
      Parameters:
      fieldName - the field to get the value of
      Returns:
      the raw value
    • getInt8

      public Byte getInt8(String fieldName)
      Equivalent to calling get(String) and casting the result to a Byte.
    • getInt16

      public Short getInt16(String fieldName)
      Equivalent to calling get(String) and casting the result to a Short.
    • getInt32

      public Integer getInt32(String fieldName)
      Equivalent to calling get(String) and casting the result to an Integer.
    • getInt64

      public Long getInt64(String fieldName)
      Equivalent to calling get(String) and casting the result to a Long.
    • getFloat32

      public Float getFloat32(String fieldName)
      Equivalent to calling get(String) and casting the result to a Float.
    • getFloat64

      public Double getFloat64(String fieldName)
      Equivalent to calling get(String) and casting the result to a Double.
    • getBoolean

      public Boolean getBoolean(String fieldName)
      Equivalent to calling get(String) and casting the result to a Boolean.
    • getString

      public String getString(String fieldName)
      Equivalent to calling get(String) and casting the result to a String.
    • getBytes

      public byte[] getBytes(String fieldName)
      Equivalent to calling get(String) and casting the result to a byte[].
    • getArray

      public <T> List<T> getArray(String fieldName)
      Equivalent to calling get(String) and casting the result to a List.
    • getMap

      public <K, V> Map<K,V> getMap(String fieldName)
      Equivalent to calling get(String) and casting the result to a Map.
    • getStruct

      public Struct getStruct(String fieldName)
      Equivalent to calling get(String) and casting the result to a Struct.
    • put

      public Struct put(String fieldName, Object value)
      Set the value of a field. Validates the value, throwing a DataException if it does not match the field's Schema.
      Parameters:
      fieldName - the name of the field to set
      value - the value of the field
      Returns:
      the Struct, to allow chaining of put(String, Object) calls
    • put

      public Struct put(Field field, Object value)
      Set the value of a field. Validates the value, throwing a DataException if it does not match the field's Schema.
      Parameters:
      field - the field to set
      value - the value of the field
      Returns:
      the Struct, to allow chaining of put(String, Object) calls
    • validate

      public void validate()
      Validates that this struct has filled in all the necessary data with valid values. For required fields without defaults, this validates that a value has been set and has matching types/schemas. If any validation fails, throws a DataException.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object