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.regionserver.StoreFile;
28  
29  import java.io.IOException;
30  
31  /**
32   * Utility class for HFile-related testing.
33   */
34  public class HFileTestUtil {
35  
36    /**
37     * Create an HFile with the given number of rows between a given
38     * start key and end key.
39     */
40    public static void createHFile(
41        Configuration configuration,
42        FileSystem fs, Path path,
43        byte[] family, byte[] qualifier,
44        byte[] startKey, byte[] endKey, int numRows) throws IOException
45    {
46      HFile.Writer writer = HFile.getWriterFactory(configuration, new CacheConfig(configuration))
47          .withPath(fs, path)
48          .withComparator(KeyValue.KEY_COMPARATOR)
49          .create();
50      long now = System.currentTimeMillis();
51      try {
52        // subtract 2 since iterateOnSplits doesn't include boundary keys
53        for (byte[] key : Bytes.iterateOnSplits(startKey, endKey, numRows-2)) {
54          KeyValue kv = new KeyValue(key, family, qualifier, now, key);
55          writer.append(kv);
56        }
57      } finally {
58        writer.appendFileInfo(StoreFile.BULKLOAD_TIME_KEY,
59            Bytes.toBytes(System.currentTimeMillis()));
60        writer.close();
61      }
62    }
63  }