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
.junit
.Assert
.assertEquals
;
22 import java
.util
.List
;
23 import java
.util
.concurrent
.ExecutionException
;
24 import java
.util
.stream
.Collectors
;
25 import java
.util
.stream
.IntStream
;
26 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
27 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
31 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
32 import org
.junit
.AfterClass
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.ClassRule
;
35 import org
.junit
.Test
;
36 import org
.junit
.experimental
.categories
.Category
;
38 @Category({ MediumTests
.class, ClientTests
.class })
39 public class TestRawAsyncTablePartialScan
{
42 public static final HBaseClassTestRule CLASS_RULE
=
43 HBaseClassTestRule
.forClass(TestRawAsyncTablePartialScan
.class);
45 private static final HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
47 private static TableName TABLE_NAME
= TableName
.valueOf("async");
49 private static byte[] FAMILY
= Bytes
.toBytes("cf");
51 private static byte[][] CQS
=
52 new byte[][] { Bytes
.toBytes("cq1"), Bytes
.toBytes("cq2"), Bytes
.toBytes("cq3") };
54 private static int COUNT
= 100;
56 private static AsyncConnection CONN
;
58 private static AsyncTable
<?
> TABLE
;
61 public static void setUp() throws Exception
{
62 TEST_UTIL
.startMiniCluster(1);
63 TEST_UTIL
.createTable(TABLE_NAME
, FAMILY
);
64 TEST_UTIL
.waitTableAvailable(TABLE_NAME
);
65 CONN
= ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get();
66 TABLE
= CONN
.getTable(TABLE_NAME
);
68 .putAll(IntStream
.range(0, COUNT
)
69 .mapToObj(i
-> new Put(Bytes
.toBytes(String
.format("%02d", i
)))
70 .addColumn(FAMILY
, CQS
[0], Bytes
.toBytes(i
))
71 .addColumn(FAMILY
, CQS
[1], Bytes
.toBytes(2 * i
))
72 .addColumn(FAMILY
, CQS
[2], Bytes
.toBytes(3 * i
)))
73 .collect(Collectors
.toList()))
78 public static void tearDown() throws Exception
{
80 TEST_UTIL
.shutdownMiniCluster();
84 public void testBatchDoNotAllowPartial() throws InterruptedException
, ExecutionException
{
85 // we set batch to 2 and max result size to 1, then server will only returns one result per call
86 // but we should get 2 + 1 for every row.
87 List
<Result
> results
= TABLE
.scanAll(new Scan().setBatch(2).setMaxResultSize(1)).get();
88 assertEquals(2 * COUNT
, results
.size());
89 for (int i
= 0; i
< COUNT
; i
++) {
90 Result firstTwo
= results
.get(2 * i
);
91 assertEquals(String
.format("%02d", i
), Bytes
.toString(firstTwo
.getRow()));
92 assertEquals(2, firstTwo
.size());
93 assertEquals(i
, Bytes
.toInt(firstTwo
.getValue(FAMILY
, CQS
[0])));
94 assertEquals(2 * i
, Bytes
.toInt(firstTwo
.getValue(FAMILY
, CQS
[1])));
96 Result secondOne
= results
.get(2 * i
+ 1);
97 assertEquals(String
.format("%02d", i
), Bytes
.toString(secondOne
.getRow()));
98 assertEquals(1, secondOne
.size());
99 assertEquals(3 * i
, Bytes
.toInt(secondOne
.getValue(FAMILY
, CQS
[2])));
104 public void testReversedBatchDoNotAllowPartial() throws InterruptedException
, ExecutionException
{
105 // we set batch to 2 and max result size to 1, then server will only returns one result per call
106 // but we should get 2 + 1 for every row.
107 List
<Result
> results
=
108 TABLE
.scanAll(new Scan().setBatch(2).setMaxResultSize(1).setReversed(true)).get();
109 assertEquals(2 * COUNT
, results
.size());
110 for (int i
= 0; i
< COUNT
; i
++) {
111 int row
= COUNT
- i
- 1;
112 Result firstTwo
= results
.get(2 * i
);
113 assertEquals(String
.format("%02d", row
), Bytes
.toString(firstTwo
.getRow()));
114 assertEquals(2, firstTwo
.size());
115 assertEquals(row
, Bytes
.toInt(firstTwo
.getValue(FAMILY
, CQS
[0])));
116 assertEquals(2 * row
, Bytes
.toInt(firstTwo
.getValue(FAMILY
, CQS
[1])));
118 Result secondOne
= results
.get(2 * i
+ 1);
119 assertEquals(String
.format("%02d", row
), Bytes
.toString(secondOne
.getRow()));
120 assertEquals(1, secondOne
.size());
121 assertEquals(3 * row
, Bytes
.toInt(secondOne
.getValue(FAMILY
, CQS
[2])));