Sha256: 398b439f6be2ca21413c203b445128a268e71159789d9b1e68603cb0942a0de5

Contents?: true

Size: 1.25 KB

Versions: 18

Compression:

Stored size: 1.25 KB

Contents

#!/usr/bin/env ruby
$: << File.dirname(__FILE__)+'/../lib'
require 'wukong'

module Size
  #
  # Feed the entire dataset through wc and sum the results
  #
  class Script < Wukong::Script
    #
    # Don't implement a wukong script to do something if there's a unix command
    # that does it faster: just override map_command or reduce_command in your
    # subclass of Wukong::Script to return the complete command line
    #
    def map_command
      '/usr/bin/wc'
    end

    # Make all records go to one reducer
    def default_options
      super.merge :reduce_tasks => 1
    end
  end

  #
  # Sums the numeric value of each column in its input
  #
  class Reducer < Wukong::Streamer::Base
    attr_accessor :sums

    #
    # The unix +wc+ command uses whitespace, not tabs, so we'll recordize
    # accordingly.
    #
    def recordize line
      line.strip.split(/\s+/)
    end

    #
    # add each corresponding column in the input
    #
    def process *vals
      self.sums = vals.zip( sums || [] ).map{|val,sum| val.to_i + sum.to_i }
    end

    #
    # run through the whole reduction input and then output the total
    #
    def stream *args
      super *args
      emit sums
    end
  end
end

# Execute the script
Size::Script.new(
  nil,
  Size::Reducer
  ).run

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
mrflip-wukong-0.1.0 examples/size.rb
wukong-1.5.4 examples/size.rb
wukong-1.5.3 examples/size.rb
wukong-1.5.2 examples/size.rb
wukong-1.5.1 examples/size.rb
wukong-1.5.0 examples/size.rb
wukong-1.4.12 examples/size.rb
wukong-1.4.11 examples/size.rb
wukong-1.4.10 examples/size.rb
wukong-1.4.9 examples/size.rb
wukong-1.4.7 examples/size.rb
wukong-1.4.6 examples/size.rb
wukong-1.4.5 examples/size.rb
wukong-1.4.2 examples/size.rb
wukong-1.4.1 examples/size.rb
wukong-1.4.0 examples/size.rb
wukong-0.1.4 examples/size.rb
wukong-0.1.1 examples/size.rb