source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestFileOutputFormat.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: 4.9 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 */
18
19package org.apache.hadoop.mapred;
20
21import org.apache.hadoop.fs.FileStatus;
22import org.apache.hadoop.fs.FileSystem;
23import org.apache.hadoop.fs.Path;
24import org.apache.hadoop.io.LongWritable;
25import org.apache.hadoop.io.Text;
26
27import java.io.DataOutputStream;
28import java.io.IOException;
29import java.io.OutputStream;
30import java.util.Iterator;
31
32public class TestFileOutputFormat extends HadoopTestCase {
33
34  public TestFileOutputFormat() throws IOException {
35    super(HadoopTestCase.CLUSTER_MR, HadoopTestCase.LOCAL_FS, 1, 1);
36  }
37
38  public void testCustomFile() throws Exception {
39    Path inDir = new Path("testing/fileoutputformat/input");
40    Path outDir = new Path("testing/fileoutputformat/output");
41
42    // Hack for local FS that does not have the concept of a 'mounting point'
43    if (isLocalFS()) {
44      String localPathRoot = System.getProperty("test.build.data", "/tmp")
45        .replace(' ', '+');
46      inDir = new Path(localPathRoot, inDir);
47      outDir = new Path(localPathRoot, outDir);
48    }
49
50
51    JobConf conf = createJobConf();
52    FileSystem fs = FileSystem.get(conf);
53
54    fs.delete(outDir, true);
55    if (!fs.mkdirs(inDir)) {
56      throw new IOException("Mkdirs failed to create " + inDir.toString());
57    }
58
59    DataOutputStream file = fs.create(new Path(inDir, "part-0"));
60    file.writeBytes("a\nb\n\nc\nd\ne");
61    file.close();
62
63    file = fs.create(new Path(inDir, "part-1"));
64    file.writeBytes("a\nb\n\nc\nd\ne");
65    file.close();
66
67    conf.setJobName("fof");
68    conf.setInputFormat(TextInputFormat.class);
69
70    conf.setOutputKeyClass(LongWritable.class);
71    conf.setOutputValueClass(Text.class);
72
73    conf.setMapOutputKeyClass(LongWritable.class);
74    conf.setMapOutputValueClass(Text.class);
75
76    conf.setOutputFormat(TextOutputFormat.class);
77    conf.setOutputKeyClass(LongWritable.class);
78    conf.setOutputValueClass(Text.class);
79
80    conf.setMapperClass(TestMap.class);
81    conf.setReducerClass(TestReduce.class);
82
83    FileInputFormat.setInputPaths(conf, inDir);
84    FileOutputFormat.setOutputPath(conf, outDir);
85
86    JobClient jc = new JobClient(conf);
87    RunningJob job = jc.submitJob(conf);
88    while (!job.isComplete()) {
89      Thread.sleep(100);
90    }
91    assertTrue(job.isSuccessful());
92
93    boolean map0 = false;
94    boolean map1 = false;
95    boolean reduce = false;
96    FileStatus[] statuses = fs.listStatus(outDir);
97    for (FileStatus status : statuses) {
98      map0 = map0 || status.getPath().getName().equals("test-m-00000");
99      map1 = map1 || status.getPath().getName().equals("test-m-00001");
100      reduce = reduce || status.getPath().getName().equals("test-r-00000");
101    }
102
103    assertTrue(map0);
104    assertTrue(map1);
105    assertTrue(reduce);
106  }
107
108  public static class TestMap implements Mapper<LongWritable, Text,
109    LongWritable, Text> {
110
111    public void configure(JobConf conf) {
112      try {
113        FileSystem fs = FileSystem.get(conf);
114        OutputStream os =
115          fs.create(FileOutputFormat.getPathForCustomFile(conf, "test"));
116        os.write(1);
117        os.close();
118      }
119      catch (IOException ex) {
120        throw new RuntimeException(ex);
121      }
122    }
123
124    public void map(LongWritable key, Text value,
125                    OutputCollector<LongWritable, Text> output,
126                    Reporter reporter) throws IOException {
127      output.collect(key, value);
128    }
129
130    public void close() throws IOException {
131    }
132  }
133
134  public static class TestReduce implements Reducer<LongWritable, Text,
135    LongWritable, Text> {
136
137    public void configure(JobConf conf) {
138      try {
139        FileSystem fs = FileSystem.get(conf);
140        OutputStream os =
141          fs.create(FileOutputFormat.getPathForCustomFile(conf, "test"));
142        os.write(1);
143        os.close();
144      }
145      catch (IOException ex) {
146        throw new RuntimeException(ex);
147      }
148    }
149
150    public void reduce(LongWritable key, Iterator<Text> values,
151                       OutputCollector<LongWritable, Text> output,
152                       Reporter reporter) throws IOException {
153      while (values.hasNext()) {
154        Text value = values.next();
155        output.collect(key, value);
156      }
157    }
158
159    public void close() throws IOException {
160    }
161  }
162
163}
Note: See TracBrowser for help on using the repository browser.