parquet_rowcount/parquet-rowcount.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Binary file to return the number of rows found from Parquet file(s).
19//!
20//! # Install
21//!
22//! `parquet-rowcount` can be installed using `cargo`:
23//! ```
24//! cargo install parquet --features=cli
25//! ```
26//! After this `parquet-rowcount` should be available:
27//! ```
28//! parquet-rowcount XYZ.parquet
29//! ```
30//!
31//! The binary can also be built from the source code and run as follows:
32//! ```
33//! cargo run --features=cli --bin parquet-rowcount XYZ.parquet ABC.parquet ZXC.parquet
34//! ```
35//!
36//! Note that `parquet-rowcount` reads full file schema, no projection or filtering is
37//! applied.
38
39use clap::Parser;
40use parquet::file::reader::{FileReader, SerializedFileReader};
41use std::{fs::File, path::Path};
42
43#[derive(Debug, Parser)]
44#[clap(author, version, about("Binary file to return the number of rows found from Parquet file(s)"), long_about = None)]
45struct Args {
46 #[clap(
47 number_of_values(1),
48 help("List of Parquet files to read from separated by space")
49 )]
50 file_paths: Vec<String>,
51}
52
53fn main() {
54 let args = Args::parse();
55
56 for filename in args.file_paths {
57 let path = Path::new(&filename);
58 let file = File::open(path).expect("Unable to open file");
59 let parquet_reader = SerializedFileReader::new(file).expect("Unable to read file");
60 let row_group_metadata = parquet_reader.metadata().row_groups();
61 let mut total_num_rows = 0;
62
63 for group_metadata in row_group_metadata {
64 total_num_rows += group_metadata.num_rows();
65 }
66
67 eprintln!("File {filename}: rowcount={total_num_rows}");
68 }
69}