parquet_schema/
parquet-schema.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 print the schema and metadata of a Parquet file.
19//!
20//! # Install
21//!
22//! `parquet-schema` can be installed using `cargo`:
23//! ```
24//! cargo install parquet --features=cli
25//! ```
26//! After this `parquet-schema` should be available:
27//! ```
28//! parquet-schema 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-schema XYZ.parquet
34//! ```
35//!
36//! Note that `verbose` is an optional boolean flag that allows to print schema only,
37//! when not provided or print full file metadata when provided.
38
39use clap::Parser;
40use parquet::{
41    file::reader::{FileReader, SerializedFileReader},
42    schema::printer::{print_file_metadata, print_parquet_metadata},
43};
44use std::{fs::File, path::Path};
45
46#[derive(Debug, Parser)]
47#[clap(author, version, about("Binary file to print the schema and metadata of a Parquet file"), long_about = None)]
48struct Args {
49    #[clap(help("Path to the parquet file"))]
50    file_path: String,
51    #[clap(short, long, help("Enable printing full file metadata"))]
52    verbose: bool,
53}
54
55fn main() {
56    let args = Args::parse();
57    let filename = args.file_path;
58    let path = Path::new(&filename);
59    let file = File::open(path).expect("Unable to open file");
60    let verbose = args.verbose;
61
62    match SerializedFileReader::new(file) {
63        Err(e) => panic!("Error when parsing Parquet file: {e}"),
64        Ok(parquet_reader) => {
65            let metadata = parquet_reader.metadata();
66            println!("Metadata for file: {}", &filename);
67            println!();
68            if verbose {
69                print_parquet_metadata(&mut std::io::stdout(), metadata);
70            } else {
71                print_file_metadata(&mut std::io::stdout(), metadata.file_metadata());
72            }
73        }
74    }
75}