Created
July 5, 2016 13:18
-
-
Save adokoy001/882b857a5306f2f1ca317f8a31f89c6f to your computer and use it in GitHub Desktop.
MapReduce by YCGL::Lite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can startup MR worker by one liner below. | |
# $ perl -MYCGL::Lite -e 'YCGL::Lite->new->plack->plackup_eval("/eval");' | |
# Example: Word count | |
use strict; | |
use warnings; | |
use YCGL::Lite; | |
use Data::Dumper; | |
my $ycgl = YCGL::Lite->new(); | |
my $data = [ | |
['I am a student.','http://localhost:5000/eval'], | |
['I like Sushi.','http://localhost:5000/eval'], | |
['Who am I?','http://localhost:5000/eval'] | |
]; | |
my $mapper = sub { | |
my $input = shift; | |
$input =~ s/\.|\,|\?|\!//eg; | |
my @words = split(/\s+/,$input); | |
my $output; | |
for(0 .. $#words){ | |
if(defined($output->{$words[$_]})){ | |
$output->{$words[$_]} += 1; | |
}else{ | |
$output->{$words[$_]} = 1; | |
} | |
} | |
return($output); | |
}; | |
my $reducer = sub { | |
my $input = shift; | |
my $output; | |
for(0 .. $#$input){ | |
my $tmp_input = $input->[$_]; | |
foreach my $key (keys %$tmp_input){ | |
if(defined($output->{$key})){ | |
$output->{$key} += $tmp_input->{$key}; | |
}else{ | |
$output->{$key} = $tmp_input->{$key}; | |
} | |
} | |
} | |
return($output); | |
}; | |
my $result = $ycgl->parallel->map_reduce( | |
$data, | |
$mapper, | |
$reducer, | |
3, | |
{remote => 1} | |
); | |
print Dumper $result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment