c++ - 如何读取ORC文件列数据
问题描述
我已经下载了 ORC c++ API 并在我的 Ubuntu 上构建了它。现在我正在尝试批量读取其列数据。在this reference中提到orc::ColumnVectorBatch
可以dynamic_cast
对特定的列数据类型进行批处理,例如:orc::Decimal64VectorBatch
。但它没有将空指针作为动态转换结果。下面是我的代码:
// Orc Reader.
#include <memory>
#include <iostream>
#include <vector>
#include <list>
#include <fstream>
#include <orc/Reader.hh>
#include <orc/ColumnPrinter.hh>
#include <orc/Exceptions.hh>
#include <orc/OrcFile.hh>
int main(int argc, char const *argv[])
{
std::list<uint64_t> read_cols = {4};
std::string file_path = "~/trades_data.zlib.orc";
std::ifstream in_file(file_path.c_str(), std::ios::binary);
in_file.seekg(0, std::ios::end);
int file_size = in_file.tellg();
std::cout << "Size of the file is" << " " << file_size << " " << "bytes";
orc::RowReaderOptions row_reader_opts;
row_reader_opts.include(read_cols);
orc::ReaderOptions reader_opts;
std::unique_ptr<orc::Reader> reader;
std::unique_ptr<orc::RowReader> row_reader;
reader = orc::createReader(orc::readFile(file_path), reader_opts);
row_reader = reader->createRowReader(row_reader_opts);
std::unique_ptr<orc::ColumnVectorBatch> batch = row_reader->createRowBatch(1000);
while (row_reader->next(*batch))
{
// BELOW LINE OF CODE IS GIVING NULLPOINTER.
orc::Decimal64VectorBatch *dec_vec = dynamic_cast<orc::Decimal64VectorBatch*>(batch.get());
}
return 0;
}
如果有人能指出错误,这对我来说真的是一个很大的帮助。
解决方案
我不久前已经解决了这个问题,现在我正在为我自己的问题写一个答案。希望也有助于您的代码。在上面的代码中,它试图将它读取的浴转换row_reader
成,orc::Decimal64VectorBatch
但批次应该首先被转换成orc::StructVectorBatch
. 然后使用列的索引数可以很容易地将其转换为所需的列数据。
const int time_idx = 0; // Index of column containing time in decimal64 format.
while (row_reader->next(*batch))
{
// Now batch should initially convert into StructVectorBatc.
const auto &struct_batch = dynamic_cast<const orc::StructVectorBatch&>(*batch.get());
// And then struct_batch can be converted into required column data format.
const auto &dec_vec = dynamic_cast<orc::Decimal64VectorBatch&>(*(struct_batch.fields[time_idx)).values.data();
}
推荐阅读
- r - 如何提取具有特定颜色的所有单元格值(以及它们的单元格 ID)?
- vue.js - 使用 v-select 下拉菜单作为默认值
- sql - 如何从 SQL Server CE 中的另一个表中选择最大值和最小值
- python - 如何绘制具有固定边界的波动方程
- c# - 如何在 html 数据表视图中显示 IEnumerable 数据分组 ID?
- compiler-errors - 在树莓派零上工作。显示错误。在编译 opencv 时,我使用 make ~j4 而不是 make。是这个原因吗?
- c++ - 如何总结各种继承类C++的std :: list?
- python - 如何添加时间到日期时间?
- powerbi - 在 DAX 的过滤器函数中动态使用日期的最佳方法是什么
- javascript - 将博客文章作为纯文本存储在 mongodb 中