How do I compute window statistics without loading the whole stream?
Available — v0.0.1 (experimental)r
A moving average in R
std:window cuts the stream into windows of three; an R script answers one window at a time. Memory is bounded by the window size.
Available in v0.0.1: the files, README and tests are in examples/r-moving-window of the openflange-go repository. Commands run from the repository root.
Needs: Rscript with the jsonlite package.
A pipe is a data-processing step with declared inputs and outputs.
What goes in, what comes out
Input (stdin)
{"value":10}
{"value":20}
{"value":30}
{"value":40}
{"value":50}Command
printf '%s\n' '{"value":10}' '{"value":20}' '{"value":30}' '{"value":40}' '{"value":50}' | openflange run ./examples/r-moving-window --grant subprocessOutput (stdout)
{"index":0,"start":0,"end":2,"count":3,"mean":20}
{"index":1,"start":1,"end":3,"count":3,"mean":30}
{"index":2,"start":2,"end":4,"count":3,"mean":40}Files and further commands
examples/r-moving-window/pipeline.yaml
# records ─▶ std:window (sliding) ─▶ window-mean (R, one frame at a time) ─▶ means
spec: openflange.org/pipeline/v0.1
nodes:
window:
pipe: std:window
inputs:
in: {from: inputs.records}
parameters:
size: {$param: size}
step: {$param: step}
emitPartial: false # only full windows: a true moving average
mean:
pipe: ./window-mean
inputs:
windows: {from: window.outputs.out}
grants:
subprocess: true
outputs:
means: {from: mean.outputs.means}What the R script receives on stdin: one window frame per line
{"window":{"index":0,"partition":null,"start":0,"end":2,"final":false,"partial":false},"items":[{"value":10},{"value":20},{"value":30}]}
{"window":{"index":1,"partition":null,"start":1,"end":3,"final":false,"partial":false},"items":[{"value":20},{"value":30},{"value":40}]}What to notice
- The pipeline has two nodes:
std:window(size 3, step 1) and the R pipewindow-mean, which reads one line, prints one mean, flushes and forgets the line. Feed it one record per second and one mean per second comes out, starting with the third record. --param size=2 --param step=2gives tumbling windows instead of sliding ones.- Only full windows are emitted (
emitPartial: false): fewer thansizerecords give no output. - These are stream windows: logical groups of records. They are not SQL window functions and not transport batches. Window framing is a proposed part of the specification.
- The repository verified it with R; it was not re-run for this page.