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 completeSchemaup 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)
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)Objectget(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.Objectget(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.BooleangetBoolean(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[].FloatgetFloat32(String fieldName)Equivalent to callingget(String)and casting the result to a Float.DoublegetFloat64(String fieldName)Equivalent to callingget(String)and casting the result to a Double.ShortgetInt16(String fieldName)Equivalent to callingget(String)and casting the result to a Short.IntegergetInt32(String fieldName)Equivalent to callingget(String)and casting the result to a Integer.LonggetInt64(String fieldName)Equivalent to callingget(String)and casting the result to a Long.BytegetInt8(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.StringgetString(String fieldName)Equivalent to callingget(String)and casting the result to a String.StructgetStruct(String fieldName)Equivalent to callingget(String)and casting the result to a Struct.ObjectgetWithoutDefault(String fieldName)Get the underlying raw value for the field without accounting for default values.inthashCode()Structput(String fieldName, Object value)Set the value of a field.Structput(Field field, Object value)Set the value of a field.Schemaschema()Get the schema for this Struct.StringtoString()voidvalidate()Validates that this struct has filled in all the necessary data with valid values.
-
-
-
Method Detail
-
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 anObjectand 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 anObjectand 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 callingget(String)and casting the result to a Byte.
-
getInt16
public Short getInt16(String fieldName)
Equivalent to callingget(String)and casting the result to a Short.
-
getInt32
public Integer getInt32(String fieldName)
Equivalent to callingget(String)and casting the result to a Integer.
-
getInt64
public Long getInt64(String fieldName)
Equivalent to callingget(String)and casting the result to a Long.
-
getFloat32
public Float getFloat32(String fieldName)
Equivalent to callingget(String)and casting the result to a Float.
-
getFloat64
public Double getFloat64(String fieldName)
Equivalent to callingget(String)and casting the result to a Double.
-
getBoolean
public Boolean getBoolean(String fieldName)
Equivalent to callingget(String)and casting the result to a Boolean.
-
getString
public String getString(String fieldName)
Equivalent to callingget(String)and casting the result to a String.
-
getBytes
public byte[] getBytes(String fieldName)
Equivalent to callingget(String)and casting the result to a byte[].
-
getArray
public <T> List<T> getArray(String fieldName)
Equivalent to callingget(String)and casting the result to a List.
-
getMap
public <K,V> Map<K,V> getMap(String fieldName)
Equivalent to callingget(String)and casting the result to a Map.
-
getStruct
public Struct getStruct(String fieldName)
Equivalent to callingget(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 aDataExceptionif 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
public Struct put(Field field, Object value)
Set the value of a field. Validates the value, throwing aDataExceptionif 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.
-
-