Skip to content

Commit 8bbb915

Browse files
committed
[Test] add testcases for hbase timeseries model get/scan
1 parent 758aa18 commit 8bbb915

File tree

4 files changed

+407
-4
lines changed

4 files changed

+407
-4
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*-
2+
* #%L
3+
* OBKV HBase Client Framework
4+
* %%
5+
* Copyright (C) 2025 OceanBase Group
6+
* %%
7+
* OBKV HBase Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.hbase.secondary;
19+
20+
import com.alipay.oceanbase.hbase.OHTableClient;
21+
import com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil;
22+
import com.alipay.oceanbase.hbase.util.ObHTableTestUtil;
23+
import com.alipay.oceanbase.hbase.util.TableTemplateManager;
24+
import org.apache.hadoop.hbase.Cell;
25+
import org.apache.hadoop.hbase.client.Get;
26+
import org.apache.hadoop.hbase.client.Put;
27+
import org.apache.hadoop.hbase.client.Result;
28+
import org.junit.*;
29+
30+
import java.util.LinkedHashMap;
31+
import java.util.LinkedList;
32+
import java.util.List;
33+
import java.util.Map;
34+
35+
import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*;
36+
import static com.alipay.oceanbase.hbase.util.ObHTableTestUtil.FOR_EACH;
37+
import static com.alipay.oceanbase.hbase.util.TableTemplateManager.TIMESERIES_TABLES;
38+
import static org.apache.hadoop.hbase.util.Bytes.toBytes;
39+
import static org.junit.Assert.assertEquals;
40+
41+
42+
public class OHTableTimeSeriesGetTest {
43+
private static List<String> tableNames = new LinkedList<String>();
44+
private static Map<String, List<String>> group2tableNames = new LinkedHashMap<>();
45+
46+
47+
@BeforeClass
48+
public static void before() throws Exception {
49+
openDistributedExecute();
50+
for (TableTemplateManager.TableType type : TIMESERIES_TABLES) {
51+
createTables(type, tableNames, group2tableNames, true);
52+
}
53+
}
54+
55+
@AfterClass
56+
public static void finish() throws Exception {
57+
closeDistributedExecute();
58+
dropTables(tableNames, group2tableNames);
59+
}
60+
61+
@Before
62+
public void prepareCase() throws Exception {
63+
truncateTables(tableNames, group2tableNames);
64+
}
65+
66+
67+
public static void testGetImpl(String tableName) throws Exception {
68+
OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName));
69+
hTable.init();
70+
71+
// 0. prepare data
72+
String family = getColumnFamilyName(tableName);
73+
String key = "putKey";
74+
String[] columns = {"putColumn1", "putColumn2", "putColumn3"};
75+
String[] columns1 = {"putColumn1", "putColumn2"};
76+
String[] columns2 = {"putColumn3"};
77+
String[] values = {"version1", "version2"}; // each column have two versions
78+
long curTs = System.currentTimeMillis();
79+
long[] ts = {curTs, curTs+1}; // each column have two versions
80+
for (int i = 0; i < values.length; i++) {
81+
for (int j = 0; j < columns1.length; j++) {
82+
Put put = new Put(toBytes(key));
83+
put.add(family.getBytes(), columns1[j].getBytes(), ts[i], toBytes(values[i]));
84+
hTable.put(put);
85+
}
86+
}
87+
88+
Put put = new Put(toBytes(key));
89+
for (int i = 0; i < values.length; i++) {
90+
for (int j = 0; j < columns2.length; j++) {
91+
put.add(family.getBytes(), columns2[j].getBytes(), ts[i], toBytes(values[i]));
92+
}
93+
}
94+
hTable.put(put);
95+
96+
97+
// 1. get specify column
98+
{
99+
int index = 0;
100+
Get get = new Get(key.getBytes());
101+
get.addColumn(family.getBytes(), columns[index].getBytes());
102+
Result r = hTable.get(get);
103+
Cell cells[] = r.rawCells();
104+
assertEquals(2, cells.length);
105+
sortCells(cells);
106+
for (int i = values.length - 1; i >= 0; i--) {
107+
AssertKeyValue(key, columns[index], ts[i], values[i], cells[values.length - 1-i]);
108+
}
109+
}
110+
111+
// 2. get do not specify column
112+
{
113+
Get get = new Get(key.getBytes());
114+
get.addFamily(family.getBytes());
115+
Result result = hTable.get(get);
116+
Cell[] cells = result.rawCells();
117+
assertEquals(columns.length * values.length, cells.length);
118+
sortCells(cells);
119+
int idx = 0;
120+
for (int i = 0; i < columns.length; i++) {
121+
for (int j = values.length - 1; j >= 0; j--) {
122+
ObHTableSecondaryPartUtil.AssertKeyValue(key, columns[i], ts[j], values[j], cells[idx]);
123+
idx++;
124+
}
125+
}
126+
}
127+
128+
// 3. get specify time range
129+
{
130+
Get get = new Get(key.getBytes());
131+
get.addFamily(family.getBytes());
132+
get.setTimeStamp(ts[1]);
133+
Result r = hTable.get(get);
134+
Cell cells[] = r.rawCells();
135+
assertEquals(columns.length, cells.length);
136+
sortCells(cells);
137+
for (int i = 0; i < columns.length; i++) {
138+
AssertKeyValue(key, columns[i], values[1],cells[i]);
139+
}
140+
}
141+
142+
hTable.close();
143+
}
144+
145+
@Test
146+
public void testGet() throws Throwable {
147+
FOR_EACH(tableNames, OHTableTimeSeriesGetTest::testGetImpl);
148+
}
149+
}
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/*-
2+
* #%L
3+
* OBKV HBase Client Framework
4+
* %%
5+
* Copyright (C) 2025 OceanBase Group
6+
* %%
7+
* OBKV HBase Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.hbase.secondary;
19+
20+
import com.alipay.oceanbase.hbase.OHTableClient;
21+
import com.alipay.oceanbase.hbase.util.ObHTableTestUtil;
22+
import com.alipay.oceanbase.hbase.util.TableTemplateManager;
23+
import org.apache.hadoop.hbase.Cell;
24+
import org.apache.hadoop.hbase.client.*;
25+
import org.junit.*;
26+
27+
import java.util.*;
28+
29+
import static com.alipay.oceanbase.hbase.util.ObHTableSecondaryPartUtil.*;
30+
import static com.alipay.oceanbase.hbase.util.ObHTableTestUtil.FOR_EACH;
31+
import static com.alipay.oceanbase.hbase.util.TableTemplateManager.TIMESERIES_TABLES;
32+
import static com.alipay.oceanbase.hbase.util.TableTemplateManager.TableType.SECONDARY_PARTITIONED_TIME_RANGE_KEY;
33+
import static org.apache.hadoop.hbase.util.Bytes.toBytes;
34+
import static org.junit.Assert.assertEquals;
35+
36+
public class OHTableTimeSeriesScanTest {
37+
private static List<String> tableNames = new LinkedList<String>();
38+
private static Map<String, List<String>> group2tableNames = new LinkedHashMap<>();
39+
40+
@BeforeClass
41+
public static void before() throws Exception {
42+
openDistributedExecute();
43+
for (TableTemplateManager.TableType type : TIMESERIES_TABLES) {
44+
createTables(type, tableNames, group2tableNames, true);
45+
}
46+
}
47+
48+
@AfterClass
49+
public static void finish() throws Exception {
50+
closeDistributedExecute();
51+
dropTables(tableNames, group2tableNames);
52+
}
53+
54+
@Before
55+
public void prepareCase() throws Exception {
56+
truncateTables(tableNames, group2tableNames);
57+
}
58+
59+
public static void testScanImpl(String tableName) throws Exception {
60+
OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableName));
61+
hTable.init();
62+
63+
// 0. prepare data
64+
// putKey1 putColumn1 putValue1,ts
65+
// putKey1 putColumn1 putValue2,ts+1
66+
// putKey1 putColumn2 putValue1,ts
67+
// putKey1 putColumn2 putValue2,ts+1
68+
// ...
69+
String family = getColumnFamilyName(tableName);
70+
long ts = System.currentTimeMillis();
71+
72+
String keys[] = {"putKey1", "putKey2", "putKey3"};
73+
String keys1[] = {"putKey1", "putKey2"};
74+
String keys2[] = {"putKey3"};
75+
String endKey = "putKey4";
76+
String columns[] = {"putColumn1", "putColumn2"};
77+
String values[] = {"putValue1", "putValue2"};
78+
long tss[] = {ts, ts + 1};
79+
80+
for (String key : keys1) {
81+
Put put = new Put(toBytes(key));
82+
for (String column : columns) {
83+
for (int i = 0; i < values.length; i++) {
84+
put.add(family.getBytes(), column.getBytes(), tss[i], values[i].getBytes());
85+
}
86+
}
87+
hTable.put(put);
88+
}
89+
90+
for (String key : keys2) {
91+
for (String column : columns) {
92+
for (int i = 0; i < values.length; i++) {
93+
Put put = new Put(toBytes(key));
94+
put.add(family.getBytes(), column.getBytes(), tss[i], values[i].getBytes());
95+
hTable.put(put);
96+
}
97+
}
98+
}
99+
100+
// 1. scan specify column
101+
{
102+
Scan scan = new Scan(keys[0].getBytes(), endKey.getBytes());
103+
scan.addColumn(family.getBytes(), columns[0].getBytes());
104+
ResultScanner scanner = hTable.getScanner(scan);
105+
Cell cells[] = getCellsFromScanner(scanner).toArray(new Cell[0]);
106+
Assert.assertEquals(keys.length*values.length, cells.length);
107+
sortCells(cells);
108+
int idx = 0;
109+
for (String key : keys) {
110+
for (int i = values.length-1; i >= 0; i--) {
111+
AssertKeyValue(key, columns[0], tss[i], values[i], cells[idx++]);
112+
}
113+
}
114+
}
115+
116+
// 2. scan do not specify column
117+
{
118+
Scan scan = new Scan(keys[0].getBytes(), endKey.getBytes());
119+
scan.addFamily(family.getBytes());
120+
ResultScanner scanner = hTable.getScanner(scan);
121+
Cell cells[] = getCellsFromScanner(scanner).toArray(new Cell[0]);
122+
Assert.assertEquals(keys.length*columns.length*values.length, cells.length);
123+
sortCells(cells);
124+
int idx = 0;
125+
for (String key : keys) {
126+
for (String column : columns) {
127+
for (int i = values.length-1; i >= 0; i--) {
128+
AssertKeyValue(key, column, tss[i], values[i], cells[idx++]);
129+
}
130+
}
131+
}
132+
}
133+
134+
// 3. scan specify time range
135+
{
136+
Scan scan = new Scan(keys[0].getBytes(), endKey.getBytes());
137+
scan.setTimeStamp(tss[1]);
138+
scan.addFamily(family.getBytes());
139+
ResultScanner scanner = hTable.getScanner(scan);
140+
Cell cells[] = getCellsFromScanner(scanner).toArray(new Cell[0]);
141+
assertEquals(keys.length * columns.length, cells.length);
142+
sortCells(cells);
143+
int idx = 0;
144+
for (String key : keys) {
145+
for (String column : columns) {
146+
AssertKeyValue(key, column, tss[1], values[1], cells[idx++]);
147+
}
148+
}
149+
}
150+
151+
152+
// 4. scan using setStartRow/setEndRow
153+
{
154+
Scan scan = new Scan();
155+
scan.setStartRow(keys[0].getBytes());
156+
scan.setStopRow(endKey.getBytes());
157+
ResultScanner scanner = hTable.getScanner(scan);
158+
Cell cells[] = getCellsFromScanner(scanner).toArray(new Cell[0]);
159+
assertEquals(keys.length * columns.length * values.length, cells.length);
160+
sortCells(cells);
161+
int idx = 0;
162+
for (String key : keys) {
163+
for (String column : columns) {
164+
for (int i = values.length-1; i >= 0; i--) {
165+
AssertKeyValue(key, column, tss[i], values[i], cells[idx++]);
166+
}
167+
}
168+
}
169+
}
170+
171+
// 5. scan using batch
172+
{
173+
int batchSize = 2;
174+
Scan scan = new Scan(keys[0].getBytes(), endKey.getBytes());
175+
scan.addFamily(family.getBytes());
176+
scan.setBatch(batchSize);
177+
ResultScanner scanner = hTable.getScanner(scan);
178+
Result result = null;
179+
int resultSize = (keys.length * columns.length * values.length) / batchSize;
180+
for (int i = 0; i < resultSize; i++) {
181+
result = scanner.next();
182+
Assert.assertEquals(2, result.size());
183+
}
184+
result = scanner.next();
185+
Assert.assertEquals(null, result);
186+
}
187+
188+
// 7. scan using setAllowPartialResults/setAllowPartialResults
189+
{
190+
Scan scan = new Scan(keys[0].getBytes(), endKey.getBytes());
191+
scan.addFamily(family.getBytes());
192+
scan.setMaxResultSize(10);
193+
scan.setAllowPartialResults(true);
194+
ResultScanner scanner = hTable.getScanner(scan);
195+
int resultSize = keys.length * columns.length * values.length;
196+
for (int i = 0; i < resultSize; i++) {
197+
Result result = scanner.next();
198+
Assert.assertEquals(1, result.size());
199+
}
200+
Result result = scanner.next();
201+
Assert.assertEquals(null, result);
202+
}
203+
204+
// 8. scan in reverse
205+
{
206+
// Scan scan = new Scan(keys[2].getBytes(), keys[0].getBytes());
207+
// scan.addFamily(family.getBytes());
208+
// scan.setReversed(true);
209+
// ResultScanner scanner = hTable.getScanner(scan);
210+
// List<Cell> cells = getCellsFromScanner(scanner);
211+
//
212+
// int cellIndex = 0;
213+
// for (int i = 1; i >= 0; i--) {
214+
// for (String column : columns) {
215+
// AssertKeyValue(keys[i], column, lastTs, latestValue, cells.get(cellIndex));
216+
// cellIndex++;
217+
// }
218+
// }
219+
// assertEquals(columns.length * 2, cells.size());
220+
}
221+
}
222+
223+
@Test
224+
public void testScan() throws Throwable {
225+
FOR_EACH(tableNames, OHTableTimeSeriesScanTest::testScanImpl);
226+
}
227+
}

0 commit comments

Comments
 (0)