-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use renoir::prelude::*; | ||
|
||
fn main() { | ||
tracing_subscriber::fmt::fmt() | ||
.with_max_level(tracing::Level::DEBUG) | ||
.init(); | ||
let conf = RuntimeConfig::local(4).unwrap(); | ||
|
||
let ctx = StreamContext::new(conf.clone()); | ||
|
||
let dir = tempfile::tempdir().unwrap(); | ||
let dir_path = dir.path().to_path_buf(); | ||
eprintln!("Writing to {}", dir_path.display()); | ||
|
||
// Write to multiple files in parallel | ||
let mut path = dir_path.clone(); | ||
ctx.stream_par_iter(0..100) | ||
.map(|i| (i, format!("{i:08x}"))) | ||
.write_csv( | ||
move |i| { | ||
path.push(format!("{i:03}.csv")); | ||
path | ||
}, | ||
false, | ||
); | ||
|
||
ctx.execute_blocking(); | ||
|
||
let ctx = StreamContext::new(conf); | ||
let mut path = dir_path; | ||
path.push("001.csv"); | ||
ctx.stream_csv::<(i32, String)>(path) | ||
.for_each(|t| println!("{t:?}")); | ||
|
||
ctx.execute_blocking(); | ||
} |
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
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
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
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
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,127 @@ | ||
use serde::Serialize; | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::marker::PhantomData; | ||
use std::path::PathBuf; | ||
|
||
use crate::block::NextStrategy; | ||
use crate::operator::{ExchangeData, Operator}; | ||
use crate::scheduler::ExecutionMetadata; | ||
use crate::{CoordUInt, Replication, Stream}; | ||
|
||
use super::writer::{WriteOperator, WriterOperator}; | ||
|
||
// #[derive(Debug)] | ||
pub struct CsvWriteOp<T, N> { | ||
_t: PhantomData<T>, | ||
make_path: Option<N>, | ||
append: bool, | ||
path: Option<PathBuf>, | ||
/// Reader used to parse the CSV file. | ||
writer: Option<csv::Writer<BufWriter<File>>>, | ||
} | ||
|
||
impl<T, N> CsvWriteOp<T, N> | ||
where | ||
T: Serialize + Send, | ||
N: FnOnce(CoordUInt) -> PathBuf + Clone + Send + 'static, | ||
{ | ||
pub fn new(make_path: N, append: bool) -> Self { | ||
Self { | ||
_t: PhantomData, | ||
make_path: Some(make_path), | ||
append, | ||
path: None, | ||
writer: None, | ||
} | ||
} | ||
} | ||
|
||
impl<T, N> WriteOperator<T> for CsvWriteOp<T, N> | ||
where | ||
T: Serialize + Send, | ||
N: FnOnce(CoordUInt) -> PathBuf + Clone + Send + 'static, | ||
{ | ||
fn setup(&mut self, metadata: &ExecutionMetadata) { | ||
let id = metadata.global_id; | ||
self.path = Some(self.make_path.take().unwrap()(id)); | ||
|
||
tracing::debug!("Write csv to path {:?}", self.path.as_ref().unwrap()); | ||
let file = File::options() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.truncate(!self.append) | ||
.append(self.append) | ||
.open(self.path.as_ref().unwrap()) | ||
.unwrap_or_else(|err| { | ||
panic!( | ||
"CsvSource: error while opening file {:?}: {:?}", | ||
self.path, err | ||
) | ||
}); | ||
let file_len = file.metadata().unwrap().len(); | ||
|
||
let buf_writer = BufWriter::new(file); | ||
let csv_writer = csv::WriterBuilder::default() | ||
.has_headers(file_len == 0) | ||
.from_writer(buf_writer); | ||
|
||
self.writer = Some(csv_writer); | ||
} | ||
|
||
fn write(&mut self, item: T) { | ||
self.writer.as_mut().unwrap().serialize(item).unwrap(); | ||
} | ||
|
||
fn flush(&mut self) { | ||
self.writer.as_mut().unwrap().flush().ok(); | ||
} | ||
|
||
fn finalize(&mut self) { | ||
self.writer.take(); | ||
} | ||
} | ||
|
||
impl<T, N> Clone for CsvWriteOp<T, N> | ||
where | ||
N: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
_t: PhantomData, | ||
make_path: self.make_path.clone(), | ||
append: self.append, | ||
path: None, | ||
writer: None, | ||
} | ||
} | ||
} | ||
|
||
impl<Op: Operator> Stream<Op> | ||
where | ||
Op: 'static, | ||
Op::Out: Serialize + ExchangeData, | ||
{ | ||
pub fn write_csv<F: FnOnce(CoordUInt) -> PathBuf + Clone + Send + 'static>( | ||
self, | ||
make_path: F, | ||
append: bool, | ||
) { | ||
self.add_operator(|prev| { | ||
let writer = CsvWriteOp::new(make_path, append); | ||
WriterOperator { prev, writer } | ||
}) | ||
.finalize_block(); | ||
} | ||
|
||
pub fn write_csv_one<P: Into<PathBuf>>(self, path: P, append: bool) { | ||
let path = path.into(); | ||
self.repartition(Replication::One, NextStrategy::only_one()) | ||
.add_operator(|prev| { | ||
let writer = CsvWriteOp::new(move |_| path, append); | ||
WriterOperator { prev, writer } | ||
}) | ||
.finalize_block(); | ||
} | ||
} |
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,70 @@ | ||
use serde::Serialize; | ||
use std::fmt::Display; | ||
|
||
use crate::{ | ||
operator::{Operator, StreamElement}, | ||
structure::{OperatorKind, OperatorStructure}, | ||
ExecutionMetadata, | ||
}; | ||
|
||
pub trait WriteOperator<T: Serialize>: Clone + Send { | ||
fn setup(&mut self, metadata: &ExecutionMetadata); | ||
fn write(&mut self, item: T); | ||
fn flush(&mut self); | ||
fn finalize(&mut self); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct WriterOperator<W, Op> { | ||
pub(super) prev: Op, | ||
pub(super) writer: W, | ||
} | ||
|
||
impl<W, Op: Operator> Display for WriterOperator<W, Op> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"{} -> WriterSink<{}, {}>", | ||
self.prev, | ||
std::any::type_name::<W>(), | ||
std::any::type_name::<Op::Out>() | ||
) | ||
} | ||
} | ||
|
||
impl<W, Op> Operator for WriterOperator<W, Op> | ||
where | ||
Op: Operator, | ||
Op::Out: Serialize, | ||
W: WriteOperator<Op::Out>, | ||
{ | ||
type Out = (); | ||
|
||
fn setup(&mut self, metadata: &mut ExecutionMetadata) { | ||
self.prev.setup(metadata); | ||
self.writer.setup(metadata); | ||
} | ||
|
||
fn next(&mut self) -> StreamElement<()> { | ||
let el = self.prev.next(); | ||
let ret = el.variant(); | ||
match el { | ||
StreamElement::Item(item) | StreamElement::Timestamped(item, _) => { | ||
self.writer.write(item); | ||
} | ||
StreamElement::Watermark(_) => {} | ||
StreamElement::FlushBatch | StreamElement::FlushAndRestart => self.writer.flush(), | ||
StreamElement::Terminate => self.writer.finalize(), | ||
} | ||
ret | ||
} | ||
|
||
fn structure(&self) -> crate::structure::BlockStructure { | ||
let mut operator = OperatorStructure::new::<Op::Out, _>(format!( | ||
"WriterSink<{}>", | ||
std::any::type_name::<W>() | ||
)); | ||
operator.kind = OperatorKind::Sink; | ||
self.prev.structure().add_operator(operator) | ||
} | ||
} |
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