-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
#[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()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's take advantage of the Result return type 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||||||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed