2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.hamcrest
.CoreMatchers
.instanceOf
;
21 import static org
.hamcrest
.MatcherAssert
.assertThat
;
22 import static org
.junit
.Assert
.assertEquals
;
23 import static org
.junit
.Assert
.assertFalse
;
24 import static org
.junit
.Assert
.assertNotNull
;
25 import static org
.junit
.Assert
.assertTrue
;
26 import static org
.junit
.Assert
.fail
;
28 import java
.io
.IOException
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Arrays
;
31 import java
.util
.Collection
;
32 import java
.util
.EnumSet
;
33 import java
.util
.HashSet
;
34 import java
.util
.List
;
35 import java
.util
.NavigableMap
;
36 import org
.apache
.hadoop
.conf
.Configuration
;
37 import org
.apache
.hadoop
.hbase
.Cell
;
38 import org
.apache
.hadoop
.hbase
.CellUtil
;
39 import org
.apache
.hadoop
.hbase
.ClusterMetrics
.Option
;
40 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
41 import org
.apache
.hadoop
.hbase
.HConstants
;
42 import org
.apache
.hadoop
.hbase
.KeyValue
;
43 import org
.apache
.hadoop
.hbase
.SingleProcessHBaseCluster
;
44 import org
.apache
.hadoop
.hbase
.TableName
;
45 import org
.apache
.hadoop
.hbase
.TableNameTestRule
;
46 import org
.apache
.hadoop
.hbase
.coprocessor
.MultiRowMutationEndpoint
;
47 import org
.apache
.hadoop
.hbase
.regionserver
.NoSuchColumnFamilyException
;
48 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
49 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
50 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
51 import org
.junit
.AfterClass
;
52 import org
.junit
.Assume
;
53 import org
.junit
.ClassRule
;
54 import org
.junit
.Rule
;
55 import org
.junit
.Test
;
56 import org
.junit
.experimental
.categories
.Category
;
57 import org
.junit
.runner
.RunWith
;
58 import org
.junit
.runners
.Parameterized
;
59 import org
.slf4j
.Logger
;
60 import org
.slf4j
.LoggerFactory
;
63 * Run tests that use the HBase clients; {@link Table}.
64 * Sets up the HBase mini cluster once at start and runs through all client tests.
65 * Each creates a table named for the method and does its stuff against that.
67 * Parameterized to run with different registry implementations.
69 @Category({LargeTests
.class, ClientTests
.class})
70 @SuppressWarnings ("deprecation")
71 @RunWith(Parameterized
.class)
72 public class TestFromClientSide4
extends FromClientSideBase
{
73 private static final Logger LOG
= LoggerFactory
.getLogger(TestFromClientSide4
.class);
75 public static final HBaseClassTestRule CLASS_RULE
=
76 HBaseClassTestRule
.forClass(TestFromClientSide4
.class);
78 public TableNameTestRule name
= new TableNameTestRule();
80 // To keep the child classes happy.
81 TestFromClientSide4() {
84 public TestFromClientSide4(Class registry
, int numHedgedReqs
) throws Exception
{
85 initialize(registry
, numHedgedReqs
, MultiRowMutationEndpoint
.class);
88 @Parameterized.Parameters
89 public static Collection
parameters() {
90 return Arrays
.asList(new Object
[][] { { MasterRegistry
.class, 1 }, { MasterRegistry
.class, 2 },
91 { ZKConnectionRegistry
.class, 1 } });
95 public static void tearDownAfterClass() throws Exception
{
100 * Test batch operations with combination of valid and invalid args
102 @Test public void testBatchOperationsWithErrors() throws Exception
{
103 final TableName tableName
= name
.getTableName();
104 try (Table foo
= TEST_UTIL
.createTable(tableName
, new byte[][] { FAMILY
}, 10)) {
108 // 1.1 Put with no column families (local validation, runtime exception)
109 List
<Put
> puts
= new ArrayList
<>(NUM_OPS
);
110 for (int i
= 0; i
!= NUM_OPS
; i
++) {
111 Put put
= new Put(Bytes
.toBytes(i
));
118 } catch (IllegalArgumentException e
) {
120 assertEquals(NUM_OPS
, puts
.size());
123 // 1.2 Put with invalid column family
125 for (int i
= 0; i
< NUM_OPS
; i
++) {
126 Put put
= new Put(Bytes
.toBytes(i
));
127 put
.addColumn((i
% 2) == 0 ? FAMILY
: INVALID_FAMILY
, FAMILY
, Bytes
.toBytes(i
));
134 } catch (RetriesExhaustedException e
) {
136 assertThat(e
.getCause(), instanceOf(NoSuchColumnFamilyException
.class));
139 // 2.1 Get non-existent rows
140 List
<Get
> gets
= new ArrayList
<>(NUM_OPS
);
141 for (int i
= 0; i
< NUM_OPS
; i
++) {
142 Get get
= new Get(Bytes
.toBytes(i
));
145 Result
[] getsResult
= foo
.get(gets
);
146 assertNotNull(getsResult
);
147 assertEquals(NUM_OPS
, getsResult
.length
);
148 for (int i
= 0; i
< NUM_OPS
; i
++) {
149 Result getResult
= getsResult
[i
];
151 assertFalse(getResult
.isEmpty());
153 assertTrue(getResult
.isEmpty());
157 // 2.2 Get with invalid column family
159 for (int i
= 0; i
< NUM_OPS
; i
++) {
160 Get get
= new Get(Bytes
.toBytes(i
));
161 get
.addColumn((i
% 2) == 0 ? FAMILY
: INVALID_FAMILY
, FAMILY
);
167 } catch (RetriesExhaustedException e
) {
169 assertThat(e
.getCause(), instanceOf(NoSuchColumnFamilyException
.class));
172 // 3.1 Delete with invalid column family
173 List
<Delete
> deletes
= new ArrayList
<>(NUM_OPS
);
174 for (int i
= 0; i
< NUM_OPS
; i
++) {
175 Delete delete
= new Delete(Bytes
.toBytes(i
));
176 delete
.addColumn((i
% 2) == 0 ? FAMILY
: INVALID_FAMILY
, FAMILY
);
182 } catch (RetriesExhaustedException e
) {
184 assertThat(e
.getCause(), instanceOf(NoSuchColumnFamilyException
.class));
187 // all valid rows should have been deleted
189 for (int i
= 0; i
< NUM_OPS
; i
++) {
190 Get get
= new Get(Bytes
.toBytes(i
));
193 getsResult
= foo
.get(gets
);
194 assertNotNull(getsResult
);
195 assertEquals(NUM_OPS
, getsResult
.length
);
196 for (Result getResult
: getsResult
) {
197 assertTrue(getResult
.isEmpty());
200 // 3.2 Delete non-existent rows
202 for (int i
= 0; i
< NUM_OPS
; i
++) {
203 Delete delete
= new Delete(Bytes
.toBytes(i
));
216 * If millions of columns in a column family, hbase scanner won't come up
217 * Test will create numRows rows, each with numColsPerRow columns
218 * (1 version each), and attempt to scan them all.
219 * To test at scale, up numColsPerRow to the millions
220 * (have not gotten that to work running as junit though)
222 @Test public void testJiraTest867() throws Exception
{
224 int numColsPerRow
= 2000;
226 final TableName tableName
= name
.getTableName();
228 byte[][] ROWS
= makeN(ROW
, numRows
);
229 byte[][] QUALIFIERS
= makeN(QUALIFIER
, numColsPerRow
);
231 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
)) {
235 for (int i
= 0; i
< numRows
; i
++) {
236 Put put
= new Put(ROWS
[i
]);
237 put
.setDurability(Durability
.SKIP_WAL
);
238 for (int j
= 0; j
< numColsPerRow
; j
++) {
239 put
.addColumn(FAMILY
, QUALIFIERS
[j
], QUALIFIERS
[j
]);
242 "Put expected to contain " + numColsPerRow
+ " columns but " + "only contains " + put
243 .size(), put
.size(), numColsPerRow
);
248 Get get
= new Get(ROWS
[numRows
- 1]);
249 Result result
= ht
.get(get
);
250 assertNumKeys(result
, numColsPerRow
);
251 Cell
[] keys
= result
.rawCells();
252 for (int i
= 0; i
< result
.size(); i
++) {
253 assertKey(keys
[i
], ROWS
[numRows
- 1], FAMILY
, QUALIFIERS
[i
], QUALIFIERS
[i
]);
257 Scan scan
= new Scan();
258 try (ResultScanner scanner
= ht
.getScanner(scan
)) {
260 while ((result
= scanner
.next()) != null) {
261 assertNumKeys(result
, numColsPerRow
);
262 Cell
[] kvs
= result
.rawCells();
263 for (int i
= 0; i
< numColsPerRow
; i
++) {
264 assertKey(kvs
[i
], ROWS
[rowCount
], FAMILY
, QUALIFIERS
[i
], QUALIFIERS
[i
]);
269 "Expected to scan " + numRows
+ " rows but actually scanned " + rowCount
+ " rows",
273 // flush and try again
278 get
= new Get(ROWS
[numRows
- 1]);
279 result
= ht
.get(get
);
280 assertNumKeys(result
, numColsPerRow
);
281 keys
= result
.rawCells();
282 for (int i
= 0; i
< result
.size(); i
++) {
283 assertKey(keys
[i
], ROWS
[numRows
- 1], FAMILY
, QUALIFIERS
[i
], QUALIFIERS
[i
]);
288 try (ResultScanner scanner
= ht
.getScanner(scan
)) {
290 while ((result
= scanner
.next()) != null) {
291 assertNumKeys(result
, numColsPerRow
);
292 Cell
[] kvs
= result
.rawCells();
293 for (int i
= 0; i
< numColsPerRow
; i
++) {
294 assertKey(kvs
[i
], ROWS
[rowCount
], FAMILY
, QUALIFIERS
[i
], QUALIFIERS
[i
]);
299 "Expected to scan " + numRows
+ " rows but actually scanned " + rowCount
+ " rows",
307 * get with timestamp will return a value if there is a version with an
310 @Test public void testJiraTest861() throws Exception
{
311 final TableName tableName
= name
.getTableName();
312 byte[][] VALUES
= makeNAscii(VALUE
, 7);
313 long[] STAMPS
= makeStamps(7);
315 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
317 // Insert three versions
319 Put put
= new Put(ROW
);
320 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
321 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
322 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
325 // Get the middle value
326 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
328 // Try to get one version before (expect fail)
329 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1]);
331 // Try to get one version after (expect fail)
332 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5]);
334 // Try same from storefile
336 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
337 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1]);
338 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5]);
340 // Insert two more versions surrounding others, into memstore
342 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
343 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[6], VALUES
[6]);
346 // Check we can get everything we should and can't get what we shouldn't
347 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
348 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1]);
349 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
350 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
351 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
352 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5]);
353 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6], VALUES
[6]);
355 // Try same from two storefiles
357 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
358 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1]);
359 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
360 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
361 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
362 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5]);
363 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6], VALUES
[6]);
369 * Add a HTable get/obtainScanner method that retrieves all versions of a
370 * particular column and row between two timestamps
372 @Test public void testJiraTest33() throws Exception
{
373 final TableName tableName
= name
.getTableName();
374 byte[][] VALUES
= makeNAscii(VALUE
, 7);
375 long[] STAMPS
= makeStamps(7);
377 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
379 // Insert lots versions
381 Put put
= new Put(ROW
);
382 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
383 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
384 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
385 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
386 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
387 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
390 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
391 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 2);
392 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
393 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 3);
395 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
396 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 2);
397 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
398 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 3);
400 // Try same from storefile
403 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
404 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 2);
405 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
406 getVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 3);
408 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
409 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 2);
410 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
411 scanVersionRangeAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 3);
417 * commit(BatchUpdate) method should return timestamp
419 @Test public void testJiraTest1014() throws Exception
{
420 final TableName tableName
= name
.getTableName();
422 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
424 long manualStamp
= 12345;
426 // Insert lots versions
428 Put put
= new Put(ROW
);
429 put
.addColumn(FAMILY
, QUALIFIER
, manualStamp
, VALUE
);
432 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, manualStamp
, VALUE
);
433 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, manualStamp
- 1);
434 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, manualStamp
+ 1);
440 * Scan for columns > some timestamp
442 @Test public void testJiraTest1182() throws Exception
{
443 final TableName tableName
= name
.getTableName();
444 byte[][] VALUES
= makeNAscii(VALUE
, 7);
445 long[] STAMPS
= makeStamps(7);
447 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
449 // Insert lots versions
451 Put put
= new Put(ROW
);
452 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
453 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
454 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
455 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
456 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
457 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
460 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
461 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 5);
462 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
464 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
465 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 5);
466 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
468 // Try same from storefile
471 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
472 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 5);
473 getVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
475 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
476 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 2, 5);
477 scanVersionRangeAndVerifyGreaterThan(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 4, 5);
483 * Add a means of scanning over all versions
485 @Test public void testJiraTest52() throws Exception
{
486 final TableName tableName
= name
.getTableName();
487 byte[][] VALUES
= makeNAscii(VALUE
, 7);
488 long[] STAMPS
= makeStamps(7);
490 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
492 // Insert lots versions
494 Put put
= new Put(ROW
);
495 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[0], VALUES
[0]);
496 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
497 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
498 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
499 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
500 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
503 getAllVersionsAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
505 scanAllVersionsAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
507 // Try same from storefile
510 getAllVersionsAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
512 scanAllVersionsAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
, VALUES
, 0, 5);
517 @SuppressWarnings("checkstyle:MethodLength")
518 public void testDuplicateVersions() throws Exception
{
519 final TableName tableName
= name
.getTableName();
521 long[] STAMPS
= makeStamps(20);
522 byte[][] VALUES
= makeNAscii(VALUE
, 20);
524 try (Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
526 // Insert 4 versions of same column
527 Put put
= new Put(ROW
);
528 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
529 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
530 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
531 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
534 // Verify we can get each one properly
535 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
536 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
537 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
538 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
539 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
540 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
541 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
542 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
544 // Verify we don't accidentally get others
545 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
546 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3]);
547 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6]);
548 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
549 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3]);
550 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6]);
552 // Ensure maxVersions in query is respected
553 Get get
= new Get(ROW
);
554 get
.addColumn(FAMILY
, QUALIFIER
);
556 Result result
= ht
.get(get
);
557 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
, new long[] { STAMPS
[4], STAMPS
[5] },
558 new byte[][] { VALUES
[4], VALUES
[5] }, 0, 1);
560 Scan scan
= new Scan().withStartRow(ROW
);
561 scan
.addColumn(FAMILY
, QUALIFIER
);
562 scan
.readVersions(2);
563 result
= getSingleScanResult(ht
, scan
);
564 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
, new long[] { STAMPS
[4], STAMPS
[5] },
565 new byte[][] { VALUES
[4], VALUES
[5] }, 0, 1);
571 // Verify we can get each one properly
572 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
573 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
574 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
575 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
576 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
577 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
578 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[4]);
579 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[5], VALUES
[5]);
581 // Verify we don't accidentally get others
582 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
583 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3]);
584 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6]);
585 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
586 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[3]);
587 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[6]);
589 // Ensure maxVersions in query is respected
591 get
.addColumn(FAMILY
, QUALIFIER
);
593 result
= ht
.get(get
);
594 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
, new long[] { STAMPS
[4], STAMPS
[5] },
595 new byte[][] { VALUES
[4], VALUES
[5] }, 0, 1);
597 scan
= new Scan().withStartRow(ROW
);
598 scan
.addColumn(FAMILY
, QUALIFIER
);
599 scan
.readVersions(2);
600 result
= getSingleScanResult(ht
, scan
);
601 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
, new long[] { STAMPS
[4], STAMPS
[5] },
602 new byte[][] { VALUES
[4], VALUES
[5] }, 0, 1);
604 // Add some memstore and retest
606 // Insert 4 more versions of same column and a dupe
608 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[3], VALUES
[3]);
609 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[14]);
610 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[6], VALUES
[6]);
611 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[7], VALUES
[7]);
612 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[8], VALUES
[8]);
615 // Ensure maxVersions in query is respected
617 get
.addColumn(FAMILY
, QUALIFIER
);
619 result
= ht
.get(get
);
620 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
621 new long[] { STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8] },
622 new byte[][] { VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7],
625 scan
= new Scan().withStartRow(ROW
);
626 scan
.addColumn(FAMILY
, QUALIFIER
);
627 scan
.readVersions(7);
628 result
= getSingleScanResult(ht
, scan
);
629 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
630 new long[] { STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8] },
631 new byte[][] { VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7],
636 result
= ht
.get(get
);
637 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
638 new long[] { STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8] },
639 new byte[][] { VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7],
642 scan
= new Scan().withStartRow(ROW
);
643 scan
.readVersions(7);
644 result
= getSingleScanResult(ht
, scan
);
645 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
646 new long[] { STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8] },
647 new byte[][] { VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7],
650 // Verify we can get each one properly
651 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
652 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
653 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[14]);
654 getVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[7], VALUES
[7]);
655 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[1], VALUES
[1]);
656 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[2], VALUES
[2]);
657 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[4], VALUES
[14]);
658 scanVersionAndVerify(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[7], VALUES
[7]);
660 // Verify we don't accidentally get others
661 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
662 getVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[9]);
663 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[0]);
664 scanVersionAndVerifyMissing(ht
, ROW
, FAMILY
, QUALIFIER
, STAMPS
[9]);
666 // Ensure maxVersions of table is respected
670 // Insert 4 more versions of same column and a dupe
672 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[9], VALUES
[9]);
673 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[11], VALUES
[11]);
674 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[13], VALUES
[13]);
675 put
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[15], VALUES
[15]);
679 get
.addColumn(FAMILY
, QUALIFIER
);
680 get
.readVersions(Integer
.MAX_VALUE
);
681 result
= ht
.get(get
);
682 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
683 new long[] { STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8], STAMPS
[9],
684 STAMPS
[11], STAMPS
[13], STAMPS
[15] },
685 new byte[][] { VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7], VALUES
[8], VALUES
[9],
686 VALUES
[11], VALUES
[13], VALUES
[15] }, 0, 9);
688 scan
= new Scan().withStartRow(ROW
);
689 scan
.addColumn(FAMILY
, QUALIFIER
);
690 scan
.readVersions(Integer
.MAX_VALUE
);
691 result
= getSingleScanResult(ht
, scan
);
692 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
693 new long[] { STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[7], STAMPS
[8], STAMPS
[9],
694 STAMPS
[11], STAMPS
[13], STAMPS
[15] },
695 new byte[][] { VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[7], VALUES
[8], VALUES
[9],
696 VALUES
[11], VALUES
[13], VALUES
[15] }, 0, 9);
698 // Delete a version in the memstore and a version in a storefile
699 Delete delete
= new Delete(ROW
);
700 delete
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[11]);
701 delete
.addColumn(FAMILY
, QUALIFIER
, STAMPS
[7]);
704 // Test that it's gone
706 get
.addColumn(FAMILY
, QUALIFIER
);
707 get
.readVersions(Integer
.MAX_VALUE
);
708 result
= ht
.get(get
);
709 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
710 new long[] { STAMPS
[1], STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[8],
711 STAMPS
[9], STAMPS
[13], STAMPS
[15] },
712 new byte[][] { VALUES
[1], VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[8],
713 VALUES
[9], VALUES
[13], VALUES
[15] }, 0, 9);
715 scan
= new Scan().withStartRow(ROW
);
716 scan
.addColumn(FAMILY
, QUALIFIER
);
717 scan
.readVersions(Integer
.MAX_VALUE
);
718 result
= getSingleScanResult(ht
, scan
);
719 assertNResult(result
, ROW
, FAMILY
, QUALIFIER
,
720 new long[] { STAMPS
[1], STAMPS
[2], STAMPS
[3], STAMPS
[4], STAMPS
[5], STAMPS
[6], STAMPS
[8],
721 STAMPS
[9], STAMPS
[13], STAMPS
[15] },
722 new byte[][] { VALUES
[1], VALUES
[2], VALUES
[3], VALUES
[14], VALUES
[5], VALUES
[6], VALUES
[8],
723 VALUES
[9], VALUES
[13], VALUES
[15] }, 0, 9);
727 @Test public void testUpdates() throws Exception
{
728 final TableName tableName
= name
.getTableName();
729 try (Table hTable
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10)) {
731 // Write a column with values at timestamp 1, 2 and 3
732 byte[] row
= Bytes
.toBytes("row1");
733 byte[] qualifier
= Bytes
.toBytes("myCol");
734 Put put
= new Put(row
);
735 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("AAA"));
739 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("BBB"));
743 put
.addColumn(FAMILY
, qualifier
, 3L, Bytes
.toBytes("EEE"));
746 Get get
= new Get(row
);
747 get
.addColumn(FAMILY
, qualifier
);
748 get
.readAllVersions();
750 // Check that the column indeed has the right values at timestamps 1 and
752 Result result
= hTable
.get(get
);
753 NavigableMap
<Long
, byte[]> navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
754 assertEquals("AAA", Bytes
.toString(navigableMap
.get(1L)));
755 assertEquals("BBB", Bytes
.toString(navigableMap
.get(2L)));
757 // Update the value at timestamp 1
759 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("CCC"));
762 // Update the value at timestamp 2
764 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("DDD"));
767 // Check that the values at timestamp 2 and 1 got updated
768 result
= hTable
.get(get
);
769 navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
770 assertEquals("CCC", Bytes
.toString(navigableMap
.get(1L)));
771 assertEquals("DDD", Bytes
.toString(navigableMap
.get(2L)));
775 @Test public void testUpdatesWithMajorCompaction() throws Exception
{
776 final TableName tableName
= name
.getTableName();
777 try (Table hTable
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10);
778 Admin admin
= TEST_UTIL
.getAdmin()) {
780 // Write a column with values at timestamp 1, 2 and 3
781 byte[] row
= Bytes
.toBytes("row2");
782 byte[] qualifier
= Bytes
.toBytes("myCol");
783 Put put
= new Put(row
);
784 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("AAA"));
788 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("BBB"));
792 put
.addColumn(FAMILY
, qualifier
, 3L, Bytes
.toBytes("EEE"));
795 Get get
= new Get(row
);
796 get
.addColumn(FAMILY
, qualifier
);
797 get
.readAllVersions();
799 // Check that the column indeed has the right values at timestamps 1 and
801 Result result
= hTable
.get(get
);
802 NavigableMap
<Long
, byte[]> navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
803 assertEquals("AAA", Bytes
.toString(navigableMap
.get(1L)));
804 assertEquals("BBB", Bytes
.toString(navigableMap
.get(2L)));
806 // Trigger a major compaction
807 admin
.flush(tableName
);
808 admin
.majorCompact(tableName
);
811 // Update the value at timestamp 1
813 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("CCC"));
816 // Update the value at timestamp 2
818 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("DDD"));
821 // Trigger a major compaction
822 admin
.flush(tableName
);
823 admin
.majorCompact(tableName
);
826 // Check that the values at timestamp 2 and 1 got updated
827 result
= hTable
.get(get
);
828 navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
829 assertEquals("CCC", Bytes
.toString(navigableMap
.get(1L)));
830 assertEquals("DDD", Bytes
.toString(navigableMap
.get(2L)));
834 @Test public void testMajorCompactionBetweenTwoUpdates() throws Exception
{
835 final TableName tableName
= name
.getTableName();
836 try (Table hTable
= TEST_UTIL
.createTable(tableName
, FAMILY
, 10);
837 Admin admin
= TEST_UTIL
.getAdmin()) {
839 // Write a column with values at timestamp 1, 2 and 3
840 byte[] row
= Bytes
.toBytes("row3");
841 byte[] qualifier
= Bytes
.toBytes("myCol");
842 Put put
= new Put(row
);
843 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("AAA"));
847 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("BBB"));
851 put
.addColumn(FAMILY
, qualifier
, 3L, Bytes
.toBytes("EEE"));
854 Get get
= new Get(row
);
855 get
.addColumn(FAMILY
, qualifier
);
856 get
.readAllVersions();
858 // Check that the column indeed has the right values at timestamps 1 and
860 Result result
= hTable
.get(get
);
861 NavigableMap
<Long
, byte[]> navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
862 assertEquals("AAA", Bytes
.toString(navigableMap
.get(1L)));
863 assertEquals("BBB", Bytes
.toString(navigableMap
.get(2L)));
865 // Trigger a major compaction
866 admin
.flush(tableName
);
867 admin
.majorCompact(tableName
);
870 // Update the value at timestamp 1
872 put
.addColumn(FAMILY
, qualifier
, 1L, Bytes
.toBytes("CCC"));
875 // Trigger a major compaction
876 admin
.flush(tableName
);
877 admin
.majorCompact(tableName
);
880 // Update the value at timestamp 2
882 put
.addColumn(FAMILY
, qualifier
, 2L, Bytes
.toBytes("DDD"));
885 // Trigger a major compaction
886 admin
.flush(tableName
);
887 admin
.majorCompact(tableName
);
890 // Check that the values at timestamp 2 and 1 got updated
891 result
= hTable
.get(get
);
892 navigableMap
= result
.getMap().get(FAMILY
).get(qualifier
);
894 assertEquals("CCC", Bytes
.toString(navigableMap
.get(1L)));
895 assertEquals("DDD", Bytes
.toString(navigableMap
.get(2L)));
899 @Test public void testGet_EmptyTable() throws IOException
{
900 try (Table table
= TEST_UTIL
.createTable(name
.getTableName(), FAMILY
)) {
901 Get get
= new Get(ROW
);
902 get
.addFamily(FAMILY
);
903 Result r
= table
.get(get
);
904 assertTrue(r
.isEmpty());
908 @Test public void testGet_NullQualifier() throws IOException
{
909 try (Table table
= TEST_UTIL
.createTable(name
.getTableName(), FAMILY
)) {
910 Put put
= new Put(ROW
);
911 put
.addColumn(FAMILY
, QUALIFIER
, VALUE
);
915 put
.addColumn(FAMILY
, null, VALUE
);
919 Get get
= new Get(ROW
);
920 get
.addColumn(FAMILY
, null);
921 Result r
= table
.get(get
);
922 assertEquals(1, r
.size());
925 get
.addFamily(FAMILY
);
927 assertEquals(2, r
.size());
931 @Test public void testGet_NonExistentRow() throws IOException
{
932 try (Table table
= TEST_UTIL
.createTable(name
.getTableName(), FAMILY
)) {
933 Put put
= new Put(ROW
);
934 put
.addColumn(FAMILY
, QUALIFIER
, VALUE
);
938 Get get
= new Get(ROW
);
939 get
.addFamily(FAMILY
);
940 Result r
= table
.get(get
);
941 assertFalse(r
.isEmpty());
942 System
.out
.println("Row retrieved successfully");
944 byte[] missingrow
= Bytes
.toBytes("missingrow");
945 get
= new Get(missingrow
);
946 get
.addFamily(FAMILY
);
948 assertTrue(r
.isEmpty());
949 LOG
.info("Row missing as it should be");
953 @Test public void testPut() throws IOException
{
954 final byte[] CONTENTS_FAMILY
= Bytes
.toBytes("contents");
955 final byte[] SMALL_FAMILY
= Bytes
.toBytes("smallfam");
956 final byte[] row1
= Bytes
.toBytes("row1");
957 final byte[] row2
= Bytes
.toBytes("row2");
958 final byte[] value
= Bytes
.toBytes("abcd");
959 try (Table table
= TEST_UTIL
960 .createTable(name
.getTableName(), new byte[][] { CONTENTS_FAMILY
, SMALL_FAMILY
})) {
961 Put put
= new Put(row1
);
962 put
.addColumn(CONTENTS_FAMILY
, null, value
);
966 put
.addColumn(CONTENTS_FAMILY
, null, value
);
968 assertEquals(1, put
.size());
969 assertEquals(1, put
.getFamilyCellMap().get(CONTENTS_FAMILY
).size());
971 // KeyValue v1 expectation. Cast for now until we go all Cell all the time. TODO
972 KeyValue kv
= (KeyValue
) put
.getFamilyCellMap().get(CONTENTS_FAMILY
).get(0);
974 assertTrue(Bytes
.equals(CellUtil
.cloneFamily(kv
), CONTENTS_FAMILY
));
975 // will it return null or an empty byte array?
976 assertTrue(Bytes
.equals(CellUtil
.cloneQualifier(kv
), new byte[0]));
978 assertTrue(Bytes
.equals(CellUtil
.cloneValue(kv
), value
));
982 Scan scan
= new Scan();
983 scan
.addColumn(CONTENTS_FAMILY
, null);
984 try (ResultScanner scanner
= table
.getScanner(scan
)) {
985 for (Result r
: scanner
) {
986 for (Cell key
: r
.rawCells()) {
987 System
.out
.println(Bytes
.toString(r
.getRow()) + ": " + key
.toString());
994 @Test public void testPutNoCF() throws IOException
{
995 final byte[] BAD_FAM
= Bytes
.toBytes("BAD_CF");
996 final byte[] VAL
= Bytes
.toBytes(100);
997 try (Table table
= TEST_UTIL
.createTable(name
.getTableName(), FAMILY
)) {
998 boolean caughtNSCFE
= false;
1001 Put p
= new Put(ROW
);
1002 p
.addColumn(BAD_FAM
, QUALIFIER
, VAL
);
1004 } catch (Exception e
) {
1005 caughtNSCFE
= e
instanceof NoSuchColumnFamilyException
;
1007 assertTrue("Should throw NoSuchColumnFamilyException", caughtNSCFE
);
1011 @Test public void testRowsPut() throws IOException
{
1012 final byte[] CONTENTS_FAMILY
= Bytes
.toBytes("contents");
1013 final byte[] SMALL_FAMILY
= Bytes
.toBytes("smallfam");
1014 final int NB_BATCH_ROWS
= 10;
1015 final byte[] value
= Bytes
.toBytes("abcd");
1016 try (Table table
= TEST_UTIL
1017 .createTable(name
.getTableName(), new byte[][] { CONTENTS_FAMILY
, SMALL_FAMILY
})) {
1018 ArrayList
<Put
> rowsUpdate
= new ArrayList
<>();
1019 for (int i
= 0; i
< NB_BATCH_ROWS
; i
++) {
1020 byte[] row
= Bytes
.toBytes("row" + i
);
1021 Put put
= new Put(row
);
1022 put
.setDurability(Durability
.SKIP_WAL
);
1023 put
.addColumn(CONTENTS_FAMILY
, null, value
);
1024 rowsUpdate
.add(put
);
1026 table
.put(rowsUpdate
);
1027 Scan scan
= new Scan();
1028 scan
.addFamily(CONTENTS_FAMILY
);
1029 try (ResultScanner scanner
= table
.getScanner(scan
)) {
1031 for (@SuppressWarnings("unused") Result row
: scanner
) {
1034 assertEquals(NB_BATCH_ROWS
, nbRows
);
1039 @Test public void testRowsPutBufferedManyManyFlushes() throws IOException
{
1040 final byte[] CONTENTS_FAMILY
= Bytes
.toBytes("contents");
1041 final byte[] SMALL_FAMILY
= Bytes
.toBytes("smallfam");
1042 final byte[] value
= Bytes
.toBytes("abcd");
1043 final int NB_BATCH_ROWS
= 10;
1044 try (Table table
= TEST_UTIL
1045 .createTable(name
.getTableName(), new byte[][] { CONTENTS_FAMILY
, SMALL_FAMILY
})) {
1046 ArrayList
<Put
> rowsUpdate
= new ArrayList
<>();
1047 for (int i
= 0; i
< NB_BATCH_ROWS
* 10; i
++) {
1048 byte[] row
= Bytes
.toBytes("row" + i
);
1049 Put put
= new Put(row
);
1050 put
.setDurability(Durability
.SKIP_WAL
);
1051 put
.addColumn(CONTENTS_FAMILY
, null, value
);
1052 rowsUpdate
.add(put
);
1054 table
.put(rowsUpdate
);
1056 Scan scan
= new Scan();
1057 scan
.addFamily(CONTENTS_FAMILY
);
1058 try (ResultScanner scanner
= table
.getScanner(scan
)) {
1060 for (@SuppressWarnings("unused") Result row
: scanner
) {
1063 assertEquals(NB_BATCH_ROWS
* 10, nbRows
);
1068 @Test public void testAddKeyValue() {
1069 final byte[] CONTENTS_FAMILY
= Bytes
.toBytes("contents");
1070 final byte[] value
= Bytes
.toBytes("abcd");
1071 final byte[] row1
= Bytes
.toBytes("row1");
1072 final byte[] row2
= Bytes
.toBytes("row2");
1073 byte[] qualifier
= Bytes
.toBytes("qf1");
1074 Put put
= new Put(row1
);
1076 // Adding KeyValue with the same row
1077 KeyValue kv
= new KeyValue(row1
, CONTENTS_FAMILY
, qualifier
, value
);
1081 } catch (IOException e
) {
1086 // Adding KeyValue with the different row
1087 kv
= new KeyValue(row2
, CONTENTS_FAMILY
, qualifier
, value
);
1091 } catch (IOException e
) {
1098 * test for HBASE-737
1100 @Test public void testHBase737() throws IOException
{
1101 final byte[] FAM1
= Bytes
.toBytes("fam1");
1102 final byte[] FAM2
= Bytes
.toBytes("fam2");
1104 try (Table table
= TEST_UTIL
.createTable(name
.getTableName(), new byte[][] { FAM1
, FAM2
})) {
1105 // Insert some values
1106 Put put
= new Put(ROW
);
1107 put
.addColumn(FAM1
, Bytes
.toBytes("letters"), Bytes
.toBytes("abcdefg"));
1111 } catch (InterruptedException i
) {
1116 put
.addColumn(FAM1
, Bytes
.toBytes("numbers"), Bytes
.toBytes("123456"));
1121 } catch (InterruptedException i
) {
1126 put
.addColumn(FAM2
, Bytes
.toBytes("letters"), Bytes
.toBytes("hijklmnop"));
1129 long[] times
= new long[3];
1131 // First scan the memstore
1133 Scan scan
= new Scan();
1134 scan
.addFamily(FAM1
);
1135 scan
.addFamily(FAM2
);
1136 try (ResultScanner s
= table
.getScanner(scan
)) {
1139 while ((r
= s
.next()) != null) {
1140 for (Cell key
: r
.rawCells()) {
1141 times
[index
++] = key
.getTimestamp();
1145 for (int i
= 0; i
< times
.length
- 1; i
++) {
1146 for (int j
= i
+ 1; j
< times
.length
; j
++) {
1147 assertTrue(times
[j
] > times
[i
]);
1151 // Flush data to disk and try again
1155 Arrays
.fill(times
, 0);
1159 } catch (InterruptedException i
) {
1163 scan
.addFamily(FAM1
);
1164 scan
.addFamily(FAM2
);
1165 try (ResultScanner s
= table
.getScanner(scan
)) {
1168 while ((r
= s
.next()) != null) {
1169 for (Cell key
: r
.rawCells()) {
1170 times
[index
++] = key
.getTimestamp();
1173 for (int i
= 0; i
< times
.length
- 1; i
++) {
1174 for (int j
= i
+ 1; j
< times
.length
; j
++) {
1175 assertTrue(times
[j
] > times
[i
]);
1182 @Test public void testListTables() throws IOException
{
1183 final String testTableName
= name
.getTableName().toString();
1184 final TableName tableName1
= TableName
.valueOf(testTableName
+ "1");
1185 final TableName tableName2
= TableName
.valueOf(testTableName
+ "2");
1186 final TableName tableName3
= TableName
.valueOf(testTableName
+ "3");
1187 TableName
[] tables
= new TableName
[] { tableName1
, tableName2
, tableName3
};
1188 for (TableName table
: tables
) {
1189 TEST_UTIL
.createTable(table
, FAMILY
);
1191 try (Admin admin
= TEST_UTIL
.getAdmin()) {
1192 List
<TableDescriptor
> ts
= admin
.listTableDescriptors();
1193 HashSet
<TableDescriptor
> result
= new HashSet
<>(ts
);
1194 int size
= result
.size();
1195 assertTrue(size
>= tables
.length
);
1196 for (TableName table
: tables
) {
1197 boolean found
= false;
1198 for (TableDescriptor t
: ts
) {
1199 if (t
.getTableName().equals(table
)) {
1204 assertTrue("Not found: " + table
, found
);
1210 * simple test that just executes parts of the client
1211 * API that accept a pre-created Connection instance
1213 @Test public void testUnmanagedHConnection() throws IOException
{
1214 final TableName tableName
= name
.getTableName();
1215 TEST_UTIL
.createTable(tableName
, HConstants
.CATALOG_FAMILY
);
1216 try (Connection conn
= ConnectionFactory
.createConnection(TEST_UTIL
.getConfiguration());
1217 Table t
= conn
.getTable(tableName
);
1218 Admin admin
= conn
.getAdmin()) {
1219 assertTrue(admin
.tableExists(tableName
));
1220 assertTrue(t
.get(new Get(ROW
)).isEmpty());
1225 * test of that unmanaged HConnections are able to reconnect
1226 * properly (see HBASE-5058)
1228 @Test public void testUnmanagedHConnectionReconnect() throws Exception
{
1229 Configuration conf
= TEST_UTIL
.getConfiguration();
1230 Class registryImpl
= conf
1231 .getClass(HConstants
.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY
, ZKConnectionRegistry
.class);
1232 // This test does not make sense for MasterRegistry since it stops the only master in the
1233 // cluster and starts a new master without populating the underlying config for the connection.
1234 Assume
.assumeFalse(registryImpl
.equals(MasterRegistry
.class));
1235 final TableName tableName
= name
.getTableName();
1236 TEST_UTIL
.createTable(tableName
, HConstants
.CATALOG_FAMILY
);
1237 try (Connection conn
= ConnectionFactory
.createConnection(TEST_UTIL
.getConfiguration())) {
1238 try (Table t
= conn
.getTable(tableName
); Admin admin
= conn
.getAdmin()) {
1239 assertTrue(admin
.tableExists(tableName
));
1240 assertTrue(t
.get(new Get(ROW
)).isEmpty());
1244 SingleProcessHBaseCluster cluster
= TEST_UTIL
.getHBaseCluster();
1245 cluster
.stopMaster(0, false);
1246 cluster
.waitOnMaster(0);
1248 // start up a new master
1249 cluster
.startMaster();
1250 assertTrue(cluster
.waitForActiveAndReadyMaster());
1252 // test that the same unmanaged connection works with a new
1253 // Admin and can connect to the new master;
1254 try (Admin admin
= conn
.getAdmin()) {
1255 assertTrue(admin
.tableExists(tableName
));
1257 admin
.getClusterMetrics(EnumSet
.of(Option
.LIVE_SERVERS
)).getLiveServerMetrics().size(),
1263 @Test public void testMiscHTableStuff() throws IOException
{
1264 final String testTableName
= name
.getTableName().toString();
1265 final TableName tableAname
= TableName
.valueOf(testTableName
+ "A");
1266 final TableName tableBname
= TableName
.valueOf(testTableName
+ "B");
1267 final byte[] attrName
= Bytes
.toBytes("TESTATTR");
1268 final byte[] attrValue
= Bytes
.toBytes("somevalue");
1269 byte[] value
= Bytes
.toBytes("value");
1271 try (Table a
= TEST_UTIL
.createTable(tableAname
, HConstants
.CATALOG_FAMILY
);
1272 Table b
= TEST_UTIL
.createTable(tableBname
, HConstants
.CATALOG_FAMILY
)) {
1273 Put put
= new Put(ROW
);
1274 put
.addColumn(HConstants
.CATALOG_FAMILY
, null, value
);
1277 // open a new connection to A and a connection to b
1278 try (Table newA
= TEST_UTIL
.getConnection().getTable(tableAname
)) {
1280 // copy data from A to B
1281 Scan scan
= new Scan();
1282 scan
.addFamily(HConstants
.CATALOG_FAMILY
);
1283 try (ResultScanner s
= newA
.getScanner(scan
)) {
1284 for (Result r
: s
) {
1285 put
= new Put(r
.getRow());
1286 put
.setDurability(Durability
.SKIP_WAL
);
1287 for (Cell kv
: r
.rawCells()) {
1295 // Opening a new connection to A will cause the tables to be reloaded
1296 try (Table anotherA
= TEST_UTIL
.getConnection().getTable(tableAname
)) {
1297 Get get
= new Get(ROW
);
1298 get
.addFamily(HConstants
.CATALOG_FAMILY
);
1302 // We can still access A through newA because it has the table information
1303 // cached. And if it needs to recalibrate, that will cause the information
1306 // Test user metadata
1307 Admin admin
= TEST_UTIL
.getAdmin();
1308 // make a modifiable descriptor
1309 TableDescriptor desc
= a
.getDescriptor();
1310 // offline the table
1311 admin
.disableTable(tableAname
);
1312 // add a user attribute to HTD
1313 TableDescriptorBuilder builder
=
1314 TableDescriptorBuilder
.newBuilder(desc
).setValue(attrName
, attrValue
);
1315 // add a user attribute to HCD
1316 for (ColumnFamilyDescriptor c
: desc
.getColumnFamilies()) {
1317 builder
.modifyColumnFamily(
1318 ColumnFamilyDescriptorBuilder
.newBuilder(c
).setValue(attrName
, attrValue
).build());
1320 // update metadata for all regions of this table
1321 admin
.modifyTable(builder
.build());
1323 admin
.enableTable(tableAname
);
1325 // Test that attribute changes were applied
1326 desc
= a
.getDescriptor();
1327 assertEquals("wrong table descriptor returned", desc
.getTableName(), tableAname
);
1328 // check HTD attribute
1329 value
= desc
.getValue(attrName
);
1330 assertNotNull("missing HTD attribute value", value
);
1331 assertFalse("HTD attribute value is incorrect", Bytes
.compareTo(value
, attrValue
) != 0);
1332 // check HCD attribute
1333 for (ColumnFamilyDescriptor c
: desc
.getColumnFamilies()) {
1334 value
= c
.getValue(attrName
);
1335 assertNotNull("missing HCD attribute value", value
);
1336 assertFalse("HCD attribute value is incorrect", Bytes
.compareTo(value
, attrValue
) != 0);