1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.hfile;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.fs.FileSystem;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.SmallTests;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31
32
33
34
35
36
37
38
39
40
41
42 @Category(SmallTests.class)
43 public class TestHFileInlineToRootChunkConversion {
44 private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
45 private final Configuration conf = testUtil.getConfiguration();
46
47 @Test
48 public void testWriteHFile() throws Exception {
49 Path hfPath = new Path(testUtil.getDataTestDir(),
50 TestHFileInlineToRootChunkConversion.class.getSimpleName() + ".hfile");
51 int maxChunkSize = 1024;
52 FileSystem fs = FileSystem.get(conf);
53 CacheConfig cacheConf = new CacheConfig(conf);
54 conf.setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, maxChunkSize);
55 HFileContext context = new HFileContextBuilder().withBlockSize(16).build();
56 HFileWriterV2 hfw =
57 (HFileWriterV2) new HFileWriterV2.WriterFactoryV2(conf, cacheConf)
58 .withFileContext(context)
59 .withPath(fs, hfPath).create();
60 List<byte[]> keys = new ArrayList<byte[]>();
61 StringBuilder sb = new StringBuilder();
62
63 for (int i = 0; i < 4; ++i) {
64 sb.append("key" + String.format("%05d", i));
65 sb.append("_");
66 for (int j = 0; j < 100; ++j) {
67 sb.append('0' + j);
68 }
69 String keyStr = sb.toString();
70 sb.setLength(0);
71
72 byte[] k = Bytes.toBytes(keyStr);
73 System.out.println("Key: " + Bytes.toString(k));
74 keys.add(k);
75 byte[] v = Bytes.toBytes("value" + i);
76 hfw.append(k, v);
77 }
78 hfw.close();
79
80 HFileReaderV2 reader = (HFileReaderV2) HFile.createReader(fs, hfPath, cacheConf, conf);
81 HFileScanner scanner = reader.getScanner(true, true);
82 for (int i = 0; i < keys.size(); ++i) {
83 scanner.seekTo(keys.get(i));
84 }
85 reader.close();
86 }
87 }