Skip to main content

parquet_read/
parquet-read.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 read data from a Parquet file.
6//!
7//! # Install
8//!
9//! `parquet-read` can be installed using `cargo`:
10//! ```
11//! cargo install parquet --features=cli
12//! ```
13//! After this `parquet-read` should be available:
14//! ```
15//! parquet-read 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-read XYZ.parquet
21//! ```
22//!
23//! Note that `parquet-read` reads full file schema, no projection or filtering is
24//! applied.
25
26use clap::Parser;
27use parquet::file::reader::{FileReader, SerializedFileReader};
28use parquet::record::Row;
29use std::io::{self, Read};
30use std::{fs::File, path::Path};
31
32#[derive(Debug, Parser)]
33#[clap(author, version, about("Binary file to read data from a Parquet file"), long_about = None)]
34struct Args {
35    #[clap(help("Path to a parquet file, or - for stdin"))]
36    file_name: String,
37    #[clap(
38        short,
39        long,
40        default_value_t = 0_usize,
41        help("Number of records to read. When not provided or 0, all records are read")
42    )]
43    num_records: usize,
44    #[clap(short, long, help("Print Parquet file in JSON lines format"))]
45    json: bool,
46}
47
48fn main() {
49    let args = Args::parse();
50
51    let filename = args.file_name;
52    let num_records = args.num_records;
53    let json = args.json;
54
55    let parquet_reader: Box<dyn FileReader> = if filename == "-" {
56        let mut buf = Vec::new();
57        io::stdin()
58            .read_to_end(&mut buf)
59            .expect("Failed to read stdin into a buffer");
60        Box::new(
61            SerializedFileReader::new(bytes::Bytes::from(buf)).expect("Failed to create reader"),
62        )
63    } else {
64        let path = Path::new(&filename);
65        let file = File::open(path).expect("Unable to open file");
66        Box::new(SerializedFileReader::new(file).expect("Failed to create reader"))
67    };
68
69    // Use full schema as projected schema
70    let mut iter = parquet_reader
71        .get_row_iter(None)
72        .expect("Failed to create row iterator");
73
74    let mut start = 0;
75    let end = num_records;
76    let all_records = end == 0;
77
78    while all_records || start < end {
79        match iter.next() {
80            Some(row) => print_row(&row.unwrap(), json),
81            None => break,
82        }
83        start += 1;
84    }
85}
86
87fn print_row(row: &Row, json: bool) {
88    if json {
89        println!("{}", row.to_json_value())
90    } else {
91        println!("{row}");
92    }
93}