1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34 public class HFileTestUtil {
35
36
37
38
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
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 }