Created
December 19, 2021 23:02
-
-
Save jayhuang75/fad56a7a3c7c06d841fa2412367d7604 to your computer and use it in GitHub Desktop.
go-train-delay-ml-pipeline-data-extract
This file contains hidden or 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
| async fn extract(&self) -> Result<String, AppError> { | |
| let stmt = &self | |
| .client | |
| .prepare("COPY (SELECT * From delays) TO STDOUT (FORMAT CSV, HEADER TRUE, quote ' ')") | |
| .await?; | |
| let data = self | |
| .client | |
| .copy_out(stmt) | |
| .await? | |
| .try_fold(BytesMut::new(), |mut buf, chunk| async move { | |
| buf.extend_from_slice(&chunk); | |
| Ok(buf) | |
| }) | |
| .await?; | |
| let file_name = format!( | |
| "./data/{}.csv", | |
| chrono::offset::Utc::now().date().to_string() | |
| ); | |
| let mut writer = csv::WriterBuilder::new() | |
| .quote_style(csv::QuoteStyle::Never) | |
| .from_path(file_name.clone())?; | |
| writer.write_field(data)?; | |
| writer.flush()?; | |
| Ok(file_name) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment