source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestUserDefinedCounters.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.mapred;
19
20import java.io.BufferedReader;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.InputStreamReader;
24import java.io.OutputStream;
25import java.io.OutputStreamWriter;
26import java.io.Writer;
27
28import org.apache.hadoop.fs.FileUtil;
29import org.apache.hadoop.fs.Path;
30import org.apache.hadoop.io.LongWritable;
31import org.apache.hadoop.io.Text;
32import org.apache.hadoop.mapred.lib.IdentityMapper;
33import org.apache.hadoop.mapred.lib.IdentityReducer;
34
35public class TestUserDefinedCounters extends ClusterMapReduceTestCase {
36 
37  enum EnumCounter { MAP_RECORDS }
38 
39  static class CountingMapper<K, V> extends IdentityMapper<K, V> {
40
41    public void map(K key, V value,
42        OutputCollector<K, V> output, Reporter reporter)
43        throws IOException {
44      output.collect(key, value);
45      reporter.incrCounter(EnumCounter.MAP_RECORDS, 1);
46      reporter.incrCounter("StringCounter", "MapRecords", 1);
47    }
48
49  }
50 
51  public void testMapReduceJob() throws Exception {
52    OutputStream os = getFileSystem().create(new Path(getInputDir(), "text.txt"));
53    Writer wr = new OutputStreamWriter(os);
54    wr.write("hello1\n");
55    wr.write("hello2\n");
56    wr.write("hello3\n");
57    wr.write("hello4\n");
58    wr.close();
59
60    JobConf conf = createJobConf();
61    conf.setJobName("counters");
62   
63    conf.setInputFormat(TextInputFormat.class);
64
65    conf.setMapOutputKeyClass(LongWritable.class);
66    conf.setMapOutputValueClass(Text.class);
67
68    conf.setOutputFormat(TextOutputFormat.class);
69    conf.setOutputKeyClass(LongWritable.class);
70    conf.setOutputValueClass(Text.class);
71
72    conf.setMapperClass(CountingMapper.class);
73    conf.setReducerClass(IdentityReducer.class);
74
75    FileInputFormat.setInputPaths(conf, getInputDir());
76
77    FileOutputFormat.setOutputPath(conf, getOutputDir());
78
79    RunningJob runningJob = JobClient.runJob(conf);
80
81    Path[] outputFiles = FileUtil.stat2Paths(
82                           getFileSystem().listStatus(getOutputDir(),
83                           new OutputLogFilter()));
84    if (outputFiles.length > 0) {
85      InputStream is = getFileSystem().open(outputFiles[0]);
86      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
87      String line = reader.readLine();
88      int counter = 0;
89      while (line != null) {
90        counter++;
91        assertTrue(line.contains("hello"));
92        line = reader.readLine();
93      }
94      reader.close();
95      assertEquals(4, counter);
96    }
97   
98    assertEquals(4,
99        runningJob.getCounters().getCounter(EnumCounter.MAP_RECORDS));
100    assertEquals(4,
101        runningJob.getCounters().getGroup("StringCounter")
102        .getCounter("MapRecords"));
103  }
104
105}
Note: See TracBrowser for help on using the repository browser.