Skip to main content

parquet_schema/
parquet-schema.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Binary file to print the schema and metadata of a Parquet file.
6//!
7//! # Install
8//!
9//! `parquet-schema` can be installed using `cargo`:
10//! ```
11//! cargo install parquet --features=cli
12//! ```
13//! After this `parquet-schema` should be available:
14//! ```
15//! parquet-schema XYZ.parquet
16//! ```
17//!
18//! The binary can also be built from the source code and run as follows:
19//! ```
20//! cargo run --features=cli --bin parquet-schema XYZ.parquet
21//! ```
22//!
23//! Note that `verbose` is an optional boolean flag that allows to print schema only,
24//! when not provided or print full file metadata when provided.
25
26use clap::Parser;
27use parquet::{
28    file::reader::{FileReader, SerializedFileReader},
29    schema::printer::{print_file_metadata, print_parquet_metadata},
30};
31use std::{fs::File, path::Path};
32
33#[derive(Debug, Parser)]
34#[clap(author, version, about("Binary file to print the schema and metadata of a Parquet file"), long_about = None)]
35struct Args {
36    #[clap(help("Path to the parquet file"))]
37    file_path: String,
38    #[clap(short, long, help("Enable printing full file metadata"))]
39    verbose: bool,
40}
41
42fn main() {
43    let args = Args::parse();
44    let filename = args.file_path;
45    let path = Path::new(&filename);
46    let file = File::open(path).expect("Unable to open file");
47    let verbose = args.verbose;
48
49    match SerializedFileReader::new(file) {
50        Err(e) => panic!("Error when parsing Parquet file: {e}"),
51        Ok(parquet_reader) => {
52            let metadata = parquet_reader.metadata();
53            println!("Metadata for file: {filename}");
54            println!();
55            if verbose {
56                print_parquet_metadata(&mut std::io::stdout(), metadata);
57            } else {
58                print_file_metadata(&mut std::io::stdout(), metadata.file_metadata());
59            }
60        }
61    }
62}