Streaming.pm untested

filter: Statementstmt 0.0% 0 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 3 SCAR -
1 package Hadoop::Streaming;
2 {
3 $Hadoop::Streaming::VERSION = '0.122420';
4 }
5  
6 #ABSTRACT: Contains Mapper, Combiner and Reducer roles to simplify writing Hadoop Streaming jobs
7  
8  
9 1;
10  
11 __END__
12 =pod
13  
14 =head1 NAME
15  
16 Hadoop::Streaming - Contains Mapper, Combiner and Reducer roles to simplify writing Hadoop Streaming jobs
17  
18 =head1 VERSION
19  
20 version 0.122420
21  
22 =head1 SYNOPSIS
23  
24 My/Hadoop/Example.pm:
25  
26 package My::Hadoop::Example;
27 use Any::Moose qw(Role);
28  
29 sub map
30 {
31 my ($self, $line) = @_;
32 my ($key, $value);
33 #... create $key and $value
34 $self->emit( $key => $value);
35 }
36 sub reduce
37 {
38 my ( $self, $key, $value_iterator) = @_;
39 my $composite_value;
40 #... set $composite_value
41 while( $value_iterator->has_next() ) { }
42 $self->emit( $key, $composite_value );
43 }
44 sub combine
45 {
46 my ( $self, $key, $value_iterator) = @_;
47 my $composite_value;
48 #... set $composite_value
49 while( $value_iterator->has_next() ) { }
50 $self->emit( $key, $composite_value );
51 }
52  
53 package My::Hadoop::Example::Mapper;
54 use Any::Moose;
55 with qw(Hadoop::Streaming::Mapper My::Hadoop::Example);
56  
57 package My::Hadoop::Example::Combiner;
58 use Any::Moose;
59 with qw(Hadoop::Streaming::Combiner My::Hadoop::Example);
60  
61 package My::Hadoop::Example::Reducer;
62 use Any::Moose;
63 with qw(Hadoop::Streaming::Reducer My::Hadoop::Example);
64  
65 1;
66  
67 mapper executable:
68  
69 #!/usr/bin/perl
70 use My::Hadoop::Example;
71 My::Hadoop::Example::Mapper->run();
72  
73 combiner executable:
74  
75 #!/usr/bin/perl
76 use My::Hadoop::Example;
77 My::Hadoop::Example::Combiner->run();
78  
79 reducer executable:
80  
81 #!/usr/bin/perl
82 use My::Hadoop::Example;
83 My::Hadoop::Example::Reducer->run();
84  
85 =head1 DESCRIPTION
86  
87 Hadoop::Streaming::* provides a simple perl interface to the Streaming interface of Hadoop.
88  
89 Hadoop is a system "reliable, scalable, distributed computing." Hadoop was developed at Yahoo! and is now maintained by the Apache Software Foundation.
90  
91 Hadoop provides a distributed map/reduce framework. Mappers take lines of unstructured file data and produce key/value pairs. These key/value pairs are merged and sorted by key and provided to Reducers. Reducers take key/value pairs and produce higher order data. This works for data that where output key/value pairs can be determined from a single line of data in isolation. The Reducer is provided sho
92  
93 =over
94  
95 =item Hadoop's Streaming Interface
96  
97 The Streaming interface provides a simple API for writing Hadoop jobs in any language. Jobs are provided input on STDIN and output is expected on STDOUT. Key value pairs are separated by a TAB character.
98  
99 Streaming map jobs are provided an input of lines instead of key-value pairs. See Hadoop::Streaming::Mapper INTERFACE DETAILS for an explanation.
100  
101 Reduce jobs are provided a stream of key\tvalue lines. multivalued keys appear on an input line once for each key\value. The stream is guaranteed to be sorted by key. The reduce job must track the key/value pairs and manually detect a key change.
102  
103 =item Hadoop::Streaming::Mapper interface
104  
105 Hadoop::Mapper consumes and chomps lines from STDIN and calls map($line) once per line. This is initiated by the run() method.
106  
107 example mapper input:
108  
109 line1
110 line2
111 line3
112  
113 Hadoop::Mapper transforms this into 3 calls to map()
114  
115 map(line1)
116 map(line2)
117 map(line3)
118  
119 =item Hadoop::Streaming::Reducer interface
120  
121 Hadoop::Reducer abstracts this stream into an interface of (key, value-iterator). reduce() is called once per key, instead of once per line. The reduce job pulls values from the iterator and outputs key/value pairs to STDOUT. emit() is provided as a convenience for outputing key/value pairs.
122  
123 example reducer input:
124  
125 key1 value1
126 key2 valuea
127 key2 valuec
128 key2 valueb
129 key3 valuefoo
130 key3 valuebar
131  
132 Hadoop::Streaming::Reduce transforms this input into three calls to reduce():
133  
134 reduce( key, iterator_over(qw(value1)) );
135 reduce( key2, iterator_over(qw(valuea valuec valueb)) );
136 reduce( key3, iterator_over(qw(valuefoo valuebarr)) );
137  
138 =item Hadoop::Streaming::Combiner interface
139  
140 The Hadoop::Streaming::Combiner interface is analagous to the Hadoop::Streaming::Reducer interface. combine() is called instead of reduce() for each key. The above example would produce three calls to combine():
141  
142 combine( key, iterator_over(qw(value1)) );
143 combine( key2, iterator_over(qw(valuea valuec valueb)) );
144 combine( key3, iterator_over(qw(valuefoo valuebarr)) );
145  
146 =back
147  
148 =head1 SEE ALSO
149  
150 =over 4
151  
152 =item Map/Reduce at wikipedia
153  
154 http://en.wikipedia.org/wiki/MapReduce
155  
156 =item Hadoop
157  
158 http://hadoop.apache.org
159  
160 =item Hadoop Streaming interface:
161  
162 http://hadoop.apache.org/common/docs/r0.20.1/streaming.html
163  
164 =item PAR::Packer
165  
166 http://search.cpan.org/perldoc?PAR::Packer
167  
168 =back
169  
170 =head1 EXAMPLES
171  
172 =over 4
173  
174 =item run locally without hadoop
175  
176 To test locally the examples from the SYNOPSIS() section, we must provide the sort function provided by hadoop. For small test_input_file examples this can be done in one large pipe:
177  
178 my_mapper < test_input_file | sort | my_combiner | my_reducer
179  
180 For larger files, make intermediary output files. The output of the intermediate files can be verified and used to demonstrate the efficiency of the (optional) combiner.
181  
182 my_mapper < test_input_file > output.map && \
183 sort output.map > output.mapsort && \
184 my_combiner < output.mapsort > output.combine && \
185 my_reducer < output.combine > output.reduce
186  
187 =item hadoop commandline
188  
189 Run this in hadoop from the shell:
190  
191 hadoop \
192 jar $streaming_jar_name \
193 -D mapred.job.name="my hadoop example" \
194 -input my_input_file \
195 -output my_output_hdfs_path \
196 -mapper my_mapper \
197 -combiner my_combiner \
198 -reducer my_reducer
199  
200 $streaming_jar_name is the full path to the streaming jar provided by the installed hadoop. For my 0.20 install the path is:
201  
202 /usr/lib/hadoop-0.20/contrib/streaming/hadoop-0.20.1+152-streaming.jar
203  
204 The -D line is optional. If included, -D lines must come directly after the jar name and before other options.
205  
206 For this hadoop job to work, the mapper, combiner and reducer must be full paths that are valid on each box in the hadoop cluster. There are a few ways to make this work.
207  
208 =item hadoop job -files option
209  
210 Additional files may be bundled into the hadoop jar via the '-files' option to hadoop jar. These files will be included in the jar that is distributed to each host. The files will be visible in the current working directory of the process. Subdirectories will not be created.
211  
212 example:
213 hadoop \
214 jar $streaming_jar_name \
215 -D mapred.job.name="my hadoop example" \
216 -input my_input_file \
217 -output my_output_hdfs_path \
218 -mapper my_mapper \
219 -combiner my_combiner \
220 -reducer my_reducer \
221 -file /path/to/my_mapper \
222 -file /path/to/my_combiner \
223 -file /path/to/my_reducer
224  
225 =item using perl modules
226  
227 All perl modules must be installed on each hadoop cluster machine. This proves to be a challenge for large installations. I have a local::lib controlled perl directory that I push out to a fixed location on all of my hadoop boxes (/apps/perl5) that is kept up-to-date and included in my system image. Previously I was producing stand-alone perl files with PAR::Packer (pp), which worked quite well except for the size of the jar with the -file option. The standalone files can be put into hdfs and then included with the jar via the -cacheFile option. A final option is to create a jar (zip) of library files and use -archives option to push the jar and expand it into the working directory.
228  
229 =over 4
230  
231 =item local::lib
232  
233 * install all modules into a local::lib controlled directory, push this directory to all of the hadoop cluster boxes (rsync, app installer, nfs mount ), explicitly include this directory in a C<use lib> or C<use local::lib> line in your mapper/reducer/combiner files.
234  
235 #!/usr/bin/perl
236 use strict; use warnings;
237 use lib qw(/apps/perl5);
238 use My::Example::Job;
239 My::Example::Job::Mapper->run();
240  
241 * The mapper/reducer/combiner files can be included with the job via -file options to hadoop jar or they can be referenced directly if they are in the shared environment.
242  
243 =item full path of shared file
244  
245 hadoop \
246 jar $streaming_jar_name \
247 -input my_input_file \
248 -output my_output_hdfs_path \
249 -mapper /apps/perl5/bin/my_mapper \
250 -combiner /apps/perl5/bin/my_combiner \
251 -reducer /apps/perl5/bin/my_reducer
252  
253 =item local path of included -file file
254  
255 hadoop \
256 jar $streaming_jar_name \
257 -input my_input_file \
258 -output my_output_hdfs_path \
259 -file /apps/perl5/bin/my_mapper \
260 -file /apps/perl5/bin/my_combiner \
261 -file /apps/perl5/bin/my_reducer \
262 -mapper ./my_mapper \
263 -combiner ./my_combiner \
264 -reducer ./my_reducer
265  
266 =back
267  
268 =item --archive flag and jar of perl libraries
269  
270 Recommended.
271  
272 Create a jar of your lib directory and include via -archives flag. The jar will be expanded into the working directory. For the example 'lib.jar' below, the jar will exand to './lib.jar/lib/' . Include this path within your mapper/reducer/combiner code.
273  
274 jar -cvf lib.jar lib/
275  
276 hadoop jar ${jarpath} \
277 -archives lib.jar \
278 -input /path/to/inputdir \
279 -output /path/to/output \
280 -file /path/to/mapper.pl \
281 -file /path/to/reducer.pl \
282 -mapper mapper.pl \
283 -reducer reducer.pl
284  
285 Within mapper.pl and reducer.pl, include the lib path "./lib.jar/lib", either by -I flag to perl or 'use libs'.
286  
287 mapper.pl:
288 #!/usr/bin/perl -I./lib.jar/lib
289 use My::Hadoop::Example;
290 My::Hadoop::Example::Mapper->run();
291  
292 mapper.pl:
293 #!/usr/bin/perl
294 use libs './lib.jar/lib';
295 use My::Hadoop::Example;
296 My::Hadoop::Example::Mapper->run();
297  
298 =item PAR::Packer / pp
299  
300 Deprecated.
301  
302 Use pp (installed via PAR::Packer) to produce a perl file that needs only a perl interpreter to execute. I use -x option to run the my_mapper script on blank input, as this forces all of the necessary modules to be loaded and thus tracked in my PAR file.
303  
304 mkdir packed
305 pp my_mapper -B -P -Ilib -o packed/my_mapper -x my_mapper < /dev/null
306 hadoop \
307 jar $streaming_jar_name \
308 -input my_input_file \
309 -output my_output_hdfs_path \
310 -file packed/my_mapper \
311 -mapper ./my_mapper
312  
313 To simplify this process and reduce errors, I use make to produce the packed binaries. Indented lines after "name :" lines are indented with a literal tab, as per Makefile requirements.
314  
315 #Makefile for PAR packed apps
316 PERLTOPACK = \
317 region-dma-mapper.pl \
318 region-dma-reducer.pl \
319 multiattribute-mapper.pl \
320 multiattribute-combiner.pl \
321 multiattribute-reducer.pl
322  
323 PERL_LIBRARIES = \
324 lib/RegionDMA.pm \
325 lib/RegionDMA/Lookup.pm \
326 lib/Truthiness/Allocator.pm
327  
328 PACKEDTARGETS = $(patsubst %,packed/%,$(PERLTOPACK))
329 PACK = packed $(PACKEDTARGETS)
330  
331 PACK : $(PACK)
332 echo "pack: $(PACKTARGETS)"
333 packed:
334 mkdir packed
335 #
336 # perl files to compile via pp
337 #
338 $(patsubst %,packed/%,$(PERLTOPACK)) : packed/%.pl : bin/%.pl $(PERL_LIBRARIES)
339 time pp $< -B -P -I lib/ -o $@ -x $< < /dev/null
340 ### END MAKEFILE
341  
342 =back
343  
344 =head1 AUTHORS
345  
346 =over 4
347  
348 =item *
349  
350 andrew grangaard <spazm@cpan.org>
351  
352 =item *
353  
354 Naoya Ito <naoya@hatena.ne.jp>
355  
356 =back
357  
358 =head1 COPYRIGHT AND LICENSE
359  
360 This software is copyright (c) 2012 by Naoya Ito <naoya@hatena.ne.jp>.
361  
362 This is free software; you can redistribute it and/or modify it under
363 the same terms as the Perl 5 programming language system itself.
364  
365 =cut
366