To convert row-wise data (like a list of objects) into an Apache Arrow Table, use specialized ArrayBuilder classes.
Key steps:
- Initialize a
MemoryPool (e.g., default_memory_pool()). - Create builders for each type (e.g.,
Int64Builder, DoubleBuilder, ListBuilder). - For nested structures like lists, use a
ListBuilder and a nested value builder (e.g., new DoubleBuilder(components_builder.value_builder())). - Iterate through your data, calling
.Append() or .AppendValues() on the builders. - Finalize the arrays using
.Finish(Array) and combine them into a Table using Table.Make(schema, ArrayVector).
// Example snippet for building a table
MemoryPool pool = default_memory_pool();
Int64Builder id_builder = new Int64Builder(int64(), pool);
DoubleBuilder cost_builder = new DoubleBuilder(float64(), pool);
ListBuilder components_builder = new ListBuilder(pool, new DoubleBuilder(float64(), pool));
DoubleBuilder cost_components_builder = new DoubleBuilder(components_builder.value_builder());
// ... loop and append data ...
Array id_array = new Array(null);
THROW_ON_FAILURE(id_builder.Finish(id_array));
// ... finish other arrays ...
FieldVector schema_vector = new FieldVector(
new Field("id", int64()),
new Field("cost", float64()),
new Field("cost_components", list(float64()))
);
Schema schema = new Schema(schema_vector);
table[0] = Table.Make(schema, new ArrayVector(id_array, cost_array, cost_components_array));