Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parquet-read: add support to read parquet data from stdin #2482

Merged
merged 1 commit into from Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions parquet/src/bin/parquet-read.rs
Expand Up @@ -41,12 +41,13 @@ extern crate parquet;
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(short, long, help("Path to a parquet file"))]
#[clap(short, long, help("Path to a parquet file, or - for stdin"))]
file_name: String,
#[clap(
short,
Expand All @@ -66,10 +67,20 @@ fn main() {
let num_records = args.num_records;
let json = args.json;

let path = Path::new(&filename);
let file = File::open(&path).expect("Unable to open file");
let parquet_reader =
SerializedFileReader::new(file).expect("Failed to create reader");
let parquet_reader: Box<dyn FileReader> = if filename == "-" {
let mut buf = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may surprise people who aren't familiar with parquet that this will buffer the entire file in memory, as opposed to streaming it. Perhaps we could add a note to the help text to alert people?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @tustvold suggests, I think it is inevitable that reading from stdin requires buffering (because the parquet footer is at the end of the file and we need to get past all the data to get to the footer).

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
Expand Down