/
PentahoMapReduceIT.java
825 lines (670 loc) · 31.9 KB
/
PentahoMapReduceIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*******************************************************************************
*
* Pentaho Big Data
*
* Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.hadoop.mapreduce.test;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.Counters.Counter;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.InputSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.RecordReader;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LoggingRegistry;
import org.pentaho.di.core.plugins.Plugin;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginMainClassType;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.trans.TransConfiguration;
import org.pentaho.di.trans.TransExecutionConfiguration;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.steps.hadoopenter.HadoopEnterMeta;
import org.pentaho.di.trans.steps.hadoopexit.HadoopExitMeta;
import org.pentaho.hadoop.mapreduce.GenericTransCombiner;
import org.pentaho.hadoop.mapreduce.GenericTransReduce;
import org.pentaho.hadoop.mapreduce.PentahoMapRunnable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.*;
@SuppressWarnings( { "nls", "unchecked", "deprecation", "rawtypes" } )
public class PentahoMapReduceIT {
private boolean debug = false;
@Before
public void setup() throws KettleException {
KettleEnvironment.init();
}
@After
public void shutdown() {
}
@Test
public void testMapperBadInjectorFields() throws IOException, KettleException {
try {
PentahoMapRunnable mapper = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
mapper.configure( createJobConf( "./src/test/resources/bad-injector-fields.ktr", "./src/it/resources/wordcount-reducer.ktr" ) );
mapper.run( reader, outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains(
"key or value is not defined in transformation injector step" ) );
}
}
@Test
public void testMapperBadOutputFields() throws IOException, KettleException {
try {
PentahoMapRunnable mapper = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
mapper.configure( createJobConf( "./src/test/resources/bad-output-fields.ktr", "./src/test/resources/bad-output-fields.ktr" ) );
mapper.run( reader, outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains(
"outKey or outValue is not defined in transformation output stream" ) );
}
}
@Test
public void testMapperNoInjectorStep() throws IOException, KettleException {
try {
PentahoMapRunnable mapper = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
mapper.configure( createJobConf( "./src/test/resources/no-injector-step.ktr", "./src/test/resources/no-injector-step.ktr" ) );
mapper.run( reader, outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains(
"Unable to find thread with name Injector and copy number 0" ) );
}
}
@Test
public void testMapperNoOutputStep() throws IOException, KettleException {
try {
PentahoMapRunnable mapper = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
mapper.configure( createJobConf( "./src/test/resources/no-output-step.ktr", "./src/test/resources/no-output-step.ktr" ) );
mapper.run( reader, outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains( "Output step not defined in transformation" ) );
}
}
@Test
public void testReducerBadInjectorFields() throws IOException, KettleException {
try {
GenericTransReduce reducer = new GenericTransReduce();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
reducer.configure( createJobConf( "./src/test/resources/bad-injector-fields.ktr", "./src/test/resources/bad-injector-fields.ktr" ) );
reducer.reduce( new Text( "key" ), Arrays.asList( new Text( "value" ) ).iterator(), outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains(
"key or value is not defined in transformation injector step" ) );
}
}
@Test
public void testReducerNoInjectorStep() throws IOException, KettleException {
try {
GenericTransReduce reducer = new GenericTransReduce();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
reducer.configure( createJobConf( "./src/test/resources/no-injector-step.ktr", "./src/test/resources/no-injector-step.ktr" ) );
reducer.reduce( new Text( "key" ), Arrays.asList( new Text( "value" ) ).iterator(), outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains(
"Unable to find thread with name Injector and copy number 0" ) );
}
}
@Test
public void testReducerNoOutputStep() throws IOException, KettleException {
try {
GenericTransReduce reducer = new GenericTransReduce();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
reducer.configure( createJobConf( "./src/test/resources/no-output-step.ktr", "./src/test/resources/no-output-step.ktr" ) );
reducer.reduce( new Text( "key" ), Arrays.asList( new Text( "value" ) ).iterator(), outputCollector, reporter );
fail( "Should have thrown an exception" );
} catch ( IOException e ) {
assertTrue( "Test for KettleException", e.getMessage().contains( "Output step not defined in transformation" ) );
}
}
public static JobConf createJobConf( String mapperTransformationFile, String reducerTransformationFile )
throws IOException, KettleException {
return createJobConf( mapperTransformationFile, null, reducerTransformationFile, "localhost", "9000", "9001" );
}
public static JobConf createJobConf( String mapperTransformationFile, String combinerTransformationFile,
String reducerTransformationFile ) throws IOException, KettleException {
return createJobConf( mapperTransformationFile, combinerTransformationFile, reducerTransformationFile, "localhost",
"9000", "9001" );
}
public static JobConf createJobConf( String mapperTransformationFile, String combinerTransformationFile,
String reducerTransformationFile, String hostname, String hdfsPort, String trackerPort ) throws IOException,
KettleException {
JobConf conf = new JobConf();
conf.setJobName( "wordcount" );
KettleEnvironment.init();
// Register Map/Reduce Input and Map/Reduce Output plugin steps
PluginMainClassType mainClassTypesAnnotation = StepPluginType.class.getAnnotation( PluginMainClassType.class );
Map<Class<?>, String> inputClassMap = new HashMap<Class<?>, String>();
inputClassMap.put( mainClassTypesAnnotation.value(), HadoopEnterMeta.class.getName() );
PluginInterface inputStepPlugin =
new Plugin( new String[] { "HadoopEnterPlugin" }, StepPluginType.class, mainClassTypesAnnotation.value(),
"Hadoop", "MapReduce Input", "Enter a Hadoop Mapper or Reducer transformation", "MRI.png", false, false,
inputClassMap, new ArrayList<String>(), null, null );
PluginRegistry.getInstance().registerPlugin( StepPluginType.class, inputStepPlugin );
Map<Class<?>, String> outputClassMap = new HashMap<Class<?>, String>();
outputClassMap.put( mainClassTypesAnnotation.value(), HadoopExitMeta.class.getName() );
PluginInterface outputStepPlugin =
new Plugin( new String[] { "HadoopExitPlugin" }, StepPluginType.class, mainClassTypesAnnotation.value(),
"Hadoop", "MapReduce Output", "Exit a Hadoop Mapper or Reducer transformation", "MRO.png", false, false,
outputClassMap, new ArrayList<String>(), null, null );
PluginRegistry.getInstance().registerPlugin( StepPluginType.class, outputStepPlugin );
TransExecutionConfiguration transExecConfig = new TransExecutionConfiguration();
TransMeta transMeta = null;
TransConfiguration transConfig = null;
if ( mapperTransformationFile != null ) {
conf.setMapRunnerClass( PentahoMapRunnable.class );
transMeta = new TransMeta( mapperTransformationFile );
transConfig = new TransConfiguration( transMeta, transExecConfig );
conf.set( "transformation-map-xml", transConfig.getXML() );
conf.set( "transformation-map-input-stepname", "Injector" );
conf.set( "transformation-map-output-stepname", "Output" );
}
if ( combinerTransformationFile != null ) {
conf.setCombinerClass( GenericTransCombiner.class );
transMeta = new TransMeta( combinerTransformationFile );
transConfig = new TransConfiguration( transMeta, transExecConfig );
conf.set( "transformation-combiner-xml", transConfig.getXML() );
conf.set( "transformation-combiner-input-stepname", "Injector" );
conf.set( "transformation-combiner-output-stepname", "Output" );
}
if ( reducerTransformationFile != null ) {
conf.setReducerClass( GenericTransReduce.class );
transMeta = new TransMeta( reducerTransformationFile );
transConfig = new TransConfiguration( transMeta, transExecConfig );
conf.set( "transformation-reduce-xml", transConfig.getXML() );
conf.set( "transformation-reduce-input-stepname", "Injector" );
conf.set( "transformation-reduce-output-stepname", "Output" );
}
conf.setOutputKeyClass( Text.class );
conf.setOutputValueClass( IntWritable.class );
File jar = new File( "./dist/pentaho-big-data-plugin-TRUNK-SNAPSHOT.jar" );
conf.setInputFormat( TextInputFormat.class );
conf.setOutputFormat( TextOutputFormat.class );
FileInputFormat.setInputPaths( conf, new Path( "/" ) );
FileOutputFormat.setOutputPath( conf, new Path( "/" ) );
conf.set( "fs.default.name", "hdfs://" + hostname + ":" + hdfsPort );
conf.set( "mapred.job.tracker", hostname + ":" + trackerPort );
conf.setJar( jar.toURI().toURL().toExternalForm() );
conf.setWorkingDirectory( new Path( "/tmp/wordcount" ) );
return conf;
}
public static String getTransformationXml( String transFilename ) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader( new FileReader( transFilename ) );
String line = null;
while ( ( line = reader.readLine() ) != null ) {
sb.append( line + Const.CR );
}
return sb.toString();
}
public static class MockOutputCollector implements OutputCollector {
private Map<Object, ArrayList<Object>> collection = new HashMap<Object, ArrayList<Object>>();
private AtomicBoolean closed = new AtomicBoolean( false );
public void close() {
closed.set( true );
}
@Override
public void collect( Object arg0, Object arg1 ) throws IOException {
if ( closed.get() ) {
return;
}
if ( !collection.containsKey( arg0 ) ) {
collection.put( arg0, new ArrayList<Object>() );
}
collection.get( arg0 ).add( arg1 );
}
public Map<Object, ArrayList<Object>> getCollection() {
return collection;
}
}
public static class MockReporter implements Reporter {
@Override
public void progress() {
// TODO Auto-generated method stub
}
@Override
public Counter getCounter( Enum<?> arg0 ) {
// TODO Auto-generated method stub
return null;
}
@Override
public Counter getCounter( String arg0, String arg1 ) {
// TODO Auto-generated method stub
return null;
}
@Override
public InputSplit getInputSplit() throws UnsupportedOperationException {
// TODO Auto-generated method stub
return null;
}
@Override
public void incrCounter( Enum<?> arg0, long arg1 ) {
// TODO Auto-generated method stub
}
@Override
public void incrCounter( String arg0, String arg1, long arg2 ) {
// TODO Auto-generated method stub
}
@Override
public void setStatus( String arg0 ) {
// TODO Auto-generated method stub
}
}
class MockRecordReader implements RecordReader<Text, Text> {
private Iterator<String> rowIter;
int rowNum = -1;
int totalRows;
// Make them provide a pre-filled list so we don't confuse the overhead of generating strings
// with the time it takes to run the mapper
public MockRecordReader( List<String> rows ) {
totalRows = rows.size();
rowIter = rows.iterator();
}
@Override
public boolean next( Text key, Text value ) throws IOException {
if ( !rowIter.hasNext() ) {
return false;
}
rowNum++;
key.set( String.valueOf( rowNum ) );
value.set( rowIter.next() );
return true;
}
@Override
public Text createKey() {
return new Text();
}
@Override
public Text createValue() {
return new Text();
}
@Override
public long getPos() throws IOException {
return rowNum;
}
@Override
public void close() throws IOException {
}
@Override
public float getProgress() throws IOException {
return ( rowNum + 1 ) / totalRows;
}
}
@Test
public void testMapRunnable_wordCount() throws IOException, KettleException {
PentahoMapRunnable mapRunnable = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
mapRunnable.configure( createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr" ) );
final int ROWS = 10000;
List<String> strings = new ArrayList<String>();
for ( int i = 0; i < ROWS; i++ ) {
strings.add( "zebra giraffe hippo elephant tiger" );
}
MockRecordReader reader = new MockRecordReader( strings );
long start = System.currentTimeMillis();
mapRunnable.run( reader, outputCollector, reporter );
outputCollector.close();
long stop = System.currentTimeMillis();
System.out.println( "Executed " + ROWS + " in " + ( stop - start ) + "ms" );
System.out.println( "Average: " + ( ( stop - start ) / (float) ROWS ) + "ms" );
System.out.println( "Rows/Second: " + ( ROWS / ( ( stop - start ) / 1000f ) ) );
class CountValues {
private Object workingKey;
public CountValues setWorkingKey( Object k ) {
workingKey = k;
return this;
}
public void countValues( String k, Object v, MockOutputCollector oc ) {
if ( workingKey.equals( new Text( k ) ) ) {
assertEquals( k.toString(), v, oc.getCollection().get( new Text( k ) ).size() );
}
}
}
assertNotNull( outputCollector );
assertNotNull( outputCollector.getCollection() );
assertNotNull( outputCollector.getCollection().keySet() );
assertEquals( 5, outputCollector.getCollection().keySet().size() );
CountValues cv = new CountValues();
for ( Object key : outputCollector.getCollection().keySet() ) {
cv.setWorkingKey( key ).countValues( "zebra", ROWS, outputCollector );
cv.setWorkingKey( key ).countValues( "giraffe", ROWS, outputCollector );
cv.setWorkingKey( key ).countValues( "hippo", ROWS, outputCollector );
cv.setWorkingKey( key ).countValues( "elephant", ROWS, outputCollector );
cv.setWorkingKey( key ).countValues( "tiger", ROWS, outputCollector );
// TODO: Add words that will not exist: unique words, same words - diff case
if ( debug ) {
for ( Object value : outputCollector.getCollection().get( key ) ) {
System.out.println( key + ": " + value );
}
}
}
GenericTransReduce reducer = new GenericTransReduce();
MockOutputCollector inputCollector = outputCollector;
outputCollector = new MockOutputCollector();
reducer.configure( createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr" ) );
start = System.currentTimeMillis();
for ( Object key : inputCollector.getCollection().keySet() ) {
System.out.println( "reducing: " + key );
reducer.reduce( (Text) key, new ArrayList( inputCollector.getCollection().get( key ) ).iterator(),
outputCollector, reporter );
}
reducer.close();
outputCollector.close();
stop = System.currentTimeMillis();
System.out.println( "Executed " + ROWS + " in " + ( stop - start ) + "ms" );
System.out.println( "Average: " + ( ( stop - start ) / (float) ROWS ) + "ms" );
System.out.println( "Rows/Second: " + ( ROWS / ( ( stop - start ) / 1000f ) ) );
assertNotNull( outputCollector );
assertNotNull( outputCollector.getCollection() );
assertNotNull( outputCollector.getCollection().keySet() );
assertEquals( 5, outputCollector.getCollection().keySet().size() );
class CheckValues {
private Object workingKey;
public CheckValues setWorkingKey( Object k ) {
workingKey = k;
return this;
}
public void checkValues( String k, Object v, MockOutputCollector oc ) {
if ( workingKey.equals( new Text( k ) ) ) {
assertEquals( k.toString(), v, ( (IntWritable) oc.getCollection().get( new Text( k ) ).get( 0 ) ).get() );
}
}
}
CheckValues cv2 = new CheckValues();
for ( Object key : outputCollector.getCollection().keySet() ) {
cv2.setWorkingKey( key ).checkValues( "zebra", ROWS, outputCollector );
cv2.setWorkingKey( key ).checkValues( "giraffe", ROWS, outputCollector );
cv2.setWorkingKey( key ).checkValues( "hippo", ROWS, outputCollector );
cv2.setWorkingKey( key ).checkValues( "elephant", ROWS, outputCollector );
cv2.setWorkingKey( key ).checkValues( "tiger", ROWS, outputCollector );
if ( debug ) {
for ( Object value : outputCollector.getCollection().get( key ) ) {
System.out.println( key + ": " + value );
}
}
}
}
@Test
public void testMapper_null_output_value() throws Exception {
PentahoMapRunnable mapper = new PentahoMapRunnable();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
mapper.configure( createJobConf( "./src/test/resources/null-test.ktr", "./src/test/resources/null-test.ktr" ) );
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
mapper.run( reader, outputCollector, reporter );
outputCollector.close();
Exception ex = mapper.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Received output when we didn't expect any. <null>s aren't passed through.", 0, outputCollector
.getCollection().size() );
}
@Test
public void testCombiner_null_output_value() throws Exception {
GenericTransCombiner combiner = new GenericTransCombiner();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
combiner.configure( createJobConf( null, "./src/test/resources/null-test.ktr", null ) );
combiner.reduce( new Text( "0" ), Arrays.asList( new Text( "test" ) ).iterator(), outputCollector, reporter );
outputCollector.close();
Exception ex = combiner.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Received output when we didn't expect any. <null>s aren't passed through.", 0, outputCollector
.getCollection().size() );
}
@Test
public void testReducer_null_output_value() throws Exception {
GenericTransReduce reducer = new GenericTransReduce();
MockOutputCollector outputCollector = new MockOutputCollector();
MockReporter reporter = new MockReporter();
reducer.configure( createJobConf( "./src/test/resources/null-test.ktr", "./src/test/resources/null-test.ktr" ) );
reducer.reduce( new Text( "0" ), Arrays.asList( new Text( "test" ) ).iterator(), outputCollector, reporter );
outputCollector.close();
Exception ex = reducer.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Received output when we didn't expect any. <null>s aren't passed through.", 0, outputCollector
.getCollection().size() );
}
@Test
public void testLogChannelLeaking_mapper() throws Exception {
JobConf jobConf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
PentahoMapRunnable mapper = new PentahoMapRunnable();
mapper.configure( jobConf );
MockReporter reporter = new MockReporter();
// We expect 4 log channels per run. The total should never grow past logChannelsBefore + 4.
final int EXPECTED_CHANNELS_PER_RUN = 5;
final int logChannels = LoggingRegistry.getInstance().getMap().size();
// Run the reducer this many times
final int RUNS = 10;
for ( int i = 0; i < RUNS; i++ ) {
MockRecordReader reader = new MockRecordReader( Arrays.asList( "test" ) );
MockOutputCollector outputCollector = new MockOutputCollector();
mapper.run( reader, outputCollector, reporter );
outputCollector.close();
Exception ex = mapper.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Incorrect output", 1, outputCollector.getCollection().size() );
assertEquals( "LogChannels are not being cleaned up. On Run #" + i + " we have too many.", logChannels
+ EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
assertEquals( logChannels + EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
@Test
public void testLogChannelLeaking_combiner() throws Exception {
JobConf jobConf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
List<IntWritable> input = Arrays.asList( new IntWritable( 1 ) );
GenericTransCombiner combiner = new GenericTransCombiner();
combiner.configure( jobConf );
MockReporter reporter = new MockReporter();
// We expect 4 log channels per run. The total should never grow past logChannelsBefore + 4.
final int EXPECTED_CHANNELS_PER_RUN = 4;
final int logChannels = LoggingRegistry.getInstance().getMap().size();
// Run the reducer this many times
final int RUNS = 10;
for ( int i = 0; i < RUNS; i++ ) {
MockOutputCollector outputCollector = new MockOutputCollector();
combiner.reduce( new Text( String.valueOf( i ) ), input.iterator(), outputCollector, reporter );
combiner.close();
outputCollector.close();
Exception ex = combiner.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Incorrect output", 1, outputCollector.getCollection().size() );
assertEquals( "LogChannels are not being cleaned up. On Run #" + i + " we have too many.", logChannels
+ EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
assertEquals( logChannels + EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
@Test
public void testLogChannelLeaking_reducer() throws Exception {
JobConf jobConf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
List<IntWritable> input = Arrays.asList( new IntWritable( 1 ) );
GenericTransReduce reducer = new GenericTransReduce();
reducer.configure( jobConf );
MockReporter reporter = new MockReporter();
// We expect 4 log channels per run. The total should never grow past logChannelsBefore + 4.
final int EXPECTED_CHANNELS_PER_RUN = 4;
final int logChannels = LoggingRegistry.getInstance().getMap().size();
// Run the reducer this many times
final int RUNS = 10;
for ( int i = 0; i < RUNS; i++ ) {
MockOutputCollector outputCollector = new MockOutputCollector();
reducer.reduce( new Text( String.valueOf( i ) ), input.iterator(), outputCollector, reporter );
reducer.close();
outputCollector.close();
Exception ex = reducer.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Incorrect output", 1, outputCollector.getCollection().size() );
assertEquals( "LogChannels are not being cleaned up. On Run #" + i + " we have too many.", logChannels
+ EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
assertEquals( logChannels + EXPECTED_CHANNELS_PER_RUN, LoggingRegistry.getInstance().getMap().size() );
}
// TODO Create tests for exception propogation from RowListeners in Mapper, Combiner, and Reducer
@Test
public void testMapReduce_InputOutput() throws Exception {
JobConf jobConf =
createJobConf( "./src/test/resources/mr-input-output.ktr", "./src/test/resources/mr-passthrough.ktr",
"./src/test/resources/mr-passthrough.ktr" );
PentahoMapRunnable mapper = new PentahoMapRunnable();
mapper.configure( jobConf );
MockReporter reporter = new MockReporter();
MockOutputCollector outputCollector = new MockOutputCollector();
MockRecordReader reader = new MockRecordReader( Arrays.asList( "1", "2", "3" ) );
mapper.run( reader, outputCollector, reporter );
outputCollector.close();
Exception ex = mapper.getException();
if ( ex != null ) {
ex.printStackTrace();
}
assertNull( "Exception thrown", ex );
assertEquals( "Incorrect output", 3, outputCollector.getCollection().size() );
assertEquals( "Validating output collector", new IntWritable( 0 ), outputCollector.getCollection().get(
new Text( "1" ) ).get( 0 ) );
assertEquals( "Validating output collector", new IntWritable( 1 ), outputCollector.getCollection().get(
new Text( "2" ) ).get( 0 ) );
assertEquals( "Validating output collector", new IntWritable( 2 ), outputCollector.getCollection().get(
new Text( "3" ) ).get( 0 ) );
}
@Test
public void testCombinerOutputClasses() throws IOException, KettleException {
JobConf jobConf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
jobConf.setMapOutputKeyClass( Text.class );
jobConf.setMapOutputValueClass( IntWritable.class );
jobConf.setOutputValueClass( NullWritable.class );
jobConf.setOutputValueClass( LongWritable.class );
GenericTransCombiner combiner = new GenericTransCombiner();
combiner.configure( jobConf );
assertEquals( jobConf.getMapOutputKeyClass(), combiner.getOutClassK() );
assertEquals( jobConf.getMapOutputValueClass(), combiner.getOutClassV() );
}
@Test
public void testReducerOutputClasses() throws IOException, KettleException {
JobConf jobConf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
jobConf.setMapOutputKeyClass( Text.class );
jobConf.setMapOutputValueClass( IntWritable.class );
jobConf.setOutputValueClass( NullWritable.class );
jobConf.setOutputValueClass( LongWritable.class );
GenericTransReduce reducer = new GenericTransReduce();
reducer.configure( jobConf );
assertEquals( jobConf.getOutputKeyClass(), reducer.getOutClassK() );
assertEquals( jobConf.getOutputValueClass(), reducer.getOutClassV() );
}
@Test
public void testTaskIdExtraction() throws Exception {
JobConf conf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
conf.set( "mapred.task.id", "job_201208090841_0133" );
PentahoMapRunnable mapRunnable = new PentahoMapRunnable();
mapRunnable.configure( conf );
Field variableSpaceField = PentahoMapRunnable.class.getDeclaredField( "variableSpace" );
variableSpaceField.setAccessible( true );
VariableSpace variableSpace = (VariableSpace) variableSpaceField.get( mapRunnable );
String s = variableSpace.getVariable( "Internal.Hadoop.NodeNumber" );
assertEquals( "133", s );
}
@Test
public void testTaskIdExtraction_over_10000() throws Exception {
JobConf conf =
createJobConf( "./src/it/resources/wordcount-mapper.ktr", "./src/it/resources/wordcount-reducer.ktr",
"./src/it/resources/wordcount-reducer.ktr" );
conf.set( "mapred.task.id", "job_201208090841_013302" );
PentahoMapRunnable mapRunnable = new PentahoMapRunnable();
mapRunnable.configure( conf );
Field variableSpaceField = PentahoMapRunnable.class.getDeclaredField( "variableSpace" );
variableSpaceField.setAccessible( true );
VariableSpace variableSpace = (VariableSpace) variableSpaceField.get( mapRunnable );
String s = variableSpace.getVariable( "Internal.Hadoop.NodeNumber" );
assertEquals( "13302", s );
}
}