问题描述
我正在尝试使用Apache Arrow提供的C ++ StreamWriter
类。
使用StreamWriter
的唯一示例是使用底层Parquet API,即
parquet::schema::NodeVector fields;
fields.push_back(parquet::schema::PrimitiveNode::Make(
"string_field",parquet::Repetition::OPTIONAL,parquet::Type::BYTE_ARRAY,parquet::ConvertedType::UTF8));
fields.push_back(parquet::schema::PrimitiveNode::Make(
"char_field",parquet::Repetition::required,parquet::Type::FIXED_LEN_BYTE_ARRAY,parquet::ConvertedType::NONE,1));
auto node = std::static_pointer_cast<parquet::schema::GroupNode>(
parquet::schema::GroupNode::Make("schema",fields));
最终结果是一个std::shared_ptr<parquet::schema::GroupNode>
,然后可以将其传递到StreamWriter
。
是否可以通过StreamWriter
构建和使用“高级”箭头模式?使用WriteTable
函数(非流式处理)时支持它们,但是我没有找到将其与流式API一起使用的示例。
我当然可以求助于使用低级API,但是在创建大型而复杂的架构时它非常冗长,我更愿意(但不需要)使用高级Arrow架构机制。
例如,
std::shared_ptr<arrow::io::FileOutputStream> outfile_;
PARQUET_ASSIGN_OR_THROW(outfile_,arrow::io::FileOutputStream::Open("test.parquet"));
// construct an arrow schema
auto schema = arrow::schema({arrow::field("field1",arrow::int64()),arrow::field("field2",arrow::float64()),arrow::field("field3",arrow::float64())});
// build the writer properties
parquet::WriterProperties::Builder builder;
auto properties = builder.build()
// my current best attempt at converting the Arrow schema to a Parquet schema
std::shared_ptr<parquet::SchemaDescriptor> parquet_schema;
parquet::arrow::ToParquetSchema(schema.get(),*properties,&parquet_schema); // parquet_schema is Now populated
// and Now I try and build the writer - this fails
auto writer = parquet::ParquetFileWriter::Open(outfile_,parquet_schema->group_node(),properties);
最后一行失败,因为parquet_schema->group_node()
(这是我知道的访问架构的GroupNode
的唯一方法)返回const GroupNode*
,而{{1} }需要ParquetFileWriter::Open)
。
我不确定抛弃返回的组节点的常数并将其强制进入std::shared_ptr<GroupNode>
调用是否是::open()
的官方支持(或正确使用)。
我想做些什么吗?
解决方法
看来你需要为StreamWriter使用低级api。
一个非常棘手的方法:
auto writer = parquet::ParquetFileWriter::Open(outfile_,std::shared_ptr<parquet::schema::GroupNode>(const_cast<parquet::schema::GroupNode *>(parquet_schema->group_node())),properties);
您可能需要手动转换架构。
源代码 cpp/src/parquet/arrow/schema.cc
可能对您有所帮助。
PARQUET_EXPORT
::arrow::Status ToParquetSchema(const ::arrow::Schema* arrow_schema,const WriterProperties& properties,std::shared_ptr<SchemaDescriptor>* out);