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 java.util.*; |
---|
23 | import junit.framework.TestCase; |
---|
24 | |
---|
25 | import org.apache.commons.logging.*; |
---|
26 | import org.apache.hadoop.fs.*; |
---|
27 | import org.apache.hadoop.io.*; |
---|
28 | import org.apache.hadoop.io.compress.*; |
---|
29 | import org.apache.hadoop.util.LineReader; |
---|
30 | import org.apache.hadoop.util.ReflectionUtils; |
---|
31 | |
---|
32 | public class TestKeyValueTextInputFormat extends TestCase { |
---|
33 | private static final Log LOG = |
---|
34 | LogFactory.getLog(TestKeyValueTextInputFormat.class.getName()); |
---|
35 | |
---|
36 | private static int MAX_LENGTH = 10000; |
---|
37 | |
---|
38 | private static JobConf defaultConf = new JobConf(); |
---|
39 | private static FileSystem localFs = null; |
---|
40 | static { |
---|
41 | try { |
---|
42 | localFs = FileSystem.getLocal(defaultConf); |
---|
43 | } catch (IOException e) { |
---|
44 | throw new RuntimeException("init failure", e); |
---|
45 | } |
---|
46 | } |
---|
47 | private static Path workDir = |
---|
48 | new Path(new Path(System.getProperty("test.build.data", "."), "data"), |
---|
49 | "TestKeyValueTextInputFormat"); |
---|
50 | |
---|
51 | public void testFormat() throws Exception { |
---|
52 | JobConf job = new JobConf(); |
---|
53 | Path file = new Path(workDir, "test.txt"); |
---|
54 | |
---|
55 | // A reporter that does nothing |
---|
56 | Reporter reporter = Reporter.NULL; |
---|
57 | |
---|
58 | int seed = new Random().nextInt(); |
---|
59 | LOG.info("seed = "+seed); |
---|
60 | Random random = new Random(seed); |
---|
61 | |
---|
62 | localFs.delete(workDir, true); |
---|
63 | FileInputFormat.setInputPaths(job, workDir); |
---|
64 | |
---|
65 | // for a variety of lengths |
---|
66 | for (int length = 0; length < MAX_LENGTH; |
---|
67 | length+= random.nextInt(MAX_LENGTH/10)+1) { |
---|
68 | |
---|
69 | LOG.debug("creating; entries = " + length); |
---|
70 | |
---|
71 | // create a file with length entries |
---|
72 | Writer writer = new OutputStreamWriter(localFs.create(file)); |
---|
73 | try { |
---|
74 | for (int i = 0; i < length; i++) { |
---|
75 | writer.write(Integer.toString(i*2)); |
---|
76 | writer.write("\t"); |
---|
77 | writer.write(Integer.toString(i)); |
---|
78 | writer.write("\n"); |
---|
79 | } |
---|
80 | } finally { |
---|
81 | writer.close(); |
---|
82 | } |
---|
83 | |
---|
84 | // try splitting the file in a variety of sizes |
---|
85 | KeyValueTextInputFormat format = new KeyValueTextInputFormat(); |
---|
86 | format.configure(job); |
---|
87 | for (int i = 0; i < 3; i++) { |
---|
88 | int numSplits = random.nextInt(MAX_LENGTH/20)+1; |
---|
89 | LOG.debug("splitting: requesting = " + numSplits); |
---|
90 | InputSplit[] splits = format.getSplits(job, numSplits); |
---|
91 | LOG.debug("splitting: got = " + splits.length); |
---|
92 | |
---|
93 | // check each split |
---|
94 | BitSet bits = new BitSet(length); |
---|
95 | for (int j = 0; j < splits.length; j++) { |
---|
96 | LOG.debug("split["+j+"]= " + splits[j]); |
---|
97 | RecordReader<Text, Text> reader = |
---|
98 | format.getRecordReader(splits[j], job, reporter); |
---|
99 | Class readerClass = reader.getClass(); |
---|
100 | assertEquals("reader class is KeyValueLineRecordReader.", KeyValueLineRecordReader.class, readerClass); |
---|
101 | |
---|
102 | Text key = reader.createKey(); |
---|
103 | Class keyClass = key.getClass(); |
---|
104 | Text value = reader.createValue(); |
---|
105 | Class valueClass = value.getClass(); |
---|
106 | assertEquals("Key class is Text.", Text.class, keyClass); |
---|
107 | assertEquals("Value class is Text.", Text.class, valueClass); |
---|
108 | try { |
---|
109 | int count = 0; |
---|
110 | while (reader.next(key, value)) { |
---|
111 | int v = Integer.parseInt(value.toString()); |
---|
112 | LOG.debug("read " + v); |
---|
113 | if (bits.get(v)) { |
---|
114 | LOG.warn("conflict with " + v + |
---|
115 | " in split " + j + |
---|
116 | " at position "+reader.getPos()); |
---|
117 | } |
---|
118 | assertFalse("Key in multiple partitions.", bits.get(v)); |
---|
119 | bits.set(v); |
---|
120 | count++; |
---|
121 | } |
---|
122 | LOG.debug("splits["+j+"]="+splits[j]+" count=" + count); |
---|
123 | } finally { |
---|
124 | reader.close(); |
---|
125 | } |
---|
126 | } |
---|
127 | assertEquals("Some keys in no partition.", length, bits.cardinality()); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | } |
---|
132 | private LineReader makeStream(String str) throws IOException { |
---|
133 | return new LineReader(new ByteArrayInputStream |
---|
134 | (str.getBytes("UTF-8")), |
---|
135 | defaultConf); |
---|
136 | } |
---|
137 | |
---|
138 | public void testUTF8() throws Exception { |
---|
139 | LineReader in = makeStream("abcd\u20acbdcd\u20ac"); |
---|
140 | Text line = new Text(); |
---|
141 | in.readLine(line); |
---|
142 | assertEquals("readLine changed utf8 characters", |
---|
143 | "abcd\u20acbdcd\u20ac", line.toString()); |
---|
144 | in = makeStream("abc\u200axyz"); |
---|
145 | in.readLine(line); |
---|
146 | assertEquals("split on fake newline", "abc\u200axyz", line.toString()); |
---|
147 | } |
---|
148 | |
---|
149 | public void testNewLines() throws Exception { |
---|
150 | LineReader in = makeStream("a\nbb\n\nccc\rdddd\r\neeeee"); |
---|
151 | Text out = new Text(); |
---|
152 | in.readLine(out); |
---|
153 | assertEquals("line1 length", 1, out.getLength()); |
---|
154 | in.readLine(out); |
---|
155 | assertEquals("line2 length", 2, out.getLength()); |
---|
156 | in.readLine(out); |
---|
157 | assertEquals("line3 length", 0, out.getLength()); |
---|
158 | in.readLine(out); |
---|
159 | assertEquals("line4 length", 3, out.getLength()); |
---|
160 | in.readLine(out); |
---|
161 | assertEquals("line5 length", 4, out.getLength()); |
---|
162 | in.readLine(out); |
---|
163 | assertEquals("line5 length", 5, out.getLength()); |
---|
164 | assertEquals("end of file", 0, in.readLine(out)); |
---|
165 | } |
---|
166 | |
---|
167 | private static void writeFile(FileSystem fs, Path name, |
---|
168 | CompressionCodec codec, |
---|
169 | String contents) throws IOException { |
---|
170 | OutputStream stm; |
---|
171 | if (codec == null) { |
---|
172 | stm = fs.create(name); |
---|
173 | } else { |
---|
174 | stm = codec.createOutputStream(fs.create(name)); |
---|
175 | } |
---|
176 | stm.write(contents.getBytes()); |
---|
177 | stm.close(); |
---|
178 | } |
---|
179 | |
---|
180 | private static final Reporter voidReporter = Reporter.NULL; |
---|
181 | |
---|
182 | private static List<Text> readSplit(KeyValueTextInputFormat format, |
---|
183 | InputSplit split, |
---|
184 | JobConf job) throws IOException { |
---|
185 | List<Text> result = new ArrayList<Text>(); |
---|
186 | RecordReader<Text, Text> reader = format.getRecordReader(split, job, |
---|
187 | voidReporter); |
---|
188 | Text key = reader.createKey(); |
---|
189 | Text value = reader.createValue(); |
---|
190 | while (reader.next(key, value)) { |
---|
191 | result.add(value); |
---|
192 | value = (Text) reader.createValue(); |
---|
193 | } |
---|
194 | return result; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Test using the gzip codec for reading |
---|
199 | */ |
---|
200 | public static void testGzip() throws IOException { |
---|
201 | JobConf job = new JobConf(); |
---|
202 | CompressionCodec gzip = new GzipCodec(); |
---|
203 | ReflectionUtils.setConf(gzip, job); |
---|
204 | localFs.delete(workDir, true); |
---|
205 | writeFile(localFs, new Path(workDir, "part1.txt.gz"), gzip, |
---|
206 | "line-1\tthe quick\nline-2\tbrown\nline-3\tfox jumped\nline-4\tover\nline-5\t the lazy\nline-6\t dog\n"); |
---|
207 | writeFile(localFs, new Path(workDir, "part2.txt.gz"), gzip, |
---|
208 | "line-1\tthis is a test\nline-1\tof gzip\n"); |
---|
209 | FileInputFormat.setInputPaths(job, workDir); |
---|
210 | KeyValueTextInputFormat format = new KeyValueTextInputFormat(); |
---|
211 | format.configure(job); |
---|
212 | InputSplit[] splits = format.getSplits(job, 100); |
---|
213 | assertEquals("compressed splits == 2", 2, splits.length); |
---|
214 | FileSplit tmp = (FileSplit) splits[0]; |
---|
215 | if (tmp.getPath().getName().equals("part2.txt.gz")) { |
---|
216 | splits[0] = splits[1]; |
---|
217 | splits[1] = tmp; |
---|
218 | } |
---|
219 | List<Text> results = readSplit(format, splits[0], job); |
---|
220 | assertEquals("splits[0] length", 6, results.size()); |
---|
221 | assertEquals("splits[0][5]", " dog", results.get(5).toString()); |
---|
222 | results = readSplit(format, splits[1], job); |
---|
223 | assertEquals("splits[1] length", 2, results.size()); |
---|
224 | assertEquals("splits[1][0]", "this is a test", |
---|
225 | results.get(0).toString()); |
---|
226 | assertEquals("splits[1][1]", "of gzip", |
---|
227 | results.get(1).toString()); |
---|
228 | } |
---|
229 | |
---|
230 | public static void main(String[] args) throws Exception { |
---|
231 | new TestKeyValueTextInputFormat().testFormat(); |
---|
232 | } |
---|
233 | } |
---|