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 and Description |
---|
Struct(Schema schema)
Create a new Struct for this
Schema |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o) |
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.
|
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.
|
<T> List<T> |
getArray(String fieldName)
Equivalent to calling
get(String) and casting the result to a List. |
Boolean |
getBoolean(String fieldName)
Equivalent to calling
get(String) and casting the result to a Boolean. |
byte[] |
getBytes(String fieldName)
Equivalent to calling
get(String) and casting the result to a byte[]. |
Float |
getFloat32(String fieldName)
Equivalent to calling
get(String) and casting the result to a Float. |
Double |
getFloat64(String fieldName)
Equivalent to calling
get(String) and casting the result to a Double. |
Short |
getInt16(String fieldName)
Equivalent to calling
get(String) and casting the result to a Short. |
Integer |
getInt32(String fieldName)
Equivalent to calling
get(String) and casting the result to a Integer. |
Long |
getInt64(String fieldName)
Equivalent to calling
get(String) and casting the result to a Long. |
Byte |
getInt8(String fieldName)
Equivalent to calling
get(String) and casting the result to a Byte. |
<K,V> Map<K,V> |
getMap(String fieldName)
Equivalent to calling
get(String) and casting the result to a Map. |
String |
getString(String fieldName)
Equivalent to calling
get(String) and casting the result to a String. |
Struct |
getStruct(String fieldName)
Equivalent to calling
get(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(Field field,
Object value)
Set the value of a field.
|
Struct |
put(String fieldName,
Object value)
Set the value of a field.
|
Schema |
schema()
Get the schema for this Struct.
|
void |
validate()
Validates that this struct has filled in all the necessary data with valid values.
|
public Schema schema()
public Object get(String fieldName)
Object
and
must be cast to a more specific type.fieldName
- the field name to lookuppublic Object get(Field field)
Object
and
must be cast to a more specific type.field
- the field to lookuppublic Object getWithoutDefault(String fieldName)
fieldName
- the field to get the value ofpublic Byte getInt8(String fieldName)
get(String)
and casting the result to a Byte.public Short getInt16(String fieldName)
get(String)
and casting the result to a Short.public Integer getInt32(String fieldName)
get(String)
and casting the result to a Integer.public Long getInt64(String fieldName)
get(String)
and casting the result to a Long.public Float getFloat32(String fieldName)
get(String)
and casting the result to a Float.public Double getFloat64(String fieldName)
get(String)
and casting the result to a Double.public Boolean getBoolean(String fieldName)
get(String)
and casting the result to a Boolean.public String getString(String fieldName)
get(String)
and casting the result to a String.public byte[] getBytes(String fieldName)
get(String)
and casting the result to a byte[].public <T> List<T> getArray(String fieldName)
get(String)
and casting the result to a List.public <K,V> Map<K,V> getMap(String fieldName)
get(String)
and casting the result to a Map.public Struct getStruct(String fieldName)
get(String)
and casting the result to a Struct.public Struct put(String fieldName, Object value)
DataException
if it does not match the field's
Schema
.fieldName
- the name of the field to setvalue
- the value of the fieldput(String, Object)
callspublic Struct put(Field field, Object value)
DataException
if it does not match the field's
Schema
.field
- the field to setvalue
- the value of the fieldput(String, Object)
callspublic void validate()