|
| 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