| 1 | package Hadoop::Streaming::Combiner; | ||||||||||
| 2 | { | ||||||||||
| 3 | 1 | $Hadoop::Streaming::Combiner::VERSION = '0.122420'; | |||||||||
| 4 | } | ||||||||||
| 5 | 1 | ▶ | use Any::Moose qw(Role); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Hadoop::Streaming::Reducer::Input; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | with 'Hadoop::Streaming::Role::Emitter'; | |||||||||
| 11 | 1 | requires qw/combine/; | |||||||||
| 12 | |||||||||||
| 13 | # ABSTRACT: Simplify writing Hadoop Streaming jobs. Combiner follows the same interface as Reducer. Requires a combine() function which will be called for each line of combiner data. Combiners are run on the same machine as the mapper as a pre-reduce reduction step. | ||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | sub run { | ||||||||||
| 18 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
run: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 19 | 1 | my $self = $class->new; | |||||||||
| 20 | |||||||||||
| 21 | 1 | my $input = Hadoop::Streaming::Reducer::Input->new(handle => \*STDIN); | |||||||||
| 22 | 1 | my $iter = $input->iterator; | |||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | while ($iter->has_next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 17 | ▶ | my ($key, $values_iter) = $iter->next or last; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 17 | eval { | |||||||||
| 27 | 17 | $self->combine( $key => $values_iter ); | |||||||||
| 28 | }; | ||||||||||
| 29 | 17 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | warn $@; | |||||||||
| 31 | } | ||||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 1 | 1; | |||||||||
| 36 | |||||||||||
| 37 | __END__ | ||||||||||
| 38 | =pod | ||||||||||
| 39 | |||||||||||
| 40 | =head1 NAME | ||||||||||
| 41 | |||||||||||
| 42 | Hadoop::Streaming::Combiner - Simplify writing Hadoop Streaming jobs. Combiner follows the same interface as Reducer. Requires a combine() function which will be called for each line of combiner data. Combiners are run on the same machine as the mapper as a pre-reduce reduction step. | ||||||||||
| 43 | |||||||||||
| 44 | =head1 VERSION | ||||||||||
| 45 | |||||||||||
| 46 | version 0.122420 | ||||||||||
| 47 | |||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||
| 49 | |||||||||||
| 50 | #!/usr/bin/env perl | ||||||||||
| 51 | |||||||||||
| 52 | package WordCount::Combiner; | ||||||||||
| 53 | use Any::Moose; | ||||||||||
| 54 | with qw/Hadoop::Streaming::Combiner/; | ||||||||||
| 55 | |||||||||||
| 56 | sub combine { | ||||||||||
| 57 | my ($self, $key, $values) = @_; | ||||||||||
| 58 | |||||||||||
| 59 | my $count = 0; | ||||||||||
| 60 | while ( $values->has_next ) { | ||||||||||
| 61 | $count++; | ||||||||||
| 62 | $values->next; | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | $self->emit( $key => $count ); | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | package main; | ||||||||||
| 69 | WordCount::Combiner->run; | ||||||||||
| 70 | |||||||||||
| 71 | This combiner is identical to our reduce example. In this case, the reducer could be used as the combiner as it maps (k,v)->(k',v') with the same key and value format. | ||||||||||
| 72 | |||||||||||
| 73 | This method exists as a convenience factor as a wrapper around the Hadoop::Streaming::Reduce pieces. | ||||||||||
| 74 | |||||||||||
| 75 | Your mapper class must implement map($key,$value), your optional combiner must implement combine($key,$value), and your reducer must implement reduce($key,$value). The combiner should generally output ($key,$value_combine) in the same format to allow the user to engage or disengage the optional combiner step as necessary to optimize the job run. | ||||||||||
| 76 | |||||||||||
| 77 | Your classes will have emit() and run() methods added via the role. | ||||||||||
| 78 | |||||||||||
| 79 | =head1 METHODS | ||||||||||
| 80 | |||||||||||
| 81 | =head2 run | ||||||||||
| 82 | |||||||||||
| 83 | Package->run(); | ||||||||||
| 84 | |||||||||||
| 85 | This method starts the Hadoop::Streaming::Combiner instance. | ||||||||||
| 86 | |||||||||||
| 87 | After creating a new object instance, it reads from STDIN and calls $object->combine( ) passing in the key and an iterator of values for that key. | ||||||||||
| 88 | |||||||||||
| 89 | Subclasses need only implement combine() to produce a complete Hadoop Streaming compatible reducer. | ||||||||||
| 90 | |||||||||||
| 91 | =head1 AUTHORS | ||||||||||
| 92 | |||||||||||
| 93 | =over 4 | ||||||||||
| 94 | |||||||||||
| 95 | =item * | ||||||||||
| 96 | |||||||||||
| 97 | andrew grangaard <spazm@cpan.org> | ||||||||||
| 98 | |||||||||||
| 99 | =item * | ||||||||||
| 100 | |||||||||||
| 101 | Naoya Ito <naoya@hatena.ne.jp> | ||||||||||
| 102 | |||||||||||
| 103 | =back | ||||||||||
| 104 | |||||||||||
| 105 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 106 | |||||||||||
| 107 | This software is copyright (c) 2012 by Naoya Ito <naoya@hatena.ne.jp>. | ||||||||||
| 108 | |||||||||||
| 109 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 110 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 111 | |||||||||||
| 112 | =cut | ||||||||||
| 113 | |||||||||||