From caf03649ffda34c2d2a1a18751f876ad7fc34646 Mon Sep 17 00:00:00 2001 From: WeiXinChan Date: Wed, 5 Mar 2025 21:54:05 +0800 Subject: [PATCH] fix batch get route bug --- .../com/alipay/oceanbase/hbase/OHTable.java | 2 +- .../hbase/OHTableSecondaryPartMcfTest.java | 81 +++++++++++++++++-- .../hbase/OHTableSecondaryPartTest.java | 81 ++++++++++++++++++- 3 files changed, 153 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/alipay/oceanbase/hbase/OHTable.java b/src/main/java/com/alipay/oceanbase/hbase/OHTable.java index 3851012c..f3634fd9 100644 --- a/src/main/java/com/alipay/oceanbase/hbase/OHTable.java +++ b/src/main/java/com/alipay/oceanbase/hbase/OHTable.java @@ -2108,7 +2108,7 @@ private BatchOperation buildBatchOperation(String tableName, List obTableQuery = buildObTableQuery(get, columnFilters); ObTableClientQueryImpl query = new ObTableClientQueryImpl(tableName, obTableQuery, obTableClient); try { - query.setRowKey(row(colVal("K", Bytes.toString(get.getRow())), colVal("Q", null), colVal("T", null))); + query.setRowKey(row(colVal("K", Bytes.toString(get.getRow())), colVal("Q", null), colVal("T", Integer.MAX_VALUE))); } catch (Exception e) { logger.error("unexpected error occurs when set row key", e); throw new IOException(e); diff --git a/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartMcfTest.java b/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartMcfTest.java index 642ac140..235b06a6 100644 --- a/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartMcfTest.java +++ b/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartMcfTest.java @@ -18,12 +18,10 @@ package com.alipay.oceanbase.hbase; import com.alipay.oceanbase.hbase.util.ObHTableTestUtil; -import org.apache.hadoop.hbase.client.Put; -import org.apache.hadoop.hbase.client.Table; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.client.*; +import org.apache.hadoop.hbase.filter.CompareFilter; +import org.junit.*; import java.sql.Connection; @@ -283,4 +281,75 @@ public void testPut() throws Exception { hTable.close(); } } + + @Test + public void testIncrement() throws Exception { + for (int i = 0; i < tableNames.length; i++) { + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableNames[i][0])); + hTable.init(); + + String key = "Key"; + String column = "Column"; + Increment increment = new Increment(key.getBytes()); + for (int j = 0; j < tableNames[i].length; j++) { + String family = getColumnFamilyName(tableNames[i][j]); + increment.addColumn(family.getBytes(), column.getBytes(), 1L); + } + hTable.increment(increment); + + hTable.close(); + } + } + + @Test + public void testAppend() throws Exception { + for (int i = 0; i < tableNames.length; i++) { + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableNames[i][0])); + hTable.init(); + + String key = "Key"; + String column = "Column"; + String value = "app"; + Append append = new Append(key.getBytes()); + for (int j = 0; j < tableNames[i].length; j++) { + String family = getColumnFamilyName(tableNames[i][j]); + KeyValue kv1 = new KeyValue(key.getBytes(), family.getBytes(), column.getBytes(), value.getBytes()); + append.add(kv1); + } + hTable.append(append); + + hTable.close(); + } + } + + @Test + public void testGet() throws Exception { + for (int i = 0; i < tableNames.length; i++) { + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableNames[i][0])); + hTable.init(); + + String key = "putKey"; + String column = "putColumn"; + String value = "value"; + long timestamp = System.currentTimeMillis(); + Put put = new Put(toBytes(key)); + for (int j = 0; j < tableNames[i].length; j++) { + String family = getColumnFamilyName(tableNames[i][j]); + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); + } + hTable.put(put); + + Get get = new Get(key.getBytes()); + get.setMaxVersions(Integer.MAX_VALUE); + for (int j = 0; j < tableNames[i].length; j++) { + String family = getColumnFamilyName(tableNames[i][j]); + get.addColumn(family.getBytes(), column.getBytes()); + } + Result r = hTable.get(get); + Assert.assertEquals(1, r.raw().length); + + hTable.close(); + } + } + } diff --git a/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartTest.java b/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartTest.java index 9b99bac4..029dd3c4 100644 --- a/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartTest.java +++ b/src/test/java/com/alipay/oceanbase/hbase/OHTableSecondaryPartTest.java @@ -18,15 +18,16 @@ package com.alipay.oceanbase.hbase; import com.alipay.oceanbase.hbase.util.ObHTableTestUtil; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.CompareFilter; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.*; import java.sql.Connection; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -254,4 +255,76 @@ public void testCheckAndMutate() throws Exception { hTable.close(); } } + + @Test + public void testGet() throws Exception { + for (int i = 0; i < tableNames.length; i++) { + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableNames[i])); + hTable.init(); + + System.out.println("put table " + tableNames[i] + " begin"); + String family = getColumnFamilyName(tableNames[i]); + String key = "putKey"; + String column = "putColumn"; + String value = "value"; + long timestamp = System.currentTimeMillis(); + Put put = new Put(toBytes(key)); + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); + hTable.put(put); + System.out.println("put table " + tableNames[i] + " done"); + + System.out.println("get table " + tableNames[i] + " begin"); + Get get = new Get(key.getBytes()); + get.setMaxVersions(Integer.MAX_VALUE); + get.addColumn(family.getBytes(), column.getBytes()); + Result r = hTable.get(get); + Assert.assertEquals(1, r.raw().length); + System.out.println("get table " + tableNames[i] + " begin"); + + hTable.close(); + } + } + + @Test + public void testBatchGet() throws Exception { + long batchSize = 10; + for (int i = 0; i < tableNames.length; i++) { + OHTableClient hTable = ObHTableTestUtil.newOHTableClient(getTableName(tableNames[i])); + hTable.init(); + + System.out.println("put table " + tableNames[i] + " begin"); + String family = getColumnFamilyName(tableNames[i]); + String column = "putColumn"; + String value = "value"; + long timestamp = System.currentTimeMillis(); + for (int j = 0; j < batchSize; j++) { + String key = "putKey" + j; + Put put = new Put(toBytes(key)); + put.add(family.getBytes(), column.getBytes(), timestamp, toBytes(value)); + hTable.put(put); + } + System.out.println("put table " + tableNames[i] + " done"); + + System.out.println("get table " + tableNames[i] + " begin"); + List gets = new ArrayList<>(); + for (int j = 0; j < batchSize; j++) { + String key = "putKey" + j; + Get get = new Get(key.getBytes()); + get.setMaxVersions(Integer.MAX_VALUE); + get.addColumn(family.getBytes(), column.getBytes()); + gets.add(get); + } + Result[] results = hTable.get(gets); + for (Result result : results) { + for (Cell cell : result.listCells()) { + String Q = Bytes.toString(CellUtil.cloneQualifier(cell)); + String V = Bytes.toString(CellUtil.cloneValue(cell)); + System.out.println("Column: " + Q + ", Value: " + V); + } + } + System.out.println("get table " + tableNames[i] + " begin"); + + hTable.close(); + } + } }