使用Arrow schema与Parquet StreamWriter。

5

我试图使用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", parquet::Repetition::REQUIRED, fields));


最终结果是一个std::shared_ptr<parquet::schema::GroupNode>,可以将其传递给StreamWriter
使用 WriteTable 函数(非流式)时,可以使用“高级”Arrow模式吗?但是在使用流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);


最后一行代码失败了,因为我所知道的唯一获取架构GroupNode访问权限的方式parquet_schema->group_node()返回了一个const GroupNode*,而ParquetFileWriter::Open()需要一个std::shared_ptr<GroupNode>

我不确定去除返回的组节点的常量性并将其强制转换为::Open()调用是否是StreamWriter的官方支持(或正确)的用法。
我想要做的这件事是否可能?
1个回答

0

看起来你需要使用低级别的API来进行StreamWriter。

一个非常巧妙的方法:

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接