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

#62 Added cli tool to export data in a csv format #85

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.70"

[dependencies]
anyhow = { version = "1.0", optional = true }
arrow = { version = "51", features = ["prettyprint"] }
arrow = { version = "51", features = ["prettyprint", "chrono-tz"] }
bytes = "1.4"
chrono = { version = "0.4.37", default-features = false, features = ["std"] }
chrono-tz = "0.8.6"
Expand Down Expand Up @@ -72,6 +72,10 @@ path = "./examples/datafusion_integration.rs"
name = "orc-metadata"
required-features = ["cli"]

[[bin]]
name = "orc-export"
required-features = ["cli"]

[[bin]]
name = "orc-stats"
required-features = ["cli"]
42 changes: 42 additions & 0 deletions src/bin/orc-export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{fs::File, io, path::PathBuf};

use anyhow::Result;
use arrow::csv;
use clap::Parser;
use orc_rust::ArrowReaderBuilder;

#[derive(Parser)]
#[command(name = "orc-export")]
#[command(version, about = "Export data from orc file to csv", long_about = None)]
struct Cli {
/// Path to the orc file
file: PathBuf,
/// Output file
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe need to enhance doc here to indicate it outputs to stdout if not provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

#[arg(short, long)]
output: Option<PathBuf>,
// TODO: head=N
// TODO: convert_dates
// TODO: format=[csv|json]
// TODO: columns="col1,col2"
}

fn main() -> Result<()> {
let cli = Cli::parse();
let f = File::open(&cli.file)?;
let output_writer: Box<dyn io::Write> = if let Some(output) = cli.output {
Box::new(File::create(output).unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Box::new(File::create(output).unwrap())
Box::new(File::create(output)?)

Let's take advantage of the Result return type 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

} else {
Box::new(io::stdout())
};

let reader = ArrowReaderBuilder::try_new(f).unwrap().build();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let reader = ArrowReaderBuilder::try_new(f).unwrap().build();
let reader = ArrowReaderBuilder::try_new(f)?.build();

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

let mut writer = csv::WriterBuilder::new()
.with_header(true)
.build(output_writer);

for batch in reader.flatten() {
writer.write(&batch)?;
}

Ok(())
}
Loading