summaryrefslogtreecommitdiff
path: root/dtrain/hstreaming/red-avg.rb
blob: c0b69eb454773d03bd246f4067ec560036efa4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby1.9.1

shard_count_key = "__SHARD_COUNT__"

STDIN.set_encoding 'utf-8'
STDOUT.set_encoding 'utf-8'

w = {}
c = {}
w.default = 0.
c.default = 0
while line = STDIN.gets
  key, val = line.split /\t/
  w[key] += val.to_f
  c[key] += 1
end

puts "# dtrain reducer: average"
shard_count = w["__SHARD_COUNT__"]
w.each_key { |k|
  if k == shard_count_key
    puts "# shard count: #{shard_count.to_i}"
  else
    puts "#{k}\t#{w[k]/shard_count}\t# #{c[k]}"
  end
}