File Coverage

blib/lib/Hadoop/Streaming/Reducer.pm
Criterion Covered Total %
statement 9 19 47.3
branch 0 4 0.0
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 28 46.4


line stmt bran cond sub pod time code
1             package Hadoop::Streaming::Reducer;
2             {
3             $Hadoop::Streaming::Reducer::VERSION = '0.122420';
4             }
5 1     1   4136 use Any::Moose qw(Role);
  1         3  
  1         9  
6              
7 1     1   503 use IO::Handle;
  1         1  
  1         40  
8 1     1   558 use Hadoop::Streaming::Reducer::Input;
  1         3  
  1         147  
9              
10             with 'Hadoop::Streaming::Role::Emitter';
11             requires qw/reduce/;
12              
13             # ABSTRACT: Simplify writing Hadoop Streaming jobs. Write a reduce() function and let this role handle the Stream interface. This Reducer roll provides an iterator over the multiple values for a given key.
14              
15              
16              
17             sub run {
18 0     0 1   my $class = shift;
19 0           my $self = $class->new;
20              
21 0           my $input = Hadoop::Streaming::Reducer::Input->new(handle => \*STDIN);
22 0           my $iter = $input->iterator;
23              
24 0           while ($iter->has_next) {
25 0 0         my ($key, $values_iter) = $iter->next or last;
26 0           eval {
27 0           $self->reduce( $key => $values_iter );
28             };
29 0 0         if ($@) {
30 0           warn $@;
31             }
32             }
33             }
34              
35             1;
36              
37              
38             __END__
39             =pod
40              
41             =head1 NAME
42              
43             Hadoop::Streaming::Reducer - Simplify writing Hadoop Streaming jobs. Write a reduce() function and let this role handle the Stream interface. This Reducer roll provides an iterator over the multiple values for a given key.
44              
45             =head1 VERSION
46              
47             version 0.122420
48              
49             =head1 SYNOPSIS
50              
51             #!/usr/bin/env perl
52              
53             package WordCount::Reducer;
54             use Any::Moose;
55             with qw/Hadoop::Streaming::Reducer/;
56              
57             sub reduce {
58             my ($self, $key, $values) = @_;
59              
60             my $count = 0;
61             while ( $values->has_next ) {
62             $count++;
63             $values->next;
64             }
65              
66             $self->emit( $key => $count );
67             }
68              
69             package main;
70             WordCount::Reducer->run;
71              
72             Your mapper class must implement map($key,$value) and your reducer must
73             implement reduce($key,$value). Your classes will have emit() and run()
74             methods added via the role.
75              
76             =head1 METHODS
77              
78             =head2 run
79              
80             Package->run();
81              
82             This method starts the Hadoop::Streaming::Reducer instance.
83              
84             After creating a new object instance, it reads from STDIN and calls $object->reduce( ) passing in the key and an iterator of values for that key.
85              
86             Subclasses need only implement reduce() to produce a complete Hadoop Streaming compatible reducer.
87              
88             =head1 AUTHORS
89              
90             =over 4
91              
92             =item *
93              
94             andrew grangaard <spazm@cpan.org>
95              
96             =item *
97              
98             Naoya Ito <naoya@hatena.ne.jp>
99              
100             =back
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             This software is copyright (c) 2012 by Naoya Ito <naoya@hatena.ne.jp>.
105              
106             This is free software; you can redistribute it and/or modify it under
107             the same terms as the Perl 5 programming language system itself.
108              
109             =cut
110