source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/file/tfile/TestTFileSplit.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: 3.5 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with this
4 * work for additional information regarding copyright ownership. The ASF
5 * licenses this file to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17package org.apache.hadoop.io.file.tfile;
18
19import java.io.IOException;
20
21import junit.framework.Assert;
22import junit.framework.TestCase;
23
24import org.apache.hadoop.conf.Configuration;
25import org.apache.hadoop.fs.FSDataOutputStream;
26import org.apache.hadoop.fs.FileSystem;
27import org.apache.hadoop.fs.Path;
28import org.apache.hadoop.io.BytesWritable;
29import org.apache.hadoop.io.file.tfile.TFile.Reader;
30import org.apache.hadoop.io.file.tfile.TFile.Writer;
31import org.apache.hadoop.io.file.tfile.TFile.Reader.Scanner;
32
33public class TestTFileSplit extends TestCase {
34  private static String ROOT =
35      System.getProperty("test.build.data", "/tmp/tfile-test");
36
37  private final static int BLOCK_SIZE = 64 * 1024;
38
39  private static final String KEY = "key";
40  private static final String VALUE = "value";
41
42  private FileSystem fs;
43  private Configuration conf;
44  private Path path;
45
46  private String comparator = "memcmp";
47  private String outputFile = "TestTFileSplit";
48
49  void createFile(int count, String compress) throws IOException {
50    conf = new Configuration();
51    path = new Path(ROOT, outputFile + "." + compress);
52    fs = path.getFileSystem(conf);
53    FSDataOutputStream out = fs.create(path);
54    Writer writer = new Writer(out, BLOCK_SIZE, compress, comparator, conf);
55
56    int nx;
57    for (nx = 0; nx < count; nx++) {
58      byte[] key = composeSortedKey(KEY, count, nx).getBytes();
59      byte[] value = (VALUE + nx).getBytes();
60      writer.append(key, value);
61    }
62    writer.close();
63    out.close();
64  }
65
66  void readFile() throws IOException {
67    long fileLength = fs.getFileStatus(path).getLen();
68    int numSplit = 10;
69    long splitSize = fileLength / numSplit + 1;
70
71    Reader reader =
72        new Reader(fs.open(path), fs.getFileStatus(path).getLen(), conf);
73    long offset = 0;
74    long rowCount = 0;
75    BytesWritable key, value;
76    for (int i = 0; i < numSplit; ++i, offset += splitSize) {
77      Scanner scanner = reader.createScanner(offset, splitSize);
78      int count = 0;
79      key = new BytesWritable();
80      value = new BytesWritable();
81      while (!scanner.atEnd()) {
82        scanner.entry().get(key, value);
83        ++count;
84        scanner.advance();
85      }
86      scanner.close();
87      Assert.assertTrue(count > 0);
88      rowCount += count;
89    }
90    Assert.assertEquals(rowCount, reader.getEntryCount());
91    reader.close();
92  }
93 
94  static String composeSortedKey(String prefix, int total, int value) {
95    return String.format("%s%010d", prefix, value);
96  }
97 
98  public void testSplit() throws IOException {
99    System.out.println("testSplit");
100    createFile(100000, Compression.Algorithm.NONE.getName());
101    readFile();
102    fs.delete(path, true);
103    createFile(500000, Compression.Algorithm.GZ.getName());
104    readFile();
105    fs.delete(path, true);
106  }
107}
Note: See TracBrowser for help on using the repository browser.