source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestFieldSelection.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 */
18package org.apache.hadoop.mapred;
19
20import org.apache.hadoop.fs.*;
21import org.apache.hadoop.io.*;
22import org.apache.hadoop.mapred.lib.*;
23import junit.framework.TestCase;
24import java.text.NumberFormat;
25
26public class TestFieldSelection extends TestCase {
27
28private static NumberFormat idFormat = NumberFormat.getInstance();
29  static {
30    idFormat.setMinimumIntegerDigits(4);
31    idFormat.setGroupingUsed(false);
32  }
33
34  public void testFieldSelection() throws Exception {
35    launch();
36  }
37
38  public static void launch() throws Exception {
39    JobConf conf = new JobConf(TestFieldSelection.class);
40    FileSystem fs = FileSystem.get(conf);
41    int numOfInputLines = 10;
42
43    Path OUTPUT_DIR = new Path("build/test/output_for_field_selection_test");
44    Path INPUT_DIR = new Path("build/test/input_for_field_selection_test");
45    String inputFile = "input.txt";
46    fs.delete(INPUT_DIR, true);
47    fs.mkdirs(INPUT_DIR);
48    fs.delete(OUTPUT_DIR, true);
49
50    StringBuffer inputData = new StringBuffer();
51    StringBuffer expectedOutput = new StringBuffer();
52
53    FSDataOutputStream fileOut = fs.create(new Path(INPUT_DIR, inputFile));
54    for (int i = 0; i < numOfInputLines; i++) {
55        inputData.append(idFormat.format(i));
56        inputData.append("-").append(idFormat.format(i+1));
57        inputData.append("-").append(idFormat.format(i+2));
58        inputData.append("-").append(idFormat.format(i+3));
59        inputData.append("-").append(idFormat.format(i+4));
60        inputData.append("-").append(idFormat.format(i+5));
61        inputData.append("-").append(idFormat.format(i+6));
62        inputData.append("\n");
63
64
65        expectedOutput.append(idFormat.format(i+3));
66        expectedOutput.append("-" ).append (idFormat.format(i+2));
67        expectedOutput.append("-" ).append (idFormat.format(i+1));
68        expectedOutput.append("-" ).append (idFormat.format(i+5));
69        expectedOutput.append("-" ).append (idFormat.format(i+6));
70
71        expectedOutput.append("-" ).append (idFormat.format(i+6));
72        expectedOutput.append("-" ).append (idFormat.format(i+5));
73        expectedOutput.append("-" ).append (idFormat.format(i+1));
74        expectedOutput.append("-" ).append (idFormat.format(i+2));
75        expectedOutput.append("-" ).append (idFormat.format(i+3));
76
77        expectedOutput.append("-" ).append (idFormat.format(i+0));
78        expectedOutput.append("-" ).append (idFormat.format(i+1));
79        expectedOutput.append("-" ).append (idFormat.format(i+2));
80        expectedOutput.append("-" ).append (idFormat.format(i+3));
81        expectedOutput.append("-" ).append (idFormat.format(i+4));
82        expectedOutput.append("-" ).append (idFormat.format(i+5));
83        expectedOutput.append("-" ).append (idFormat.format(i+6));
84        expectedOutput.append("\n");
85    }
86    fileOut.write(inputData.toString().getBytes("utf-8"));
87    fileOut.close();
88
89    System.out.println("inputData:");
90    System.out.println(inputData.toString());
91    JobConf job = new JobConf(conf, TestFieldSelection.class);
92    FileInputFormat.setInputPaths(job, INPUT_DIR);
93    job.setInputFormat(TextInputFormat.class);
94    job.setMapperClass(FieldSelectionMapReduce.class);
95    job.setReducerClass(FieldSelectionMapReduce.class);
96
97    FileOutputFormat.setOutputPath(job, OUTPUT_DIR);
98    job.setOutputKeyClass(Text.class);
99    job.setOutputValueClass(Text.class);
100    job.setOutputFormat(TextOutputFormat.class);
101    job.setNumReduceTasks(1);
102
103    job.set("mapred.data.field.separator", "-");
104    job.set("map.output.key.value.fields.spec", "6,5,1-3:0-");
105    job.set("reduce.output.key.value.fields.spec", ":4,3,2,1,0,0-");
106
107    JobClient.runJob(job);
108
109    //
110    // Finally, we compare the reconstructed answer key with the
111    // original one.  Remember, we need to ignore zero-count items
112    // in the original key.
113    //
114    boolean success = true;
115    Path outPath = new Path(OUTPUT_DIR, "part-00000");
116    String outdata = TestMiniMRWithDFS.readOutput(outPath,job);
117
118    assertEquals(expectedOutput.toString(),outdata);
119    fs.delete(OUTPUT_DIR, true);
120    fs.delete(INPUT_DIR, true);
121  }
122
123  /**
124   * Launches all the tasks in order.
125   */
126  public static void main(String[] argv) throws Exception {
127    launch();
128  }
129}
Note: See TracBrowser for help on using the repository browser.