[120] | 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 | |
---|
| 19 | package org.apache.hadoop.mapred; |
---|
| 20 | |
---|
| 21 | import java.io.*; |
---|
| 22 | import junit.framework.TestCase; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.fs.*; |
---|
| 25 | import org.apache.hadoop.io.*; |
---|
| 26 | |
---|
| 27 | public class TestTextOutputFormat extends TestCase { |
---|
| 28 | private static JobConf defaultConf = new JobConf(); |
---|
| 29 | |
---|
| 30 | private static FileSystem localFs = null; |
---|
| 31 | static { |
---|
| 32 | try { |
---|
| 33 | localFs = FileSystem.getLocal(defaultConf); |
---|
| 34 | } catch (IOException e) { |
---|
| 35 | throw new RuntimeException("init failure", e); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | // A random task attempt id for testing. |
---|
| 39 | private static String attempt = "attempt_200707121733_0001_m_000000_0"; |
---|
| 40 | |
---|
| 41 | private static Path workDir = |
---|
| 42 | new Path(new Path( |
---|
| 43 | new Path(System.getProperty("test.build.data", "."), |
---|
| 44 | "data"), |
---|
| 45 | FileOutputCommitter.TEMP_DIR_NAME), "_" + attempt); |
---|
| 46 | |
---|
| 47 | @SuppressWarnings("unchecked") |
---|
| 48 | public void testFormat() throws Exception { |
---|
| 49 | JobConf job = new JobConf(); |
---|
| 50 | job.set("mapred.task.id", attempt); |
---|
| 51 | FileOutputFormat.setOutputPath(job, workDir.getParent().getParent()); |
---|
| 52 | FileOutputFormat.setWorkOutputPath(job, workDir); |
---|
| 53 | FileSystem fs = workDir.getFileSystem(job); |
---|
| 54 | if (!fs.mkdirs(workDir)) { |
---|
| 55 | fail("Failed to create output directory"); |
---|
| 56 | } |
---|
| 57 | String file = "test.txt"; |
---|
| 58 | |
---|
| 59 | // A reporter that does nothing |
---|
| 60 | Reporter reporter = Reporter.NULL; |
---|
| 61 | |
---|
| 62 | TextOutputFormat theOutputFormat = new TextOutputFormat(); |
---|
| 63 | RecordWriter theRecordWriter = |
---|
| 64 | theOutputFormat.getRecordWriter(localFs, job, file, reporter); |
---|
| 65 | |
---|
| 66 | Text key1 = new Text("key1"); |
---|
| 67 | Text key2 = new Text("key2"); |
---|
| 68 | Text val1 = new Text("val1"); |
---|
| 69 | Text val2 = new Text("val2"); |
---|
| 70 | NullWritable nullWritable = NullWritable.get(); |
---|
| 71 | |
---|
| 72 | try { |
---|
| 73 | theRecordWriter.write(key1, val1); |
---|
| 74 | theRecordWriter.write(null, nullWritable); |
---|
| 75 | theRecordWriter.write(null, val1); |
---|
| 76 | theRecordWriter.write(nullWritable, val2); |
---|
| 77 | theRecordWriter.write(key2, nullWritable); |
---|
| 78 | theRecordWriter.write(key1, null); |
---|
| 79 | theRecordWriter.write(null, null); |
---|
| 80 | theRecordWriter.write(key2, val2); |
---|
| 81 | |
---|
| 82 | } finally { |
---|
| 83 | theRecordWriter.close(reporter); |
---|
| 84 | } |
---|
| 85 | File expectedFile = new File(new Path(workDir, file).toString()); |
---|
| 86 | StringBuffer expectedOutput = new StringBuffer(); |
---|
| 87 | expectedOutput.append(key1).append('\t').append(val1).append("\n"); |
---|
| 88 | expectedOutput.append(val1).append("\n"); |
---|
| 89 | expectedOutput.append(val2).append("\n"); |
---|
| 90 | expectedOutput.append(key2).append("\n"); |
---|
| 91 | expectedOutput.append(key1).append("\n"); |
---|
| 92 | expectedOutput.append(key2).append('\t').append(val2).append("\n"); |
---|
| 93 | String output = UtilsForTests.slurp(expectedFile); |
---|
| 94 | assertEquals(output, expectedOutput.toString()); |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | @SuppressWarnings("unchecked") |
---|
| 99 | public void testFormatWithCustomSeparator() throws Exception { |
---|
| 100 | JobConf job = new JobConf(); |
---|
| 101 | String separator = "\u0001"; |
---|
| 102 | job.set("mapred.textoutputformat.separator", separator); |
---|
| 103 | job.set("mapred.task.id", attempt); |
---|
| 104 | FileOutputFormat.setOutputPath(job, workDir.getParent().getParent()); |
---|
| 105 | FileOutputFormat.setWorkOutputPath(job, workDir); |
---|
| 106 | FileSystem fs = workDir.getFileSystem(job); |
---|
| 107 | if (!fs.mkdirs(workDir)) { |
---|
| 108 | fail("Failed to create output directory"); |
---|
| 109 | } |
---|
| 110 | String file = "test.txt"; |
---|
| 111 | |
---|
| 112 | // A reporter that does nothing |
---|
| 113 | Reporter reporter = Reporter.NULL; |
---|
| 114 | |
---|
| 115 | TextOutputFormat theOutputFormat = new TextOutputFormat(); |
---|
| 116 | RecordWriter theRecordWriter = |
---|
| 117 | theOutputFormat.getRecordWriter(localFs, job, file, reporter); |
---|
| 118 | |
---|
| 119 | Text key1 = new Text("key1"); |
---|
| 120 | Text key2 = new Text("key2"); |
---|
| 121 | Text val1 = new Text("val1"); |
---|
| 122 | Text val2 = new Text("val2"); |
---|
| 123 | NullWritable nullWritable = NullWritable.get(); |
---|
| 124 | |
---|
| 125 | try { |
---|
| 126 | theRecordWriter.write(key1, val1); |
---|
| 127 | theRecordWriter.write(null, nullWritable); |
---|
| 128 | theRecordWriter.write(null, val1); |
---|
| 129 | theRecordWriter.write(nullWritable, val2); |
---|
| 130 | theRecordWriter.write(key2, nullWritable); |
---|
| 131 | theRecordWriter.write(key1, null); |
---|
| 132 | theRecordWriter.write(null, null); |
---|
| 133 | theRecordWriter.write(key2, val2); |
---|
| 134 | |
---|
| 135 | } finally { |
---|
| 136 | theRecordWriter.close(reporter); |
---|
| 137 | } |
---|
| 138 | File expectedFile = new File(new Path(workDir, file).toString()); |
---|
| 139 | StringBuffer expectedOutput = new StringBuffer(); |
---|
| 140 | expectedOutput.append(key1).append(separator).append(val1).append("\n"); |
---|
| 141 | expectedOutput.append(val1).append("\n"); |
---|
| 142 | expectedOutput.append(val2).append("\n"); |
---|
| 143 | expectedOutput.append(key2).append("\n"); |
---|
| 144 | expectedOutput.append(key1).append("\n"); |
---|
| 145 | expectedOutput.append(key2).append(separator).append(val2).append("\n"); |
---|
| 146 | String output = UtilsForTests.slurp(expectedFile); |
---|
| 147 | assertEquals(output, expectedOutput.toString()); |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | public static void main(String[] args) throws Exception { |
---|
| 152 | new TestTextOutputFormat().testFormat(); |
---|
| 153 | } |
---|
| 154 | } |
---|