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.IOException; |
---|
22 | import java.util.Random; |
---|
23 | |
---|
24 | import org.apache.hadoop.fs.*; |
---|
25 | import org.apache.hadoop.io.*; |
---|
26 | |
---|
27 | import junit.framework.TestCase; |
---|
28 | import org.apache.commons.logging.*; |
---|
29 | |
---|
30 | public class TestSequenceFileAsBinaryInputFormat extends TestCase { |
---|
31 | private static final Log LOG = FileInputFormat.LOG; |
---|
32 | private static final int RECORDS = 10000; |
---|
33 | |
---|
34 | public void testBinary() throws IOException { |
---|
35 | JobConf job = new JobConf(); |
---|
36 | FileSystem fs = FileSystem.getLocal(job); |
---|
37 | Path dir = new Path(System.getProperty("test.build.data",".") + "/mapred"); |
---|
38 | Path file = new Path(dir, "testbinary.seq"); |
---|
39 | Random r = new Random(); |
---|
40 | long seed = r.nextLong(); |
---|
41 | r.setSeed(seed); |
---|
42 | |
---|
43 | fs.delete(dir, true); |
---|
44 | FileInputFormat.setInputPaths(job, dir); |
---|
45 | |
---|
46 | Text tkey = new Text(); |
---|
47 | Text tval = new Text(); |
---|
48 | |
---|
49 | SequenceFile.Writer writer = |
---|
50 | new SequenceFile.Writer(fs, job, file, Text.class, Text.class); |
---|
51 | try { |
---|
52 | for (int i = 0; i < RECORDS; ++i) { |
---|
53 | tkey.set(Integer.toString(r.nextInt(), 36)); |
---|
54 | tval.set(Long.toString(r.nextLong(), 36)); |
---|
55 | writer.append(tkey, tval); |
---|
56 | } |
---|
57 | } finally { |
---|
58 | writer.close(); |
---|
59 | } |
---|
60 | |
---|
61 | InputFormat<BytesWritable,BytesWritable> bformat = |
---|
62 | new SequenceFileAsBinaryInputFormat(); |
---|
63 | |
---|
64 | int count = 0; |
---|
65 | r.setSeed(seed); |
---|
66 | BytesWritable bkey = new BytesWritable(); |
---|
67 | BytesWritable bval = new BytesWritable(); |
---|
68 | Text cmpkey = new Text(); |
---|
69 | Text cmpval = new Text(); |
---|
70 | DataInputBuffer buf = new DataInputBuffer(); |
---|
71 | final int NUM_SPLITS = 3; |
---|
72 | FileInputFormat.setInputPaths(job, file); |
---|
73 | for (InputSplit split : bformat.getSplits(job, NUM_SPLITS)) { |
---|
74 | RecordReader<BytesWritable,BytesWritable> reader = |
---|
75 | bformat.getRecordReader(split, job, Reporter.NULL); |
---|
76 | try { |
---|
77 | while (reader.next(bkey, bval)) { |
---|
78 | tkey.set(Integer.toString(r.nextInt(), 36)); |
---|
79 | tval.set(Long.toString(r.nextLong(), 36)); |
---|
80 | buf.reset(bkey.getBytes(), bkey.getLength()); |
---|
81 | cmpkey.readFields(buf); |
---|
82 | buf.reset(bval.getBytes(), bval.getLength()); |
---|
83 | cmpval.readFields(buf); |
---|
84 | assertTrue( |
---|
85 | "Keys don't match: " + "*" + cmpkey.toString() + ":" + |
---|
86 | tkey.toString() + "*", |
---|
87 | cmpkey.toString().equals(tkey.toString())); |
---|
88 | assertTrue( |
---|
89 | "Vals don't match: " + "*" + cmpval.toString() + ":" + |
---|
90 | tval.toString() + "*", |
---|
91 | cmpval.toString().equals(tval.toString())); |
---|
92 | ++count; |
---|
93 | } |
---|
94 | } finally { |
---|
95 | reader.close(); |
---|
96 | } |
---|
97 | } |
---|
98 | assertEquals("Some records not found", RECORDS, count); |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|