3个版本
| 0.0.3 | 2024年3月29日 |
|---|---|
| 0.0.2 | 2024年3月29日 |
| 0.0.1 | 2024年3月27日 |
#29 in #prost
14KB
264 行
PROST! Apache Arrow支持
prost-arrow提供了一个derive特质,可用于为使用prost生成的任何protobuf类型生成arrow数组构建器。
用法
此crate提供了ToArrow特质和一个派生它的proc-macro。它必须在所有消息上派生,所以我们将其作为type_attribute添加,路径为通配符"."。生成的impls依赖于prost-arrowcrate以及几个arrowcrate。
您需要在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>,对于具有派生的ToArrow类型的某些prost生成的类型T。返回的构建器将实现基础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]],
});
构建器可以像其他任何箭头构建器实现类型一样使用,因此可以使用finish或finish_cloned方法最终确定箭头数组(在我们的情况下,是结构数组)。
// 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字段 | 🚧 |
| 映射字段 | 🚧 |
| 嵌套消息 | ✅ |
| 递归/循环消息 | ❌ |
依赖项
~5–11MB
~121K SLoC