Class Row
- java.lang.Object
-
- org.apache.flink.types.Row
-
- All Implemented Interfaces:
Serializable
@PublicEvolving public final class Row extends Object implements Serializable
A row is a fixed-length, null-aware composite type for storing multiple values in a deterministic field order. Every field can be null regardless of the field's type. The type of row fields cannot be automatically inferred; therefore, it is required to provide type information whenever a row is produced.The main purpose of rows is to bridge between Flink's Table and SQL ecosystem and other APIs. Therefore, a row does not only consist of a schema part (containing the fields) but also attaches a
RowKindfor encoding a change in a changelog. Thus, a row can be considered as an entry in a changelog. For example, in regular batch scenarios, a changelog would consist of a bounded stream ofRowKind.INSERTrows. The row kind is kept separate from the fields and can be accessed by usinggetKind()andsetKind(RowKind).Fields of a row can be accessed either position-based or name-based. An implementer can decide in which field mode a row should operate during creation. Rows that were produced by the framework support a hybrid of both field modes (i.e. named positions):
Position-based field mode
withPositions(int)creates a fixed-length row. The fields can be accessed by position (zero-based) usinggetField(int)andsetField(int, Object). Every field is initialized withnullby default.Name-based field mode
withNames()creates a variable-length row. The fields can be accessed by name usinggetField(String)andsetField(String, Object). Every field is initialized during the first call tosetField(String, Object)for the given name. However, the framework will initialize missing fields withnulland reorder all fields once more type information is available during serialization or input conversion. Thus, even name-based rows eventually become fixed-length composite types with a deterministic field order. Name-based rows perform worse than position-based rows but simplify row creation and code readability.Hybrid / named-position field mode
Rows that were produced by the framework (after deserialization or output conversion) are fixed-length rows with a deterministic field order that can map static field names to field positions. Thus, fields can be accessed both via
getField(int)andgetField(String). BothsetField(int, Object)andsetField(String, Object)are supported for existing fields. However, adding new field names viasetField(String, Object)is not allowed. A hybrid row'sequals(Object)supports comparing to all kinds of rows. A hybrid row'shashCode()is only valid for position-based rows.A row instance is in principle
Serializable. However, it may contain non-serializable fields in which case serialization will fail if the row is not serialized with Flink's serialization stack.The
equals(Object)andhashCode()methods of this class support all external conversion classes of the table ecosystem.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears all fields of this row.static Rowcopy(Row row)Creates a new row which is copied from another row (including itsRowKind).booleanequals(Object o)intgetArity()Returns the number of fields in the row.ObjectgetField(int pos)Returns the field's content at the specified field position.ObjectgetField(String name)Returns the field's content using the specified field name.<T> TgetFieldAs(int pos)Returns the field's content at the specified field position.<T> TgetFieldAs(String name)Returns the field's content using the specified field name.Set<String>getFieldNames(boolean includeNamedPositions)Returns the set of field names if this row operates in name-based field mode, otherwise null.RowKindgetKind()Returns the kind of change that this row describes in a changelog.inthashCode()static Rowjoin(Row first, Row... remainings)Creates a new row with fields that are copied from the other rows and appended to the resulting row in the given order.static Rowof(Object... values)Creates a fixed-length row in position-based field mode and assigns the given values to the row's fields.static RowofKind(RowKind kind, Object... values)Creates a fixed-length row in position-based field mode with given kind and assigns the given values to the row's fields.static Rowproject(Row row, int[] fieldPositions)Creates a new row with projected fields and identicalRowKindfrom another row.static Rowproject(Row row, String[] fieldNames)Creates a new row with projected fields and identicalRowKindfrom another row.voidsetField(int pos, Object value)Sets the field's content at the specified position.voidsetField(String name, Object value)Sets the field's content using the specified field name.voidsetKind(RowKind kind)Sets the kind of change that this row describes in a changelog.StringtoString()static RowwithNames()Creates a variable-length row in name-based field mode.static RowwithNames(RowKind kind)Creates a variable-length row in name-based field mode.static RowwithPositions(int arity)Creates a fixed-length row in position-based field mode.static RowwithPositions(RowKind kind, int arity)Creates a fixed-length row in position-based field mode.
-
-
-
Constructor Detail
-
Row
public Row(RowKind kind, int arity)
Creates a fixed-length row in position-based field mode.The semantics are equivalent to
withPositions(RowKind, int). This constructor exists for backwards compatibility.- Parameters:
kind- kind of change a row describes in a changelogarity- the number of fields in the row
-
Row
public Row(int arity)
Creates a fixed-length row in position-based field mode.The semantics are equivalent to
withPositions(int). This constructor exists for backwards compatibility.- Parameters:
arity- the number of fields in the row
-
-
Method Detail
-
withPositions
public static Row withPositions(RowKind kind, int arity)
Creates a fixed-length row in position-based field mode.Fields can be accessed by position via
setField(int, Object)andgetField(int).See the class documentation of
Rowfor more information.- Parameters:
kind- kind of change a row describes in a changelogarity- the number of fields in the row- Returns:
- a new row instance
-
withPositions
public static Row withPositions(int arity)
Creates a fixed-length row in position-based field mode.Fields can be accessed by position via
setField(int, Object)andgetField(int).By default, a row describes an
RowKind.INSERTchange.See the class documentation of
Rowfor more information.- Parameters:
arity- the number of fields in the row- Returns:
- a new row instance
-
withNames
public static Row withNames(RowKind kind)
Creates a variable-length row in name-based field mode.Fields can be accessed by name via
setField(String, Object)andgetField(String).See the class documentation of
Rowfor more information.- Parameters:
kind- kind of change a row describes in a changelog- Returns:
- a new row instance
-
withNames
public static Row withNames()
Creates a variable-length row in name-based field mode.Fields can be accessed by name via
setField(String, Object)andgetField(String).By default, a row describes an
RowKind.INSERTchange.See the class documentation of
Rowfor more information.- Returns:
- a new row instance
-
getKind
public RowKind getKind()
Returns the kind of change that this row describes in a changelog.By default, a row describes an
RowKind.INSERTchange.- See Also:
RowKind
-
setKind
public void setKind(RowKind kind)
Sets the kind of change that this row describes in a changelog.By default, a row describes an
RowKind.INSERTchange.- See Also:
RowKind
-
getArity
public int getArity()
Returns the number of fields in the row.Note: The row kind is kept separate from the fields and is not included in this number.
- Returns:
- the number of fields in the row
-
getField
@Nullable public Object getField(int pos)
Returns the field's content at the specified field position.Note: The row must operate in position-based field mode.
- Parameters:
pos- the position of the field, 0-based- Returns:
- the field's content at the specified position
-
getFieldAs
public <T> T getFieldAs(int pos)
Returns the field's content at the specified field position.Note: The row must operate in position-based field mode.
This method avoids a lot of manual casting in the user implementation.
- Parameters:
pos- the position of the field, 0-based- Returns:
- the field's content at the specified position
-
getField
@Nullable public Object getField(String name)
Returns the field's content using the specified field name.Note: The row must operate in name-based field mode.
- Parameters:
name- the name of the field or null if not set previously- Returns:
- the field's content
-
getFieldAs
public <T> T getFieldAs(String name)
Returns the field's content using the specified field name.Note: The row must operate in name-based field mode.
This method avoids a lot of manual casting in the user implementation.
- Parameters:
name- the name of the field, set previously- Returns:
- the field's content
-
setField
public void setField(int pos, @Nullable Object value)Sets the field's content at the specified position.Note: The row must operate in position-based field mode.
- Parameters:
pos- the position of the field, 0-basedvalue- the value to be assigned to the field at the specified position
-
setField
public void setField(String name, @Nullable Object value)
Sets the field's content using the specified field name.Note: The row must operate in name-based field mode.
- Parameters:
name- the name of the fieldvalue- the value to be assigned to the field
-
getFieldNames
@Nullable public Set<String> getFieldNames(boolean includeNamedPositions)
Returns the set of field names if this row operates in name-based field mode, otherwise null.This method is a helper method for serializers and converters but can also be useful for other row transformations.
- Parameters:
includeNamedPositions- whether or not to include named positions when this row operates in a hybrid field mode
-
clear
public void clear()
Clears all fields of this row.
-
of
public static Row of(Object... values)
Creates a fixed-length row in position-based field mode and assigns the given values to the row's fields.This method should be more convenient than
withPositions(int)in many cases.For example:
Row.of("hello", true, 1L);instead ofRow row = Row.withPositions(3); row.setField(0, "hello"); row.setField(1, true); row.setField(2, 1L);By default, a row describes an
RowKind.INSERTchange.
-
ofKind
public static Row ofKind(RowKind kind, Object... values)
Creates a fixed-length row in position-based field mode with given kind and assigns the given values to the row's fields.This method should be more convenient than
withPositions(RowKind, int)in many cases.For example:
Row.ofKind(RowKind.INSERT, "hello", true, 1L);instead ofRow row = Row.withPositions(RowKind.INSERT, 3); row.setField(0, "hello"); row.setField(1, true); row.setField(2, 1L);
-
copy
public static Row copy(Row row)
Creates a new row which is copied from another row (including itsRowKind).This method does not perform a deep copy. Use
RowSerializer.copy(Row)if required.
-
project
public static Row project(Row row, int[] fieldPositions)
Creates a new row with projected fields and identicalRowKindfrom another row.This method does not perform a deep copy.
Note: The row must operate in position-based field mode. Field names are not projected.
- Parameters:
fieldPositions- field indices to be projected
-
project
public static Row project(Row row, String[] fieldNames)
Creates a new row with projected fields and identicalRowKindfrom another row.This method does not perform a deep copy.
Note: The row must operate in name-based field mode.
- Parameters:
fieldNames- field names to be projected
-
-