Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Created December 19, 2021 23:02
Show Gist options
  • Select an option

  • Save jayhuang75/fad56a7a3c7c06d841fa2412367d7604 to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/fad56a7a3c7c06d841fa2412367d7604 to your computer and use it in GitHub Desktop.
go-train-delay-ml-pipeline-data-extract
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