|
45 | 45 | //! - the [status summary][_impl::docs::status] for an overview over the
|
46 | 46 | //! supported Arrow and Rust constructs
|
47 | 47 | //!
|
48 |
| -//! ## Example |
| 48 | +//! ## `arrow` Example |
| 49 | +//! ```rust |
| 50 | +//! # use serde::{Deserialize, Serialize}; |
| 51 | +//! # #[cfg(feature = "has_arrow")] |
| 52 | +//! # fn main() -> serde_arrow::Result<()> { |
| 53 | +//! use arrow::datatypes::Schema; |
| 54 | +//! use arrow::record_batch::RecordBatch; |
| 55 | +//! use serde_arrow::schema::{TracingOptions, SerdeArrowSchema}; |
| 56 | +//! |
| 57 | +//! ##[derive(Serialize, Deserialize)] |
| 58 | +//! struct Record { |
| 59 | +//! a: f32, |
| 60 | +//! b: i32, |
| 61 | +//! } |
| 62 | +//! |
| 63 | +//! let records = vec![ |
| 64 | +//! Record { a: 1.0, b: 1 }, |
| 65 | +//! Record { a: 2.0, b: 2 }, |
| 66 | +//! Record { a: 3.0, b: 3 }, |
| 67 | +//! ]; |
| 68 | +//! |
| 69 | +//! // Determine Arrow schema |
| 70 | +//! let fields = Vec::<Field>::from_type::<Record>(TracingOptions::default())?; |
| 71 | +//! |
| 72 | +//! // Convert Rust records to Arrow arrays |
| 73 | +//! let arrays = serde_arrow::to_arrow(&fields, &records)?; |
| 74 | +//! |
| 75 | +//! // Create RecordBatch |
| 76 | +//! let schema = Schema::new(fields); |
| 77 | +//! let batch = RecordBatch::try_new(schema, arrays)?; |
| 78 | +//! # Ok(()) |
| 79 | +//! # } |
| 80 | +//! # #[cfg(not(feature = "has_arrow"))] |
| 81 | +//! # fn main() { } |
| 82 | +//! ``` |
| 83 | +//! |
| 84 | +//! The `RecordBatch` can then be written to disk, e.g., as parquet using |
| 85 | +//! the [`ArrowWriter`] from the [`parquet`] crate: |
| 86 | +//! |
| 87 | +//! [`ArrowWriter`]: https://docs.rs/parquet/latest/parquet/arrow/arrow_writer/struct.ArrowWriter.html |
| 88 | +//! [`parquet`]: https://docs.rs/parquet/latest/parquet/ |
| 89 | +//! |
| 90 | +//! ## `arrow2` Example |
49 | 91 | //!
|
50 | 92 | //! Requires one of `arrow2` feature (see below).
|
51 | 93 | //!
|
|
0 commit comments