Interface StructBuilder
Sets the fields of one struct — a record being written, or a struct nested inside one.
A record is a struct, so RowWriter.writeRow(Consumer) hands the same builder a nested
setStruct(String, Consumer) does. Fields are addressed by the name they carry in the schema, or by their
position in it; the synthetic list / element and key_value groups a LIST or MAP
introduces are never spelled by the caller.
rows.writeRow(row -> row
.setLong("id", 1)
.setString("name", "hardwood")
.setStruct("address", address -> address.setString("city", "Berlin")));
Every setter has an index-taking mirror, and getFieldCount() and getFieldName(int) report
the positions those indices address — the same pair FieldAccessor
exposes on the read side.
A loop that walks a row's fields by index can therefore read back and write forward
through the same positions, provided the write schema mirrors what was read. The
reader's index is the position in projected schema order; this one is the position in
the write schema's declaration order. They are the same position when the whole file is
read into its own schema, and they diverge under a projection: reading three of ten
columns and writing into the ten-column schema shifts every position, and where the
columns that land on each other happen to share a type the values are written to the
wrong fields rather than rejected. Comparing getFieldName(int) against the reader's for
each index costs one string comparison per field and turns that into a failure.
A field that is never set is written as null if it is OPTIONAL, and fails the record if
it is REQUIRED; setNull(String) states the same thing explicitly, and a null value handed
to any of the object-typed setters does too. Setting the same field twice within one
scope, naming a field the schema does not have, or using a setter that does not fit the
field's declared type all throw at the call.
The builder is valid only inside the lambda it was passed to. Retaining one and using it
afterwards throws IllegalStateException rather than writing into a later record.
This API is Experimental: the shape may change in future releases.
-
Method Summary
Modifier and TypeMethodDescriptionintThe number of fields this struct declares — the record's top-level fields for the builderRowWriter.writeRow(Consumer)hands over, and a nested struct's own fields for the onesetStruct(String, Consumer)does.getFieldName(int fieldIndex) The name of the field atfieldIndex, the inverse of the resolution the by-name setters perform.setBinary(int fieldIndex, byte[] value) Sets aBYTE_ARRAYorFIXED_LEN_BYTE_ARRAYfield by index.Sets aBYTE_ARRAYorFIXED_LEN_BYTE_ARRAYfield from its bytes, which are referenced rather than copied and must not be mutated until the record is written.setBoolean(int fieldIndex, boolean value) Sets aBOOLEANfield by index.setBoolean(String name, boolean value) Sets aBOOLEANfield.Sets aDATEfield by index.Sets anINT32field annotatedDATE, as days since the Unix epoch.setDecimal(int fieldIndex, BigDecimal value) Sets aDECIMALfield by index.setDecimal(String name, BigDecimal value) Sets a field annotatedDECIMAL, in whichever ofINT32,INT64,BYTE_ARRAYorFIXED_LEN_BYTE_ARRAYthe column declares.setDouble(int fieldIndex, double value) Sets aDOUBLEfield by index.Sets aDOUBLEfield.setFloat(int fieldIndex, float value) Sets aFLOATfield by index.Sets aFLOATfield.setInt(int fieldIndex, int value) Sets anINT32field by index.Sets anINT32field.setInterval(int fieldIndex, PqInterval value) Sets anINTERVALfield by index.setInterval(String name, PqInterval value) Sets aFIXED_LEN_BYTE_ARRAY(12)field annotatedINTERVAL.setList(int fieldIndex, Consumer<ListBuilder> filler) Sets aLISTfield by index.setList(String name, Consumer<ListBuilder> filler) Sets aLISTfield, appending its entries throughfiller.setLocalTimestamp(int fieldIndex, LocalDateTime value) Sets a local-wall-clockTIMESTAMPfield by index.setLocalTimestamp(String name, LocalDateTime value) Sets anINT64field annotatedTIMESTAMPwithisAdjustedToUTC = false.setLong(int fieldIndex, long value) Sets anINT64field by index.Sets anINT64field.setMap(int fieldIndex, Consumer<MapBuilder> filler) Sets aMAPfield by index.setMap(String name, Consumer<MapBuilder> filler) Sets aMAPfield, appending its entries throughfiller.setNull(int fieldIndex) Sets a field null by index.Sets a field null.Sets aSTRING-shaped field by index.Sets aBYTE_ARRAYfield annotatedSTRING,ENUMorJSON, or unannotated, from the value's UTF-8 bytes.setStruct(int fieldIndex, Consumer<StructBuilder> filler) Sets a nested struct field by index.setStruct(String name, Consumer<StructBuilder> filler) Sets a nested struct field, populating it throughfiller.Sets aTIMEfield by index.Sets a field annotatedTIME, in the unit the annotation declares.setTimestamp(int fieldIndex, Instant value) Sets a UTC-adjustedTIMESTAMPfield by index.setTimestamp(String name, Instant value) Sets anINT64field annotatedTIMESTAMPwithisAdjustedToUTC = true, in the unit the annotation declares.Sets aUUIDfield by index.Sets aFIXED_LEN_BYTE_ARRAY(16)field annotatedUUID.
-
Method Details
-
setInt
Sets an
INT32field.The value is stored as declared: a
DATE,TIME(MILLIS)orDECIMALannotation on the column does not change what this setter writes, so it is the raw-value escape hatch alongsidesetDate(String, LocalDate)and friends — mirroring the reader, whosegetIntreturns the storedintwhatever the annotation. The value is still range-checked against what the annotation can hold: anINT(8)orINT(16)bounds it to that width, signed or unsigned, aTIMEto the times of day its unit can spell, and aDECIMALto the digits its precision declares.UINT_32bounds nothing, because every bit pattern is one of its values and a negativeintis the only way to spell one aboveInteger.MAX_VALUE.- Parameters:
name- the field namevalue- the value- Returns:
- this builder, for chaining
- Throws:
IllegalArgumentException- if the schema has no such field, the field is notINT32, the field is already set in this scope, or the value is out of range for the field's annotationIllegalStateException- if this builder's scope has ended
-
setLong
-
setFloat
-
setDouble
-
setBoolean
-
setString
Sets aBYTE_ARRAYfield annotatedSTRING,ENUMorJSON, or unannotated, from the value's UTF-8 bytes. Anullvalue sets the field null.- See Also:
-
setBinary
Sets aBYTE_ARRAYorFIXED_LEN_BYTE_ARRAYfield from its bytes, which are referenced rather than copied and must not be mutated until the record is written. AFIXED_LEN_BYTE_ARRAYfield requires exactly its declared length, and aDECIMALfield an unscaled value the declared precision holds. Anullvalue sets the field null.- See Also:
-
setDate
Sets anINT32field annotatedDATE, as days since the Unix epoch. Anullvalue sets the field null.- See Also:
-
setTime
Sets a field annotatedTIME, in the unit the annotation declares. A value carrying finer precision than the unit can hold is rejected, or narrowed, according to the configuredPrecisionLossPolicy. Anullvalue sets the field null.- See Also:
-
setTimestamp
Sets an
INT64field annotatedTIMESTAMPwithisAdjustedToUTC = true, in the unit the annotation declares. A value carrying finer precision than the unit can hold is rejected, or narrowed, according to the configuredPrecisionLossPolicy; one outside the range that unit spans is rejected under either. Anullvalue sets the field null.A local-wall-clock column —
isAdjustedToUTC = false— is written throughsetLocalTimestamp(String, LocalDateTime)instead, mirroring the reader's split betweengetTimestampandgetLocalTimestamp; using the wrong one throws rather than silently reinterpreting the instant.- See Also:
-
setLocalTimestamp
Sets anINT64field annotatedTIMESTAMPwithisAdjustedToUTC = false.- See Also:
-
setDecimal
Sets a field annotatedDECIMAL, in whichever ofINT32,INT64,BYTE_ARRAYorFIXED_LEN_BYTE_ARRAYthe column declares. The value is rescaled to the declared scale when that is lossless, and otherwise rejected or truncated according to the configuredPrecisionLossPolicy; its unscaled value must fit the declared precision under either. Anullvalue sets the field null.- See Also:
-
setUuid
Sets aFIXED_LEN_BYTE_ARRAY(16)field annotatedUUID. Anullvalue sets the field null.- See Also:
-
setInterval
Sets aFIXED_LEN_BYTE_ARRAY(12)field annotatedINTERVAL. Anullvalue sets the field null.- See Also:
-
setNull
Sets a field null. Equivalent to leaving it unset, and available so code that walks its fields uniformly can say so explicitly.- Parameters:
name- the field name- Returns:
- this builder, for chaining
- Throws:
IllegalArgumentException- if the schema has no such field, the field isREQUIRED, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setStruct
Sets a nested struct field, populating it throughfiller. Leaving the field unset writes a null struct; entering it with a filler that sets nothing writes a present struct whose own fields are null.- Parameters:
name- the field namefiller- populates the nested struct- Returns:
- this builder, for chaining
- Throws:
IllegalArgumentException- if the schema has no such field, the field is not a struct group, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setList
Sets aLISTfield, appending its entries throughfiller. An absent list — the field left unset, orsetNull(String)— and an empty list, whose filler appends nothing, are distinct and both writable.- Parameters:
name- the field namefiller- appends the list's entries- Returns:
- this builder, for chaining
- Throws:
IllegalArgumentException- if the schema has no such field, the field is not aLISTgroup, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setMap
Sets aMAPfield, appending its entries throughfiller. An absent map and an empty map are distinct and both writable.- Parameters:
name- the field namefiller- appends the map's entries- Returns:
- this builder, for chaining
- Throws:
IllegalArgumentException- if the schema has no such field, the field is not aMAPgroup, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
getFieldCount
int getFieldCount()The number of fields this struct declares — the record's top-level fields for the builderRowWriter.writeRow(Consumer)hands over, and a nested struct's own fields for the onesetStruct(String, Consumer)does. AMAPentry declares two,keyandvalue.- Returns:
- the field count
- Throws:
IllegalStateException- if this builder's scope has ended
-
getFieldName
The name of the field atfieldIndex, the inverse of the resolution the by-name setters perform.- Parameters:
fieldIndex- the field index,0-based- Returns:
- the field name
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalStateException- if this builder's scope has ended
-
setInt
Sets anINT32field by index. SeesetInt(String, int).- Parameters:
fieldIndex- the field index,0-basedvalue- the value- Returns:
- this builder, for chaining
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalArgumentException- if the field is notINT32, the field is already set in this scope, or the value is out of range for the field's annotationIllegalStateException- if this builder's scope has ended
-
setLong
Sets anINT64field by index. SeesetLong(String, long).- See Also:
-
setFloat
Sets aFLOATfield by index. SeesetFloat(String, float).- See Also:
-
setDouble
Sets aDOUBLEfield by index. SeesetDouble(String, double).- See Also:
-
setBoolean
Sets aBOOLEANfield by index. SeesetBoolean(String, boolean).- See Also:
-
setString
Sets aSTRING-shaped field by index. SeesetString(String, String).- See Also:
-
setBinary
Sets aBYTE_ARRAYorFIXED_LEN_BYTE_ARRAYfield by index. See [#setBinary(String, byte[])].- See Also:
-
setDate
Sets aDATEfield by index. SeesetDate(String, LocalDate).- See Also:
-
setTime
Sets aTIMEfield by index. SeesetTime(String, LocalTime).- See Also:
-
setTimestamp
Sets a UTC-adjustedTIMESTAMPfield by index. SeesetTimestamp(String, Instant).- See Also:
-
setLocalTimestamp
Sets a local-wall-clockTIMESTAMPfield by index. SeesetLocalTimestamp(String, LocalDateTime).- See Also:
-
setDecimal
Sets aDECIMALfield by index. SeesetDecimal(String, BigDecimal).- See Also:
-
setUuid
Sets aUUIDfield by index. SeesetUuid(String, UUID).- See Also:
-
setInterval
Sets anINTERVALfield by index. SeesetInterval(String, PqInterval).- See Also:
-
setNull
Sets a field null by index. SeesetNull(String).- Parameters:
fieldIndex- the field index,0-based- Returns:
- this builder, for chaining
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalArgumentException- if the field isREQUIRED, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setStruct
Sets a nested struct field by index. SeesetStruct(String, Consumer).- Parameters:
fieldIndex- the field index,0-basedfiller- populates the nested struct- Returns:
- this builder, for chaining
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalArgumentException- if the field is not a struct group, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setList
Sets aLISTfield by index. SeesetList(String, Consumer).- Parameters:
fieldIndex- the field index,0-basedfiller- appends the list's entries- Returns:
- this builder, for chaining
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalArgumentException- if the field is not aLISTgroup, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-
setMap
Sets aMAPfield by index. SeesetMap(String, Consumer).- Parameters:
fieldIndex- the field index,0-basedfiller- appends the map's entries- Returns:
- this builder, for chaining
- Throws:
IndexOutOfBoundsException- iffieldIndexis not in[0, getFieldCount())IllegalArgumentException- if the field is not aMAPgroup, or the field is already set in this scopeIllegalStateException- if this builder's scope has ended
-