View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.fs.FileSystem;
23  import org.apache.hadoop.fs.Path;
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
26  import org.apache.hadoop.hbase.io.hfile.HFile;
27  import org.apache.hadoop.hbase.io.hfile.HFileContext;
28  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
29  import org.apache.hadoop.hbase.regionserver.StoreFile;
30  
31  import java.io.IOException;
32  
33  /**
34   * Utility class for HFile-related testing.
35   */
36  public class HFileTestUtil {
37  
38    /**
39     * Create an HFile with the given number of rows between a given
40     * start key and end key.
41     */
42    public static void createHFile(
43        Configuration configuration,
44        FileSystem fs, Path path,
45        byte[] family, byte[] qualifier,
46        byte[] startKey, byte[] endKey, int numRows) throws IOException
47    {
48      HFileContext meta = new HFileContextBuilder().build();
49      HFile.Writer writer = HFile.getWriterFactory(configuration, new CacheConfig(configuration))
50          .withPath(fs, path)
51          .withFileContext(meta)
52          .create();
53      long now = System.currentTimeMillis();
54      try {
55        // subtract 2 since iterateOnSplits doesn't include boundary keys
56        for (byte[] key : Bytes.iterateOnSplits(startKey, endKey, numRows-2)) {
57          KeyValue kv = new KeyValue(key, family, qualifier, now, key);
58          writer.append(kv);
59        }
60      } finally {
61        writer.appendFileInfo(StoreFile.BULKLOAD_TIME_KEY,
62            Bytes.toBytes(System.currentTimeMillis()));
63        writer.close();
64      }
65    }
66  }