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 | |
---|
27 | import org.apache.hadoop.fs.*; |
---|
28 | import org.apache.hadoop.io.*; |
---|
29 | import org.apache.hadoop.conf.*; |
---|
30 | |
---|
31 | public class TestSequenceFileAsTextInputFormat extends TestCase { |
---|
32 | private static final Log LOG = FileInputFormat.LOG; |
---|
33 | |
---|
34 | private static int MAX_LENGTH = 10000; |
---|
35 | private static Configuration conf = new Configuration(); |
---|
36 | |
---|
37 | public void testFormat() throws Exception { |
---|
38 | JobConf job = new JobConf(conf); |
---|
39 | FileSystem fs = FileSystem.getLocal(conf); |
---|
40 | Path dir = new Path(System.getProperty("test.build.data",".") + "/mapred"); |
---|
41 | Path file = new Path(dir, "test.seq"); |
---|
42 | |
---|
43 | Reporter reporter = Reporter.NULL; |
---|
44 | |
---|
45 | int seed = new Random().nextInt(); |
---|
46 | //LOG.info("seed = "+seed); |
---|
47 | Random random = new Random(seed); |
---|
48 | |
---|
49 | fs.delete(dir, true); |
---|
50 | |
---|
51 | FileInputFormat.setInputPaths(job, dir); |
---|
52 | |
---|
53 | // for a variety of lengths |
---|
54 | for (int length = 0; length < MAX_LENGTH; |
---|
55 | length+= random.nextInt(MAX_LENGTH/10)+1) { |
---|
56 | |
---|
57 | //LOG.info("creating; entries = " + length); |
---|
58 | |
---|
59 | // create a file with length entries |
---|
60 | SequenceFile.Writer writer = |
---|
61 | SequenceFile.createWriter(fs, conf, file, |
---|
62 | IntWritable.class, LongWritable.class); |
---|
63 | try { |
---|
64 | for (int i = 0; i < length; i++) { |
---|
65 | IntWritable key = new IntWritable(i); |
---|
66 | LongWritable value = new LongWritable(10 * i); |
---|
67 | writer.append(key, value); |
---|
68 | } |
---|
69 | } finally { |
---|
70 | writer.close(); |
---|
71 | } |
---|
72 | |
---|
73 | // try splitting the file in a variety of sizes |
---|
74 | InputFormat<Text, Text> format = |
---|
75 | new SequenceFileAsTextInputFormat(); |
---|
76 | |
---|
77 | for (int i = 0; i < 3; i++) { |
---|
78 | int numSplits = |
---|
79 | random.nextInt(MAX_LENGTH/(SequenceFile.SYNC_INTERVAL/20))+1; |
---|
80 | //LOG.info("splitting: requesting = " + numSplits); |
---|
81 | InputSplit[] splits = format.getSplits(job, numSplits); |
---|
82 | //LOG.info("splitting: got = " + splits.length); |
---|
83 | |
---|
84 | // check each split |
---|
85 | BitSet bits = new BitSet(length); |
---|
86 | for (int j = 0; j < splits.length; j++) { |
---|
87 | RecordReader<Text, Text> reader = |
---|
88 | format.getRecordReader(splits[j], job, reporter); |
---|
89 | Class readerClass = reader.getClass(); |
---|
90 | assertEquals("reader class is SequenceFileAsTextRecordReader.", SequenceFileAsTextRecordReader.class, readerClass); |
---|
91 | Text value = reader.createValue(); |
---|
92 | Text key = reader.createKey(); |
---|
93 | try { |
---|
94 | int count = 0; |
---|
95 | while (reader.next(key, value)) { |
---|
96 | // if (bits.get(key.get())) { |
---|
97 | // LOG.info("splits["+j+"]="+splits[j]+" : " + key.get()); |
---|
98 | // LOG.info("@"+reader.getPos()); |
---|
99 | // } |
---|
100 | int keyInt = Integer.parseInt(key.toString()); |
---|
101 | assertFalse("Key in multiple partitions.", bits.get(keyInt)); |
---|
102 | bits.set(keyInt); |
---|
103 | count++; |
---|
104 | } |
---|
105 | //LOG.info("splits["+j+"]="+splits[j]+" count=" + count); |
---|
106 | } finally { |
---|
107 | reader.close(); |
---|
108 | } |
---|
109 | } |
---|
110 | assertEquals("Some keys in no partition.", length, bits.cardinality()); |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | public static void main(String[] args) throws Exception { |
---|
117 | new TestSequenceFileAsTextInputFormat().testFormat(); |
---|
118 | } |
---|
119 | } |
---|