1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::io::Write;
use parquet2::metadata::SchemaDescriptor;
use parquet2::write::RowGroupIter;
use parquet2::{metadata::KeyValue, write::WriteOptions};
use crate::datatypes::Schema;
use crate::error::{ArrowError, Result};
use super::{schema::schema_to_metadata_key, to_parquet_schema};
pub fn add_arrow_schema(
schema: &Schema,
key_value_metadata: Option<Vec<KeyValue>>,
) -> Option<Vec<KeyValue>> {
key_value_metadata
.map(|mut x| {
x.push(schema_to_metadata_key(schema));
x
})
.or_else(|| Some(vec![schema_to_metadata_key(schema)]))
}
pub struct FileWriter<W: Write> {
writer: parquet2::write::FileWriter<W>,
schema: Schema,
}
impl<W: Write> FileWriter<W> {
pub fn options(&self) -> &WriteOptions {
self.writer.options()
}
pub fn parquet_schema(&self) -> &SchemaDescriptor {
self.writer.schema()
}
pub fn schema(&self) -> &Schema {
&self.schema
}
}
impl<W: Write> FileWriter<W> {
pub fn try_new(writer: W, schema: Schema, options: WriteOptions) -> Result<Self> {
let parquet_schema = to_parquet_schema(&schema)?;
let created_by = Some("Arrow2 - Native Rust implementation of Arrow".to_string());
Ok(Self {
writer: parquet2::write::FileWriter::new(writer, parquet_schema, options, created_by),
schema,
})
}
pub fn start(&mut self) -> Result<()> {
Ok(self.writer.start()?)
}
pub fn write(
&mut self,
row_group: RowGroupIter<'_, ArrowError>,
num_rows: usize,
) -> Result<()> {
Ok(self.writer.write(row_group, num_rows)?)
}
pub fn end(self, key_value_metadata: Option<Vec<KeyValue>>) -> Result<(u64, W)> {
let key_value_metadata = add_arrow_schema(&self.schema, key_value_metadata);
Ok(self.writer.end(key_value_metadata)?)
}
}