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