diff options
| author | franck cuny <franck@lumberjaph.net> | 2010-06-16 11:49:34 +0200 |
|---|---|---|
| committer | franck cuny <franck@lumberjaph.net> | 2010-06-16 11:49:34 +0200 |
| commit | 859212604e96818f5ec0b45c01d83bf2934c6697 (patch) | |
| tree | 7d7e950b16088335198f89ebbed863f749f43b4b /eg/mapred_03.pl | |
| parent | add get_keys; change params default in get_properties; add POD (diff) | |
| download | net-riak-859212604e96818f5ec0b45c01d83bf2934c6697.tar.gz | |
add three map/reduce operations (from the fast track)
Diffstat (limited to 'eg/mapred_03.pl')
| -rw-r--r-- | eg/mapred_03.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/eg/mapred_03.pl b/eg/mapred_03.pl new file mode 100644 index 0000000..228260b --- /dev/null +++ b/eg/mapred_03.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.010; + +use Net::Riak; + +my $riak = Net::Riak->new(); +my $result; + +$result = + $riak->add('goog')->map(map_max_daily_variance()) + ->reduce(reduce_max_daily_variance())->run; +say "max daily variance"; +map { say $_ . " => ". $result->[0]->{$_} } sort {$a cmp $b} keys %{$result->[0]}; + +sub map_max_daily_variance { + " +function(value, keyData, arg){ + var data = Riak.mapValuesJson(value)[0]; + var month = value.key.split('-').slice(0,2).join('-'); + var obj = {}; + obj[month] = data.High - data.Low; + return [ obj ]; +} +"; +} + +sub reduce_max_daily_variance { + " +function(values, arg){ + return [ values.reduce(function(acc, item){ + for(var month in item){ + if(acc[month]) { acc[month] = (acc[month] < item[month]) ? item[month] : acc[month]; } + else { acc[month] = item[month]; } + } + return acc; + }) + ]; +} +"; +} + |
