简体   繁体   中英

How to convert arrow::Array to std::vector?

I have an Apache arrow array that is created by reading a file.

std::shared_ptr<arrow::Array> array;
PARQUET_THROW_NOT_OK(reader->ReadColumn(0, &array));

Is there a way to convert it to std::vector or any other native array type in C++?

You can use std::static_pointer_cast to cast the arrow::Array to, for example, an arrow::DoubleArray if the array contains doubles, and then use the Value function to get the value at a particular index. For example:

auto arrow_double_array = std::static_pointer_cast<arrow::DoubleArray>(array);
std::vector<double> double_vector;
for (int64_t i = 0; i < array->length(); ++i) 
{
    double_vector.push_back(arrow_double_array->Value(i));
}

See the latter part of the ColumnarTableToVector function in this example: https://arrow.apache.org/docs/cpp/examples/row_columnar_conversion.html . In that example, table->column(0)->chunk(0) is a std::shared_ptr<arrow::Array> .

To learn more, I found it useful to click on various parts of the inheritance diagram tree here: https://arrow.apache.org/docs/cpp/classarrow_1_1_flat_array.html . For example, strings in an arrow::StringArray are accessed using a GetString function instead of a Value function.

This is just what I've pieced together from these links, johnathan's comment above, and playing around with a small example myself, so I'm not sure if this is the best way, as I'm quite new to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM