Class: SumReduce
- Inherits:
-
ReduceBase
- Object
- Stage
- ReduceBase
- SumReduce
- Defined in:
- lib/mrtoolkit.rb
Overview
Reducer sums given fields Specify how many fields to sum (default 1). May optionally specify how many initial fields to skip Outputs one line of sums
Instance Attribute Summary
Attributes inherited from Stage
#errors, #in_fields, #in_sep, #out_fields, #out_sep
Instance Method Summary collapse
- #declare ⇒ Object
-
#initialize(*args) ⇒ SumReduce
constructor
A new instance of SumReduce.
- #process(input, output) ⇒ Object
- #process_begin(dummy, output) ⇒ Object
- #process_end(dummy, output) ⇒ Object
Methods inherited from ReduceBase
#process_each, #process_end_internal, #process_init, #process_internal, #process_term, #run
Methods inherited from Stage
#catch_errors, #copy_struct, #emit, #emit_separator, #field, #field_separator, #new_input, #new_output, #prepare, #process_step, #write_out
Constructor Details
#initialize(*args) ⇒ SumReduce
Returns a new instance of SumReduce.
343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/mrtoolkit.rb', line 343 def initialize(*args) if args[0] @n = args[0].to_i - 1 else @n = 0 end if args[1] @m = args[1].to_i - 1 else @m = -1 end end |
Instance Method Details
#declare ⇒ Object
355 356 357 358 359 360 |
# File 'lib/mrtoolkit.rb', line 355 def declare (0..@m).each {|i| field "skip#{i}"} (0..@n).each {|i| field "count#{i}"} (0..@n).each {|i| emit "sum#{i}"} end |
#process(input, output) ⇒ Object
366 367 368 369 |
# File 'lib/mrtoolkit.rb', line 366 def process(input, output) (0..@n).each {|i| @sum[i] += input[i+@m+1].to_f} nil end |
#process_begin(dummy, output) ⇒ Object
362 363 364 365 |
# File 'lib/mrtoolkit.rb', line 362 def process_begin(dummy, output) @sum = Array.new(@n+1, 0) nil end |
#process_end(dummy, output) ⇒ Object
370 371 372 373 |
# File 'lib/mrtoolkit.rb', line 370 def process_end(dummy, output) (0..@n).each {|i| output[i] = @sum[i]} output end |