[英]How to convert arrow::Array to std::vector?
I have an Apache arrow array that is created by reading a file.我有一个通过读取文件创建的 Apache 箭头数组。
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++?有没有办法将其转换为 std::vector 或 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.如果数组包含双精度值,您可以使用
std::static_pointer_cast
将arrow::Array
std::static_pointer_cast
转换为例如arrow::DoubleArray
,然后使用Value
函数获取特定索引处的值。 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 .请参阅本示例中
ColumnarTableToVector
函数的后半部分: 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>
.在该示例中,
table->column(0)->chunk(0)
是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 .要了解更多信息,我发现在此处单击继承图树的各个部分很有用: 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.例如,使用
GetString
函数而不是Value
函数访问arrow::StringArray
中的字符串。
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.这只是我从这些链接、上面约翰纳森的评论以及我自己的一个小例子中拼凑出来的,所以我不确定这是否是最好的方法,因为我对此很陌生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.