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