This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
main.rs
201 lines (181 loc) · 5.44 KB
/
main.rs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::fs::File;
use std::sync::Arc;
use std::{collections::HashMap, io::Read};
use arrow2::array::Array;
use arrow2::io::ipc::IpcField;
use arrow2::{
chunk::Chunk,
datatypes::{DataType, Schema},
error::Result,
io::{
json_integration::read,
json_integration::ArrowJson,
parquet::write::{
write_file, Compression, Encoding, RowGroupIterator, Version, WriteOptions,
},
},
};
use clap::{App, Arg};
use flate2::read::GzDecoder;
/// Read gzipped JSON file
pub fn read_gzip_json(
version: &str,
file_name: &str,
) -> Result<(Schema, Vec<IpcField>, Vec<Chunk<Arc<dyn Array>>>)> {
let path = format!(
"../testing/arrow-testing/data/arrow-ipc-stream/integration/{}/{}.json.gz",
version, file_name
);
let file = File::open(path).unwrap();
let mut gz = GzDecoder::new(&file);
let mut s = String::new();
gz.read_to_string(&mut s).unwrap();
// convert to Arrow JSON
let arrow_json: ArrowJson = serde_json::from_str(&s)?;
let schema = serde_json::to_value(arrow_json.schema).unwrap();
let (schema, ipc_fields) = read::deserialize_schema(&schema)?;
// read dictionaries
let mut dictionaries = HashMap::new();
if let Some(dicts) = arrow_json.dictionaries {
for json_dict in dicts {
// TODO: convert to a concrete Arrow type
dictionaries.insert(json_dict.id, json_dict);
}
}
let batches = arrow_json
.batches
.iter()
.map(|batch| read::deserialize_chunk(&schema, &ipc_fields, batch, &dictionaries))
.collect::<Result<Vec<_>>>()?;
Ok((schema, ipc_fields, batches))
}
fn main() -> Result<()> {
let matches = App::new("json-parquet-integration")
.arg(
Arg::with_name("json")
.long("json")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("write_path")
.long("output")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("version")
.long("version")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("projection")
.long("projection")
.required(false)
.takes_value(true),
)
.arg(
Arg::with_name("encoding-utf8")
.long("encoding-utf8")
.required(true)
.takes_value(true),
)
.get_matches();
let json_file = matches
.value_of("json")
.expect("must provide path to json file");
let write_path = matches
.value_of("write_path")
.expect("must provide path to write parquet");
let version = matches
.value_of("version")
.expect("must provide version of parquet");
let projection = matches.value_of("projection");
let utf8_encoding = matches
.value_of("encoding-utf8")
.expect("must provide utf8 type encoding");
let projection = projection.map(|x| {
x.split(',')
.map(|x| x.parse::<usize>().unwrap())
.collect::<Vec<_>>()
});
let (schema, _, batches) = read_gzip_json("1.0.0-littleendian", json_file)?;
let schema = if let Some(projection) = &projection {
let fields = schema
.fields
.iter()
.enumerate()
.filter_map(|(i, f)| {
if projection.contains(&i) {
Some(f.clone())
} else {
None
}
})
.collect::<Vec<_>>();
Schema::from(fields)
} else {
schema
};
let batches = if let Some(projection) = &projection {
batches
.iter()
.map(|batch| {
let columns = batch
.columns()
.iter()
.enumerate()
.filter_map(|(i, f)| {
if projection.contains(&i) {
Some(f.clone())
} else {
None
}
})
.collect();
Chunk::try_new(columns).unwrap()
})
.collect::<Vec<_>>()
} else {
batches
};
let version = if version == "1" {
Version::V1
} else {
Version::V2
};
let options = WriteOptions {
write_statistics: true,
compression: Compression::Uncompressed,
version,
};
let encodings = schema
.fields
.iter()
.map(|x| match x.data_type() {
DataType::Dictionary(..) => Encoding::RleDictionary,
DataType::Utf8 | DataType::LargeUtf8 => {
if utf8_encoding == "delta" {
Encoding::DeltaLengthByteArray
} else {
Encoding::Plain
}
}
_ => Encoding::Plain,
})
.collect();
let row_groups =
RowGroupIterator::try_new(batches.into_iter().map(Ok), &schema, options, encodings)?;
let parquet_schema = row_groups.parquet_schema().clone();
let mut writer = File::create(write_path)?;
let _ = write_file(
&mut writer,
row_groups,
&schema,
parquet_schema,
options,
None,
)?;
Ok(())
}