toSequenceOf
Converts this DataFrame into a Sequence of T instances, where T is given explicitly as the type argument of this call.
Use this function in most cases. The type argument of this DataFrame does not matter, so this function works on a DataFrame<*> too, and on a DataFrame whose type argument is generated by the compiler plugin.
The columns are matched and converted when this function is called, so the exceptions listed below are thrown right away. The T instances are created only when the sequence is iterated, one per row that is read, and each iteration creates them again.
The sequence has one T instance per row, in the order of the rows.
T has to be a data class. An interface does not work, even one marked with DataSchema. A generic data class works too, for example, Pair<String, Int>. Each instance is created by the primary constructor of T. Each constructor parameter gets the value of the column with the same name, or with the name given in ColumnName. Names are case-sensitive. The order of the columns does not matter, and columns that match no parameter are ignored. A parameter with a default value gets this value when there is no column for it.
When a column type differs from the parameter type, the values are converted the same way as in convertTo, for example, from Int to Long. A ColumnGroup becomes a nested data class, and a FrameColumn becomes a List of data class instances, or a DataFrame. A frame that goes into a DataFrame<S> parameter is converted to the schema of S the same way as in convertTo, and its other columns stay. A frame that goes into a DataFrame<*> parameter is passed as it is.
An exception is thrown when:
T is not a data class;
there is no column for a constructor parameter without a default value;
a column has
nullvalues, but its parameter is not nullable;a value cannot be converted to the parameter type;
a frame cannot be converted to the schema of its
DataFrame<S>parameter, for example, because a column ofSis missing.
For more information: See toSequence on the documentation website.
See also:
toDataFrame— the reverse operation: creates aDataFramefrom a list of objects.convertTo— changes theDataFrameto match the schema of T and returns a DataFrame instead.
Example
val df = dataFrameOf("name", "lastName", "age")("John", "Doe", 21)
.group("name", "lastName").into("fullName")
data class FullName(val name: String, val lastName: String)
data class Person(val fullName: FullName, val age: Int)
// The "fullName" column group becomes a nested `FullName`
df.toSequenceOf<Person>().first() // Person(fullName=FullName(name=John, lastName=Doe), age=21)Return
Type Parameters
The data class to create for each row.