-
Notifications
You must be signed in to change notification settings - Fork 785
/
parquet-read.rs
106 lines (96 loc) · 3.21 KB
/
parquet-read.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Binary file to read data from a Parquet file.
//!
//! # Install
//!
//! `parquet-read` can be installed using `cargo`:
//! ```
//! cargo install parquet --features=cli
//! ```
//! After this `parquet-read` should be available:
//! ```
//! parquet-read XYZ.parquet
//! ```
//!
//! The binary can also be built from the source code and run as follows:
//! ```
//! cargo run --features=cli --bin parquet-read XYZ.parquet
//! ```
//!
//! Note that `parquet-read` reads full file schema, no projection or filtering is
//! applied.
use clap::Parser;
use parquet::file::reader::{FileReader, SerializedFileReader};
use parquet::record::Row;
use std::io::{self, Read};
use std::{fs::File, path::Path};
#[derive(Debug, Parser)]
#[clap(author, version, about("Binary file to read data from a Parquet file"), long_about = None)]
struct Args {
#[clap(help("Path to a parquet file, or - for stdin"))]
file_name: String,
#[clap(
short,
long,
default_value_t = 0_usize,
help("Number of records to read. When not provided or 0, all records are read")
)]
num_records: usize,
#[clap(short, long, help("Print Parquet file in JSON lines format"))]
json: bool,
}
fn main() {
let args = Args::parse();
let filename = args.file_name;
let num_records = args.num_records;
let json = args.json;
let parquet_reader: Box<dyn FileReader> = if filename == "-" {
let mut buf = Vec::new();
io::stdin()
.read_to_end(&mut buf)
.expect("Failed to read stdin into a buffer");
Box::new(
SerializedFileReader::new(bytes::Bytes::from(buf)).expect("Failed to create reader"),
)
} else {
let path = Path::new(&filename);
let file = File::open(path).expect("Unable to open file");
Box::new(SerializedFileReader::new(file).expect("Failed to create reader"))
};
// Use full schema as projected schema
let mut iter = parquet_reader
.get_row_iter(None)
.expect("Failed to create row iterator");
let mut start = 0;
let end = num_records;
let all_records = end == 0;
while all_records || start < end {
match iter.next() {
Some(row) => print_row(&row.unwrap(), json),
None => break,
};
start += 1;
}
}
fn print_row(row: &Row, json: bool) {
if json {
println!("{}", row.to_json_value())
} else {
println!("{row}");
}
}