-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#62 Added cli tool to export data in a csv format (#85)
* #62 Added cli tool to export data in a csv format * CR fixes
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. If not provided output will be printed on console | ||
#[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)?) | ||
} else { | ||
Box::new(io::stdout()) | ||
}; | ||
|
||
let reader = ArrowReaderBuilder::try_new(f)?.build(); | ||
let mut writer = csv::WriterBuilder::new() | ||
.with_header(true) | ||
.build(output_writer); | ||
|
||
for batch in reader.flatten() { | ||
writer.write(&batch)?; | ||
} | ||
|
||
Ok(()) | ||
} |