@@ -32,7 +32,53 @@ arrays, and deserialization from arrays to Rust structs.
32
32
[ datafusion ] : https://github.com/apache/arrow-datafusion/
33
33
34
34
## Example
35
+ Given this Rust structure
36
+ ``` rust
37
+ #[derive(Serialize , Deserialize )]
38
+ struct Record {
39
+ a : f32 ,
40
+ b : i32 ,
41
+ }
35
42
43
+ let records = vec! [
44
+ Record { a : 1.0 , b : 1 },
45
+ Record { a : 2.0 , b : 2 },
46
+ Record { a : 3.0 , b : 3 },
47
+ ];
48
+ ```
49
+
50
+ ### Serialize to ` arrow ` ` RecordBatch `
51
+ ``` rust
52
+ use serde_arrow :: schema :: {TracingOptions , SerdeArrowSchema };
53
+
54
+ // Determine Arrow schema
55
+ let fields =
56
+ SerdeArrowSchema :: from_type :: <Record >(TracingOptions :: default ())?
57
+ . to_arrow_fields ()
58
+
59
+ // Convert to Arrow arrays
60
+ let arrays = serde_arrow :: to_arrow (& fields , & records )? ;
61
+
62
+ // Form a RecordBatch
63
+ let schema = Schema :: new (& fields );
64
+ let batch = RecordBatch :: try_new (schema . into (), arrays )? ;
65
+ ```
66
+
67
+ This ` RecordBatch ` can now be written to disk using [ ArrowWriter] from the [ parquet] crate.
68
+
69
+ [ ArrowWriter ] : https://docs.rs/parquet/latest/parquet/arrow/arrow_writer/struct.ArrowWriter.html
70
+ [ parquet ] : https://docs.rs/parquet/latest/parquet/
71
+
72
+
73
+ ``` rust
74
+ let file = File :: create (" example.pq" );
75
+ let mut writer = ArrowWriter :: try_new (file , batch . schema (), None )? ;
76
+ writer . write (& batch )? ;
77
+ writer . close ()? ;
78
+ ```
79
+
80
+
81
+ ### Serialize to ` arrow2 ` arrays
36
82
``` rust
37
83
use serde_arrow :: schema :: {TracingOptions , SerdeArrowSchema };
38
84
@@ -69,16 +115,32 @@ write_chunk(
69
115
)?;
70
116
```
71
117
118
+ ### Usage from python
119
+
72
120
The written file can now be read in Python via
73
121
74
122
``` python
75
123
# using polars
76
- import polars as pl
77
- pl.read_parquet(" example.pq" )
124
+ >> > import polars as pl
125
+ >> > pl.read_parquet(" example.pq" )
126
+ shape: (3 , 2 )
127
+ ┌─────┬─────┐
128
+ │ a ┆ b │
129
+ │ -- - ┆ -- - │
130
+ │ f32 ┆ i32 │
131
+ ╞═════╪═════╡
132
+ │ 1.0 ┆ 1 │
133
+ │ 2.0 ┆ 2 │
134
+ │ 3.0 ┆ 3 │
135
+ └─────┴─────┘
78
136
79
137
# using pandas
80
- import pandas as pd
81
- pd.read_parquet(" example.pq" )
138
+ >> > import pandas as pd
139
+ >> > pd.read_parquet(" example.pq" )
140
+ a b
141
+ 0 1.0 1
142
+ 1 2.0 2
143
+ 2 3.0 3
82
144
```
83
145
84
146
[ arrow2-guide ] : https://jorgecarleitao.github.io/arrow2
0 commit comments