3个版本
| 0.0.3 | 2024年3月29日 |
|---|---|
| 0.0.2 | 2024年3月29日 |
| 0.0.1 | 2024年3月27日 |
68 in #prost
在prost-arrow中使用
15KB
245 行
PROST! Apache Arrow支持
prost-arrow 提供了一个 derive 特质,可以用来为使用 prost 生成的任何 protobuf 类型生成 arrow 数组构建器。
用法
此crate提供了 ToArrow 特质和一个派生它的进程宏。它必须在 所有 消息上派生,因此我们将其作为 type_attribute 添加,使用通配符路径 "."。生成的 impls 依赖于 prost-arrow crate 以及几个 arrow crate。
您需要在您的Cargo.toml中添加以下依赖项
arrow-array
arrow-buffer
arrow-schema
prost-arrow
在您的构建脚本中
// prost
prost_build::Config::new()
.type_attribute(".", "#[derive(::prost_arrow::ToArrow)]")
.compile_protos(&["proto/routeguide/route_guide.proto"], &["proto/"])
.unwrap();
// tonic
tonic_build::configure()
.type_attribute(".", "#[derive(::prost_arrow::ToArrow)]")
.compile(&["proto/routeguide/route_guide.proto"], &["proto"])
.unwrap();
最后,要访问生成的prost类型数组的构建器,我们使用 prost_arrow::new_builder<T>,其中 T 是某个具有派生 ToArrow 类型的prost生成的类型。返回的构建器将实现基础 arrow_builder::Builder 特质,但还将具有接受我们的prost类型 T 的 append_value 和 append_option 方法。
// required trait imports
use arrow_array::builder::ArrayBuilder;
use prost_arrow::{ArrowBuilder, ToArrow};
// Rectangle is a prost-generated struct that has ToArrow derived.
let mut builder = prost_arrow::new_builder::<Rectangle>();
builder.append_value(Rectangle {
lo: Some(pt_1),
hi: None,
messages: vec!["one".to_string(), "two".to_string()],
extra_points: vec![
Point {
latitude: 1,
longitude: 2,
},
Point {
latitude: 3,
longitude: 4,
},
],
binary: vec![0, 1, 2, 3],
repeated_binary: vec![vec![10, 100]],
});
构建器可以像任何其他arrow构建器实现类型一样使用,因此可以使用 finish 或 finish_cloned 方法最终确定arrow数组(在我们的情况下,是一个结构体数组)。
// finish the array builder to get an ArrayRef
let arr = builder.finish();
// downcast the array into StructArray
let struct_arr = arr.as_any().downcast_ref::<StructArray>().unwrap();
// convert to RecordBatch if desired
let record_batch: RecordBatch = struct_arr.into();
完整性
| 功能 | 支持 |
|---|---|
| 基本类型 | ✅ |
| 重复字段 | ✅ |
可选字段(通过 optional) |
✅ |
| 可选字段(通过包装类型) | 🚧 |
| 已知类型(例如时间戳) | 🚧 |
| oneof字段 | 🚧 |
| 映射字段 | 🚧 |
| 嵌套消息 | ✅ |
| 递归/循环消息 | ❌ |
依赖关系
~270–720KB
~17K SLoC