Package org.apache.kafka.connect.data
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 Summary
-
Method Summary
Modifier and Type Method Description boolean
equals(Object o)
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.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.<T> List<T>
getArray(String fieldName)
Equivalent to callingget(String)
and casting the result to a List.Boolean
getBoolean(String fieldName)
Equivalent to callingget(String)
and casting the result to a Boolean.byte[]
getBytes(String fieldName)
Equivalent to callingget(String)
and casting the result to a byte[].Float
getFloat32(String fieldName)
Equivalent to callingget(String)
and casting the result to a Float.Double
getFloat64(String fieldName)
Equivalent to callingget(String)
and casting the result to a Double.Short
getInt16(String fieldName)
Equivalent to callingget(String)
and casting the result to a Short.Integer
getInt32(String fieldName)
Equivalent to callingget(String)
and casting the result to a Integer.Long
getInt64(String fieldName)
Equivalent to callingget(String)
and casting the result to a Long.Byte
getInt8(String fieldName)
Equivalent to callingget(String)
and casting the result to a Byte.<K, V> Map<K,V>
getMap(String fieldName)
Equivalent to callingget(String)
and casting the result to a Map.String
getString(String fieldName)
Equivalent to callingget(String)
and casting the result to a String.Struct
getStruct(String fieldName)
Equivalent to callingget(String)
and casting the result to a Struct.Object
getWithoutDefault(String fieldName)
Get the underlying raw value for the field without accounting for default values.int
hashCode()
Struct
put(String fieldName, Object value)
Set the value of a field.Struct
put(Field field, Object value)
Set the value of a field.Schema
schema()
Get the schema for this Struct.String
toString()
void
validate()
Validates that this struct has filled in all the necessary data with valid values.
-
Constructor Details
-
Method Details
-
schema
Get the schema for this Struct.- Returns:
- the Struct's schema
-
get
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 anObject
and must be cast to a more specific type.- Parameters:
fieldName
- the field name to lookup- Returns:
- the value for the field
-
get
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 anObject
and must be cast to a more specific type.- Parameters:
field
- the field to lookup- Returns:
- the value for the field
-
getWithoutDefault
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
Equivalent to callingget(String)
and casting the result to a Byte. -
getInt16
Equivalent to callingget(String)
and casting the result to a Short. -
getInt32
Equivalent to callingget(String)
and casting the result to a Integer. -
getInt64
Equivalent to callingget(String)
and casting the result to a Long. -
getFloat32
Equivalent to callingget(String)
and casting the result to a Float. -
getFloat64
Equivalent to callingget(String)
and casting the result to a Double. -
getBoolean
Equivalent to callingget(String)
and casting the result to a Boolean. -
getString
Equivalent to callingget(String)
and casting the result to a String. -
getBytes
Equivalent to callingget(String)
and casting the result to a byte[]. -
getArray
Equivalent to callingget(String)
and casting the result to a List. -
getMap
Equivalent to callingget(String)
and casting the result to a Map. -
getStruct
Equivalent to callingget(String)
and casting the result to a Struct. -
put
Set the value of a field. Validates the value, throwing aDataException
if it does not match the field'sSchema
.- Parameters:
fieldName
- the name of the field to setvalue
- the value of the field- Returns:
- the Struct, to allow chaining of
put(String, Object)
calls
-
put
Set the value of a field. Validates the value, throwing aDataException
if it does not match the field'sSchema
.- Parameters:
field
- the field to setvalue
- 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
-
hashCode
public int hashCode() -
toString
-