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.lib; |
---|
20 | |
---|
21 | import java.io.*; |
---|
22 | import java.util.*; |
---|
23 | import junit.framework.TestCase; |
---|
24 | |
---|
25 | import org.apache.hadoop.fs.*; |
---|
26 | import org.apache.hadoop.io.*; |
---|
27 | import org.apache.hadoop.mapred.*; |
---|
28 | |
---|
29 | public class TestLineInputFormat extends TestCase { |
---|
30 | private static int MAX_LENGTH = 200; |
---|
31 | |
---|
32 | private static JobConf defaultConf = new JobConf(); |
---|
33 | private static FileSystem localFs = null; |
---|
34 | |
---|
35 | static { |
---|
36 | try { |
---|
37 | localFs = FileSystem.getLocal(defaultConf); |
---|
38 | } catch (IOException e) { |
---|
39 | throw new RuntimeException("init failure", e); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | private static Path workDir = |
---|
44 | new Path(new Path(System.getProperty("test.build.data", "."), "data"), |
---|
45 | "TestLineInputFormat"); |
---|
46 | |
---|
47 | public void testFormat() throws Exception { |
---|
48 | JobConf job = new JobConf(); |
---|
49 | Path file = new Path(workDir, "test.txt"); |
---|
50 | |
---|
51 | int seed = new Random().nextInt(); |
---|
52 | Random random = new Random(seed); |
---|
53 | |
---|
54 | localFs.delete(workDir, true); |
---|
55 | FileInputFormat.setInputPaths(job, workDir); |
---|
56 | int numLinesPerMap = 5; |
---|
57 | job.setInt("mapred.line.input.format.linespermap", numLinesPerMap); |
---|
58 | |
---|
59 | // for a variety of lengths |
---|
60 | for (int length = 0; length < MAX_LENGTH; |
---|
61 | length += random.nextInt(MAX_LENGTH/10) + 1) { |
---|
62 | // create a file with length entries |
---|
63 | Writer writer = new OutputStreamWriter(localFs.create(file)); |
---|
64 | try { |
---|
65 | for (int i = 0; i < length; i++) { |
---|
66 | writer.write(Integer.toString(i)); |
---|
67 | writer.write("\n"); |
---|
68 | } |
---|
69 | } finally { |
---|
70 | writer.close(); |
---|
71 | } |
---|
72 | checkFormat(job, numLinesPerMap); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | // A reporter that does nothing |
---|
77 | private static final Reporter voidReporter = Reporter.NULL; |
---|
78 | |
---|
79 | void checkFormat(JobConf job, int expectedN) throws IOException{ |
---|
80 | NLineInputFormat format = new NLineInputFormat(); |
---|
81 | format.configure(job); |
---|
82 | int ignoredNumSplits = 1; |
---|
83 | InputSplit[] splits = format.getSplits(job, ignoredNumSplits); |
---|
84 | |
---|
85 | // check all splits except last one |
---|
86 | int count = 0; |
---|
87 | for (int j = 0; j < splits.length -1; j++) { |
---|
88 | assertEquals("There are no split locations", 0, |
---|
89 | splits[j].getLocations().length); |
---|
90 | RecordReader<LongWritable, Text> reader = |
---|
91 | format.getRecordReader(splits[j], job, voidReporter); |
---|
92 | Class readerClass = reader.getClass(); |
---|
93 | assertEquals("reader class is LineRecordReader.", |
---|
94 | LineRecordReader.class, readerClass); |
---|
95 | LongWritable key = reader.createKey(); |
---|
96 | Class keyClass = key.getClass(); |
---|
97 | assertEquals("Key class is LongWritable.", LongWritable.class, keyClass); |
---|
98 | Text value = reader.createValue(); |
---|
99 | Class valueClass = value.getClass(); |
---|
100 | assertEquals("Value class is Text.", Text.class, valueClass); |
---|
101 | |
---|
102 | try { |
---|
103 | count = 0; |
---|
104 | while (reader.next(key, value)) { |
---|
105 | count++; |
---|
106 | } |
---|
107 | } finally { |
---|
108 | reader.close(); |
---|
109 | } |
---|
110 | assertEquals("number of lines in split is " + expectedN , |
---|
111 | expectedN, count); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | public static void main(String[] args) throws Exception { |
---|
116 | new TestLineInputFormat().testFormat(); |
---|
117 | } |
---|
118 | } |
---|