1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36
37
38
39
40 @Category(MediumTests.class)
41 public class TestHTableUtil {
42 final Log LOG = LogFactory.getLog(getClass());
43 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44 private static byte [] FAMILY = Bytes.toBytes("testFamily");
45 private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
46 private static byte [] VALUE = Bytes.toBytes("testValue");
47
48
49
50
51 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
53 TEST_UTIL.startMiniCluster();
54 }
55
56
57
58
59 @AfterClass
60 public static void tearDownAfterClass() throws Exception {
61 TEST_UTIL.shutdownMiniCluster();
62 }
63
64
65
66
67
68 @Test
69 public void testBucketPut() throws Exception {
70 byte [] TABLE = Bytes.toBytes("testBucketPut");
71 HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
72 ht.setAutoFlush(false, true);
73
74 List<Put> puts = new ArrayList<Put>();
75 puts.add( createPut("row1") );
76 puts.add( createPut("row2") );
77 puts.add( createPut("row3") );
78 puts.add( createPut("row4") );
79
80 HTableUtil.bucketRsPut( ht, puts );
81
82 Scan scan = new Scan();
83 scan.addColumn(FAMILY, QUALIFIER);
84 int count = 0;
85 for(Result result : ht.getScanner(scan)) {
86 count++;
87 }
88 LOG.info("bucket put count=" + count);
89 assertEquals(count, puts.size());
90 ht.close();
91 }
92
93 private Put createPut(String row) {
94 Put put = new Put( Bytes.toBytes(row));
95 put.add(FAMILY, QUALIFIER, VALUE);
96 return put;
97 }
98
99
100
101
102
103 @Test
104 public void testBucketBatch() throws Exception {
105 byte [] TABLE = Bytes.toBytes("testBucketBatch");
106 HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
107
108 List<Row> rows = new ArrayList<Row>();
109 rows.add( createPut("row1") );
110 rows.add( createPut("row2") );
111 rows.add( createPut("row3") );
112 rows.add( createPut("row4") );
113
114 HTableUtil.bucketRsBatch( ht, rows );
115
116 Scan scan = new Scan();
117 scan.addColumn(FAMILY, QUALIFIER);
118
119 int count = 0;
120 for(Result result : ht.getScanner(scan)) {
121 count++;
122 }
123 LOG.info("bucket batch count=" + count);
124 assertEquals(count, rows.size());
125 ht.close();
126 }
127
128
129 }
130