I'm new to Apache Beam, and I want to calculate the mean and std deviation over a large dataset.
Given a .csv file of the form "A,B" where A, B are ints, this is basically what I have.
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.io.textio import ReadFromText
class Split(beam.DoFn):
def process(self, element):
A, B = element.split(',')
return [('A', A), ('B', B)]
with beam.Pipeline(options=PipelineOptions()) as p:
# parse the rows
rows = (p
| ReadFromText('data.csv')
| beam.ParDo(Split()))
# calculate the mean
avgs = (rows
| beam.CombinePerKey(
beam.combiners.MeanCombineFn()))
# calculate the stdv per key
# ???
std >> beam.io.WriteToText('std.out')
I'd like to do something like:
class SquaredDiff(beam.DoFn):
def process(self, element):
A = element[0][1]
B = element[1][1]
return [('A', A - avgs[0]), ('B', B - avgs[1])]
stdv = (rows
| beam.ParDo(SquaredDiff())
| beam.CombinePerKey(
beam.combiners.MeanCombineFn()))
or something, but I can't figure out how.