source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestSequenceFileInputFormat.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.0 KB
Line 
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
19package org.apache.hadoop.mapred;
20
21import java.io.*;
22import java.util.*;
23import junit.framework.TestCase;
24
25import org.apache.commons.logging.*;
26
27import org.apache.hadoop.fs.*;
28import org.apache.hadoop.io.*;
29import org.apache.hadoop.conf.*;
30
31public class TestSequenceFileInputFormat 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, BytesWritable.class);
63      try {
64        for (int i = 0; i < length; i++) {
65          IntWritable key = new IntWritable(i);
66          byte[] data = new byte[random.nextInt(10)];
67          random.nextBytes(data);
68          BytesWritable value = new BytesWritable(data);
69          writer.append(key, value);
70        }
71      } finally {
72        writer.close();
73      }
74
75      // try splitting the file in a variety of sizes
76      InputFormat<IntWritable, BytesWritable> format =
77        new SequenceFileInputFormat<IntWritable, BytesWritable>();
78      IntWritable key = new IntWritable();
79      BytesWritable value = new BytesWritable();
80      for (int i = 0; i < 3; i++) {
81        int numSplits =
82          random.nextInt(MAX_LENGTH/(SequenceFile.SYNC_INTERVAL/20))+1;
83        //LOG.info("splitting: requesting = " + numSplits);
84        InputSplit[] splits = format.getSplits(job, numSplits);
85        //LOG.info("splitting: got =        " + splits.length);
86
87        // check each split
88        BitSet bits = new BitSet(length);
89        for (int j = 0; j < splits.length; j++) {
90          RecordReader<IntWritable, BytesWritable> reader =
91            format.getRecordReader(splits[j], job, reporter);
92          try {
93            int count = 0;
94            while (reader.next(key, value)) {
95              // if (bits.get(key.get())) {
96              // LOG.info("splits["+j+"]="+splits[j]+" : " + key.get());
97              // LOG.info("@"+reader.getPos());
98              // }
99              assertFalse("Key in multiple partitions.", bits.get(key.get()));
100              bits.set(key.get());
101              count++;
102            }
103            //LOG.info("splits["+j+"]="+splits[j]+" count=" + count);
104          } finally {
105            reader.close();
106          }
107        }
108        assertEquals("Some keys in no partition.", length, bits.cardinality());
109      }
110
111    }
112  }
113
114  public static void main(String[] args) throws Exception {
115    new TestSequenceFileInputFormat().testFormat();
116  }
117}
Note: See TracBrowser for help on using the repository browser.