rowkeys = new ArrayList<>();
- for (int i = 1; i <= 13; i++) {
- rowkeys.add("test-key-" + i);
- }
- HBASE_TEMPLATE.batchDelete(TABLE_NAME, rowkeys.toArray(new String[0]));
- }
-
-}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateGetTest.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateGetTest.java
index 3dc250f3..a0e953ce 100644
--- a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateGetTest.java
+++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateGetTest.java
@@ -1,26 +1,28 @@
package io.github.dunwu.javadb.hbase;
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.json.JSONUtil;
-import io.github.dunwu.javadb.hbase.entity.ColumnDo;
-import io.github.dunwu.javadb.hbase.entity.FamilyDo;
-import io.github.dunwu.javadb.hbase.entity.RowDo;
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
+import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity;
+import io.github.dunwu.javadb.hbase.entity.common.ColumnDo;
+import io.github.dunwu.javadb.hbase.entity.common.FamilyDo;
+import io.github.dunwu.javadb.hbase.entity.common.RowDo;
+import io.github.dunwu.javadb.hbase.util.JsonUtil;
+import org.apache.hadoop.hbase.client.Put;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
+import java.math.BigDecimal;
+import java.util.Date;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
/**
- * Get 测试集
- *
- * 测试前,先完整执行 {@link HbaseTemplatePutTest}
+ * Hbase Get 测试
*
* @author Zhang Peng
* @date 2023-11-13
@@ -40,85 +42,267 @@ public class HbaseTemplateGetTest {
}
@Test
- @DisplayName("查询实体")
- public void getEntity() throws IOException {
- User user = HBASE_TEMPLATE.getEntity(TABLE_NAME, "test-key-3", "f1", User.class);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonStr(user)));
- Assertions.assertThat(user).isNotNull();
+ @DisplayName("put、get 单列数据")
+ public void test00() throws IOException {
+ long timestamp = System.currentTimeMillis();
+ HBASE_TEMPLATE.put(TABLE_NAME, "test-key-0", "f1", "name", "user0");
+ ColumnDo columnDo = HBASE_TEMPLATE.getColumn(TABLE_NAME, "test-key-0", "f1", "name");
+ Assertions.assertThat(columnDo).isNotNull();
+ Assertions.assertThat(columnDo.getColumn()).isEqualTo("name");
+ Assertions.assertThat(columnDo.getValue()).isEqualTo("user0");
+
+ HBASE_TEMPLATE.put(TABLE_NAME, "test-key-0", timestamp, "f2", "姓名", "张三");
+ ColumnDo columnDo2 = HBASE_TEMPLATE.getColumn(TABLE_NAME, "test-key-0", "f2", "姓名");
+ Assertions.assertThat(columnDo2).isNotNull();
+ Assertions.assertThat(columnDo2.getColumn()).isEqualTo("姓名");
+ Assertions.assertThat(columnDo2.getValue()).isEqualTo("张三");
+ Assertions.assertThat(columnDo2.getTimestamp()).isEqualTo(timestamp);
+
+ HBASE_TEMPLATE.delete(TABLE_NAME, "test-key-0");
+ columnDo = HBASE_TEMPLATE.getColumn(TABLE_NAME, "test-key-0", "f1", "name");
+ Assertions.assertThat(columnDo).isNull();
+ columnDo2 = HBASE_TEMPLATE.getColumn(TABLE_NAME, "test-key-0", "f2", "姓名");
+ Assertions.assertThat(columnDo2).isNull();
}
@Test
- @DisplayName("查询实体列表")
- public void getEntityList() throws IOException {
- List rows = Arrays.asList("test-key-3", "test-key-4", "test-key-5");
- List list = HBASE_TEMPLATE.getEntityList(TABLE_NAME, rows.toArray(new String[0]), "f1", User.class);
- System.out.println(StrUtil.format("查询实体列表: {}", JSONUtil.toJsonStr(list)));
- Assertions.assertThat(list).isNotEmpty();
- Assertions.assertThat(list.size()).isEqualTo(rows.size());
+ @DisplayName("put、get 多列数据")
+ public void test01() throws IOException {
+
+ String row = "test-key-1";
+ long timestamp = System.currentTimeMillis();
+ Map map1 = new HashMap<>(2);
+ map1.put("id", 1);
+ map1.put("name", "zhangsan");
+ Map map2 = new HashMap<>(2);
+ map2.put("编号", 1);
+ map2.put("姓名", "张三");
+
+ HBASE_TEMPLATE.put(TABLE_NAME, row, timestamp, "f1", map1);
+ HBASE_TEMPLATE.put(TABLE_NAME, row, timestamp, "f2", map2);
+
+ Map f1ColumnMap = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, row, "f1", "id", "name");
+ Assertions.assertThat(f1ColumnMap).isNotEmpty();
+ Assertions.assertThat(f1ColumnMap.get("id")).isNotNull();
+ Assertions.assertThat(f1ColumnMap.get("id").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f1ColumnMap.get("name")).isNotNull();
+ Assertions.assertThat(f1ColumnMap.get("name").getValue()).isEqualTo("zhangsan");
+
+ Map f2ColumnMap = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, row, "f2", "编号", "姓名");
+ Assertions.assertThat(f2ColumnMap).isNotEmpty();
+ Assertions.assertThat(f2ColumnMap.get("编号")).isNotNull();
+ Assertions.assertThat(f2ColumnMap.get("编号").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f2ColumnMap.get("姓名")).isNotNull();
+ Assertions.assertThat(f2ColumnMap.get("姓名").getValue()).isEqualTo("张三");
+
+ HBASE_TEMPLATE.delete(TABLE_NAME, row);
+ f1ColumnMap = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, row, "f1", "id", "name");
+ Assertions.assertThat(f1ColumnMap).isEmpty();
+ f2ColumnMap = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, row, "f2", "编号", "姓名");
+ Assertions.assertThat(f2ColumnMap).isEmpty();
}
@Test
- @DisplayName("查询列")
- public void getColumn() throws IOException {
- ColumnDo columnDo = HBASE_TEMPLATE.getColumn(TABLE_NAME, "test-key-1", "f1", "key");
- System.out.println(StrUtil.format("查询单列: {}", JSONUtil.toJsonStr(columnDo)));
+ @DisplayName("put、get 列族数据")
+ public void test02() throws IOException {
+
+ String row = "test-key-2";
+ long timestamp = System.currentTimeMillis();
+ Map map1 = new HashMap<>(2);
+ map1.put("id", 1);
+ map1.put("name", "zhangsan");
+ Map map2 = new HashMap<>(2);
+ map2.put("编号", 1);
+ map2.put("姓名", "张三");
+
+ HBASE_TEMPLATE.put(TABLE_NAME, row, timestamp, "f1", map1);
+ HBASE_TEMPLATE.put(TABLE_NAME, row, timestamp, "f2", map2);
+
+ FamilyDo f1 = HBASE_TEMPLATE.getFamily(TABLE_NAME, row, "f1");
+ Assertions.assertThat(f1).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("id")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("id").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f1.getColumnMap().get("name")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("name").getValue()).isEqualTo("zhangsan");
+
+ FamilyDo f2 = HBASE_TEMPLATE.getFamily(TABLE_NAME, row, "f2");
+ Assertions.assertThat(f2).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("编号")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("编号").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f2.getColumnMap().get("姓名")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("姓名").getValue()).isEqualTo("张三");
+
+ HBASE_TEMPLATE.delete(TABLE_NAME, row);
+ f1 = HBASE_TEMPLATE.getFamily(TABLE_NAME, row, "f1");
+ Assertions.assertThat(f1).isNull();
+ f2 = HBASE_TEMPLATE.getFamily(TABLE_NAME, row, "f2");
+ Assertions.assertThat(f2).isNull();
}
@Test
- @DisplayName("查询多列")
- public void getColumnMap() throws IOException {
- Map columnMap = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, "test-key-3", "f1");
- System.out.println(StrUtil.format("查询多列: {}", JSONUtil.toJsonStr(columnMap)));
- Assertions.assertThat(columnMap).isNotEmpty();
-
- Map columnMap2 = HBASE_TEMPLATE.getColumnMap(TABLE_NAME, "test-key-3", "f1", "id", "name");
- System.out.println(StrUtil.format("查询多列: {}", JSONUtil.toJsonStr(columnMap2)));
- Assertions.assertThat(columnMap2).isNotEmpty();
+ @DisplayName("put、get 单行数据")
+ public void test03() throws IOException {
+
+ String row = "test-key-3";
+ long timestamp = System.currentTimeMillis();
+ Map map1 = new HashMap<>(2);
+ map1.put("id", 1);
+ map1.put("name", "zhangsan");
+ Map map2 = new HashMap<>(2);
+ map2.put("编号", 1);
+ map2.put("姓名", "张三");
+ Map> familyMap = new HashMap<>(2);
+ familyMap.put("f1", map1);
+ familyMap.put("f2", map2);
+
+ HBASE_TEMPLATE.put(TABLE_NAME, row, timestamp, familyMap);
+
+ RowDo rowDo = HBASE_TEMPLATE.getRow(TABLE_NAME, row);
+ Assertions.assertThat(rowDo).isNotNull();
+
+ FamilyDo f1 = rowDo.getFamilyMap().get("f1");
+ Assertions.assertThat(f1).isNotNull();
+ Assertions.assertThat(f1.getColumnMap()).isNotEmpty();
+ Assertions.assertThat(f1.getColumnMap().get("id")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("id").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f1.getColumnMap().get("name")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("name").getValue()).isEqualTo("zhangsan");
+
+ FamilyDo f2 = rowDo.getFamilyMap().get("f2");
+ Assertions.assertThat(f2).isNotNull();
+ Assertions.assertThat(f2.getColumnMap()).isNotEmpty();
+ Assertions.assertThat(f2.getColumnMap().get("编号")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("编号").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f2.getColumnMap().get("姓名")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("姓名").getValue()).isEqualTo("张三");
+
+ HBASE_TEMPLATE.delete(TABLE_NAME, row);
+ rowDo = HBASE_TEMPLATE.getRow(TABLE_NAME, row);
+ Assertions.assertThat(rowDo).isNull();
}
@Test
- @DisplayName("查询列族")
- public void getFamily() throws IOException {
- FamilyDo familyDo = HBASE_TEMPLATE.getFamily(TABLE_NAME, "test-key-7", "f1");
- System.out.println(StrUtil.format("查询列族: {}", JSONUtil.toJsonStr(familyDo)));
- Assertions.assertThat(familyDo).isNotNull();
- Assertions.assertThat(familyDo.getFamily()).isEqualTo("f1");
-
- FamilyDo familyDo2 = HBASE_TEMPLATE.getFamily(TABLE_NAME, "test-key-7", "f2");
- System.out.println(StrUtil.format("查询列族: {}", JSONUtil.toJsonStr(familyDo2)));
- Assertions.assertThat(familyDo2).isNotNull();
- Assertions.assertThat(familyDo2.getFamily()).isEqualTo("f2");
+ @DisplayName("put get 多行数据")
+ public void test04() throws IOException, InterruptedException {
+
+ long timestamp = System.currentTimeMillis();
+
+ Map columnMap1 = new HashMap<>(2);
+ columnMap1.put("id", 1);
+ columnMap1.put("name", "zhangsan");
+ Put put = HbaseTemplate.newPut("test-key-1", timestamp, "f1", columnMap1);
+
+ Map columnMap2 = new HashMap<>(2);
+ columnMap2.put("id", 2);
+ columnMap2.put("name", "lisi");
+ Put put2 = HbaseTemplate.newPut("test-key-2", timestamp, "f1", columnMap2);
+
+ List puts = CollectionUtil.newArrayList(put, put2);
+
+ HBASE_TEMPLATE.batchPut(TABLE_NAME, puts);
+
+ Map rowMap = HBASE_TEMPLATE.getRowMap(TABLE_NAME, "test-key-1", "test-key-2");
+
+ RowDo rowDo1 = rowMap.get("test-key-1");
+ Assertions.assertThat(rowDo1).isNotNull();
+ FamilyDo f1 = rowDo1.getFamilyMap().get("f1");
+ Assertions.assertThat(f1).isNotNull();
+ Assertions.assertThat(f1.getColumnMap()).isNotEmpty();
+ Assertions.assertThat(f1.getColumnMap().get("id")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("id").getValue()).isEqualTo(String.valueOf(1));
+ Assertions.assertThat(f1.getColumnMap().get("name")).isNotNull();
+ Assertions.assertThat(f1.getColumnMap().get("name").getValue()).isEqualTo("zhangsan");
+
+ RowDo rowDo2 = rowMap.get("test-key-2");
+ FamilyDo f2 = rowDo2.getFamilyMap().get("f1");
+ Assertions.assertThat(f2).isNotNull();
+ Assertions.assertThat(f2.getColumnMap()).isNotEmpty();
+ Assertions.assertThat(f2.getColumnMap().get("id")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("id").getValue()).isEqualTo(String.valueOf(2));
+ Assertions.assertThat(f2.getColumnMap().get("name")).isNotNull();
+ Assertions.assertThat(f2.getColumnMap().get("name").getValue()).isEqualTo("lisi");
+
+ HBASE_TEMPLATE.batchDelete(TABLE_NAME, "test-key-1", "test-key-2");
+ rowDo1 = HBASE_TEMPLATE.getRow(TABLE_NAME, "test-key-1");
+ Assertions.assertThat(rowDo1).isNull();
+ rowDo2 = HBASE_TEMPLATE.getRow(TABLE_NAME, "test-key-2");
+ Assertions.assertThat(rowDo2).isNull();
}
@Test
- @DisplayName("查询多列族")
- public void getFamilyMap() throws IOException {
- Map> familyColumnMap = new HashMap<>();
- familyColumnMap.put("f1", Collections.singleton("id"));
- familyColumnMap.put("f2", Collections.singleton("name"));
- Map familyMap = HBASE_TEMPLATE.getFamilyMap(TABLE_NAME, "test-key-7", familyColumnMap);
- System.out.println(StrUtil.format("查询多列族: {}", JSONUtil.toJsonStr(familyMap)));
- Assertions.assertThat(familyMap).isNotEmpty();
- Assertions.assertThat(familyMap.size()).isEqualTo(familyColumnMap.size());
+ @DisplayName("put get 简单 Java 实体数据")
+ public void test05() throws IOException, InterruptedException {
+
+ User originUser1 = new User(1, "user1");
+ HBASE_TEMPLATE.put(TABLE_NAME, "test-key-1", "f1", originUser1);
+ User user1 = HBASE_TEMPLATE.getEntity(TABLE_NAME, "test-key-1", "f1", User.class);
+ Assertions.assertThat(user1).isNotNull();
+ Assertions.assertThat(ObjectUtil.equals(originUser1, user1)).isTrue();
+
+ HBASE_TEMPLATE.batchDelete(TABLE_NAME, "test-key-1", "test-key-2");
+ user1 = HBASE_TEMPLATE.getEntity(TABLE_NAME, "test-key-1", "f1", User.class);
+ Assertions.assertThat(user1).isNull();
}
@Test
- @DisplayName("查询行")
- public void getRow() throws IOException {
- RowDo rowDo = HBASE_TEMPLATE.getRow(TABLE_NAME, "test-key-7");
- System.out.println(StrUtil.format("查询行: {}", JSONUtil.toJsonStr(rowDo)));
- Assertions.assertThat(rowDo).isNotNull();
- Assertions.assertThat(rowDo.getRow()).isEqualTo("test-key-7");
+ @DisplayName("put get 实现 BaseHbaseEntity 的简单 Java 实体数据")
+ public void test06() throws IOException, InterruptedException {
+
+ Product product1 = new Product("1", "product1", new BigDecimal(4000.0));
+ Product product2 = new Product("2", "product2", new BigDecimal(5000.0));
+ List products = CollectionUtil.newArrayList(product1, product2);
+ HBASE_TEMPLATE.batchPut(TABLE_NAME, "f1", products);
+
+ List rows = products.stream().map(BaseHbaseEntity::getRowKey).collect(Collectors.toList());
+ List list = HBASE_TEMPLATE.getEntityList(TABLE_NAME, rows, "f1", Product.class);
+ Assertions.assertThat(list).isNotEmpty();
+ Assertions.assertThat(list.size()).isEqualTo(rows.size());
+
+ HBASE_TEMPLATE.batchDelete(TABLE_NAME, rows.toArray(new String[0]));
+ product1 = HBASE_TEMPLATE.getEntity(TABLE_NAME, "test-key-1", "f1", Product.class);
+ Assertions.assertThat(product1).isNull();
+ product2 = HBASE_TEMPLATE.getEntity(TABLE_NAME, "test-key-2", "f1", Product.class);
+ Assertions.assertThat(product2).isNull();
+ list = HBASE_TEMPLATE.getEntityList(TABLE_NAME, rows, "f1", Product.class);
+ Assertions.assertThat(list).isEmpty();
}
@Test
- @DisplayName("批量查询行记录")
- public void getRowMap() throws IOException {
- String[] rows = new String[] { "test-key-3", "test-key-4", "test-key-7" };
- Map rowMap = HBASE_TEMPLATE.getRowMap(TABLE_NAME, rows);
- System.out.println(StrUtil.format("批量查询行记录: {}", JSONUtil.toJsonStr(rowMap)));
- Assertions.assertThat(rowMap).isNotEmpty();
- Assertions.assertThat(rowMap.size()).isEqualTo(rows.length);
+ @DisplayName("put get 实现 BaseHbaseEntity 的复杂 Java 实体数据")
+ public void test07() throws IOException {
+
+ Date now = new Date();
+ Product product1 = new Product("1", "product1", new BigDecimal(4000.0));
+ Product product2 = new Product("2", "product2", new BigDecimal(5000.0));
+ List products = CollectionUtil.newArrayList(product1, product2);
+ User user1 = new User(1, "user1");
+ Map tags = new LinkedHashMap<>();
+ tags.put("type", "tool");
+ tags.put("color", "red");
+
+ Order originOrder = Order.builder()
+ .id("1")
+ .user(user1)
+ .products(products)
+ .desc("测试订单")
+ .date(now)
+ .tags(tags)
+ .build();
+
+ HBASE_TEMPLATE.put(TABLE_NAME, "f1", originOrder);
+
+ Order order = HBASE_TEMPLATE.getEntity(TABLE_NAME, originOrder.getRowKey(), "f1", Order.class);
+ Assertions.assertThat(order).isNotNull();
+ Assertions.assertThat(order.getDate()).isNotNull().isEqualTo(now);
+ Assertions.assertThat(order.getTags()).isNotNull().isEqualTo(tags);
+ Assertions.assertThat(order.getUser()).isNotNull().isEqualTo(user1);
+ Assertions.assertThat(order.getProducts()).isNotEmpty();
+
+ System.out.println("order: " + JsonUtil.toString(order));
+
+ HBASE_TEMPLATE.delete(TABLE_NAME, originOrder.getRowKey());
+ order = HBASE_TEMPLATE.getEntity(TABLE_NAME, order.getRowKey(), "f1", Order.class);
+ Assertions.assertThat(order).isNull();
}
}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplatePutTest.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplatePutTest.java
deleted file mode 100644
index f7593036..00000000
--- a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplatePutTest.java
+++ /dev/null
@@ -1,105 +0,0 @@
-package io.github.dunwu.javadb.hbase;
-
-import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.collection.CollectionUtil;
-import org.apache.hadoop.hbase.client.Put;
-import org.junit.jupiter.api.DisplayName;
-import org.junit.jupiter.api.Test;
-
-import java.io.IOException;
-import java.math.BigDecimal;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Hbase Put 测试
- *
- * @author Zhang Peng
- * @date 2023-11-13
- */
-public class HbaseTemplatePutTest {
-
- public static final String TABLE_NAME = "test:test";
-
- private static final HbaseTemplate HBASE_TEMPLATE;
-
- static {
- try {
- HBASE_TEMPLATE = HbaseFactory.newHbaseTemplate();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- @DisplayName("put 测试 01")
- public void put01() throws IOException {
- long timestamp = System.currentTimeMillis();
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-0", "f1", "name", "user0");
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-1", timestamp, "f1", "name", "user1");
- }
-
- @Test
- @DisplayName("put 测试 02")
- public void put02() throws IOException {
- long timestamp = System.currentTimeMillis();
- User user2 = new User(2, "user2");
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-2", "f1", user2);
- User user3 = new User(3, "user3");
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-3", timestamp, "f1", user3);
- }
-
- @Test
- @DisplayName("put 测试 03")
- public void put03() throws IOException {
- long timestamp = System.currentTimeMillis();
- User user4 = new User(4, "user4");
- Map map = BeanUtil.beanToMap(user4);
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-4", timestamp, "f1", map);
- }
-
- @Test
- @DisplayName("put 测试 04")
- public void put04() throws IOException {
- long timestamp = System.currentTimeMillis();
- User user5 = new User(5, "user5");
- Product product5 = new Product("test-key-5", "product5", new BigDecimal(4000.0));
- Map> familyMap = new HashMap<>(2);
- Map userMap = BeanUtil.beanToMap(user5);
- familyMap.put("f1", userMap);
- Map productMap = BeanUtil.beanToMap(product5);
- familyMap.put("f2", productMap);
- HBASE_TEMPLATE.put(TABLE_NAME, "test-key-5", timestamp, familyMap);
- }
-
- @Test
- @DisplayName("put 测试 05")
- public void put05() throws IOException {
- Put put = HbaseTemplate.newPut("test-key-6", null, "f1", "name", "user6");
- HBASE_TEMPLATE.put(TABLE_NAME, put);
- }
-
- @Test
- @DisplayName("put 测试 06")
- public void put06() throws IOException, InterruptedException {
- long timestamp = System.currentTimeMillis();
- User user7 = new User(5, "user7");
- Product product7 = new Product("test-key-7", "product5", new BigDecimal(4000.0));
- Put put1 = HbaseTemplate.newPut("test-key-7", timestamp, "f1", user7);
- Put put2 = HbaseTemplate.newPut("test-key-7", timestamp, "f2", product7);
- List list = Arrays.asList(put1, put2);
- HBASE_TEMPLATE.batchPut(TABLE_NAME, list);
- }
-
- @Test
- @DisplayName("batchPut 测试2")
- public void batchPut2() throws IOException, InterruptedException {
- Product product1 = new Product("test-key-8", "product8", new BigDecimal(4000.0));
- Product product2 = new Product("test-key-9", "product9", new BigDecimal(5000.0));
- List products = CollectionUtil.newArrayList(product1, product2);
- HBASE_TEMPLATE.batchPut(TABLE_NAME, "f2", products);
- }
-
-}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateScanTest.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateScanTest.java
index e916b150..4496fc08 100644
--- a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateScanTest.java
+++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HbaseTemplateScanTest.java
@@ -1,32 +1,31 @@
package io.github.dunwu.javadb.hbase;
import cn.hutool.core.collection.CollectionUtil;
-import cn.hutool.core.util.ReflectUtil;
+import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
-import io.github.dunwu.javadb.hbase.entity.PageData;
-import io.github.dunwu.javadb.hbase.entity.RowDo;
-import io.github.dunwu.javadb.hbase.entity.ScrollData;
+import io.github.dunwu.javadb.hbase.entity.common.PageData;
+import io.github.dunwu.javadb.hbase.entity.common.RowDo;
+import io.github.dunwu.javadb.hbase.entity.common.ScrollData;
import io.github.dunwu.javadb.hbase.entity.scan.MultiFamilyScan;
import io.github.dunwu.javadb.hbase.entity.scan.SingleFamilyScan;
+import org.apache.hadoop.hbase.client.Put;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
-import java.lang.reflect.Field;
+import java.math.BigDecimal;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
/**
* Get 测试集
*
- * 测试前,先完整执行 {@link HbaseTemplatePutTest}
+ * 测试前,先完整执行 {@link HbaseTemplateGetTest}
*
* @author Zhang Peng
* @date 2023-11-13
@@ -45,103 +44,136 @@ public class HbaseTemplateScanTest {
}
}
+ @Test
+ @DisplayName("批量初始化")
+ public void init() throws IOException, InterruptedException {
+ List products = new ArrayList<>();
+ List userPuts = new ArrayList<>();
+ for (int i = 1; i <= 100; i++) {
+ Product product = new Product(String.valueOf(i), "product" + i,
+ new BigDecimal(RandomUtil.randomDouble(9999.0)));
+ products.add(product);
+
+ User user = new User(i, "user" + i);
+ Put put = HbaseTemplate.newPut(product.getRowKey(), null, "f2", user);
+ userPuts.add(put);
+ }
+ HBASE_TEMPLATE.batchPut(TABLE_NAME, "f1", products);
+ HBASE_TEMPLATE.batchPut(TABLE_NAME, userPuts);
+ }
+
@Test
@DisplayName("单列族分页查询")
- public void page() throws IOException {
- for (int page = 1; page <= 2; page++) {
- SingleFamilyScan scan = new SingleFamilyScan();
- scan.setFamily("f1")
- .setTableName(TABLE_NAME)
- .setPage(page)
- .setSize(2)
- .setReversed(true);
- PageData rowDoMap = HBASE_TEMPLATE.page(scan);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonStr(rowDoMap)));
- Assertions.assertThat(rowDoMap).isNotNull();
+ public void test01() throws IOException {
+ SingleFamilyScan scan = new SingleFamilyScan();
+ scan.setFamily("f1")
+ .setTableName(TABLE_NAME)
+ .setPage(1)
+ .setSize(10)
+ .setReversed(true);
+ PageData firstPage = HBASE_TEMPLATE.page(scan);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", 1, JSONUtil.toJsonStr(firstPage)));
+
+ int totalPages = firstPage.getTotalPages();
+ for (int page = 2; page <= totalPages; page++) {
+ scan.setPage(page);
+ PageData nextPage = HBASE_TEMPLATE.page(scan);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", page, JSONUtil.toJsonStr(nextPage)));
+ Assertions.assertThat(nextPage).isNotNull();
}
}
@Test
@DisplayName("多列族分页查询")
- public void page2() throws IOException {
+ public void test02() throws IOException {
Map> familyColumnMap = new HashMap<>();
- familyColumnMap.put("f1", Collections.singleton("id"));
- familyColumnMap.put("f2", Collections.singleton("name"));
- for (int page = 1; page <= 2; page++) {
- MultiFamilyScan scan = new MultiFamilyScan();
- scan.setFamilyColumnMap(familyColumnMap)
- .setTableName(TABLE_NAME)
- .setPage(page)
- .setSize(2)
- .setReversed(true);
- PageData rowDoMap = HBASE_TEMPLATE.page(scan);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonStr(rowDoMap)));
- Assertions.assertThat(rowDoMap).isNotNull();
+ familyColumnMap.put("f1", CollectionUtil.newArrayList("id", "name", "price"));
+ familyColumnMap.put("f2", CollectionUtil.newArrayList("id", "name"));
+
+ MultiFamilyScan scan = new MultiFamilyScan();
+ scan.setFamilyColumnMap(familyColumnMap)
+ .setTableName(TABLE_NAME)
+ .setPage(1)
+ .setSize(10)
+ .setReversed(true);
+ PageData firstPage = HBASE_TEMPLATE.page(scan);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", 1, JSONUtil.toJsonStr(firstPage)));
+
+ int totalPages = firstPage.getTotalPages();
+ for (int page = 1; page <= totalPages; page++) {
+ scan.setPage(page);
+ PageData nextPage = HBASE_TEMPLATE.page(scan);
+ System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonStr(nextPage)));
+ Assertions.assertThat(nextPage).isNotNull();
}
}
@Test
- @DisplayName("查询实体列表")
- public void getEntityPage() throws IOException {
+ @DisplayName("实体分页查询")
+ public void test03() throws IOException {
+
SingleFamilyScan scan = new SingleFamilyScan();
- scan.setFamily("f1")
+ scan.setFamily("f2")
.setTableName(TABLE_NAME)
.setPage(1)
- .setSize(2)
+ .setSize(10)
.setReversed(true);
- PageData entityPage = HBASE_TEMPLATE.getEntityPage(scan, User.class);
- System.out.println(StrUtil.format("查询实体列表: {}", JSONUtil.toJsonStr(entityPage)));
- Assertions.assertThat(entityPage).isNotNull();
+ PageData firstPage = HBASE_TEMPLATE.getEntityPage(scan, User.class);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", 1, JSONUtil.toJsonStr(firstPage)));
+
+ int totalPages = firstPage.getTotalPages();
+ for (int page = 2; page <= totalPages; page++) {
+ scan.setPage(page);
+ PageData nextPage = HBASE_TEMPLATE.getEntityPage(scan, User.class);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", page, JSONUtil.toJsonStr(nextPage)));
+ Assertions.assertThat(nextPage).isNotNull();
+ }
}
@Test
@DisplayName("单列族滚动查询")
- public void scroll() throws IOException {
+ public void test04() throws IOException {
SingleFamilyScan scan = new SingleFamilyScan();
scan.setFamily("f1")
.setTableName(TABLE_NAME)
- .setSize(1)
- .setStartRow("test-key-1")
- .setStopRow("test-key-9")
+ .setSize(10)
.setReversed(false);
- ScrollData data = HBASE_TEMPLATE.scroll(scan);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(data)));
- Assertions.assertThat(data).isNotNull();
- scan.setScrollRow(data.getScrollRow());
+
+ int page = 1;
+ ScrollData first = HBASE_TEMPLATE.scroll(scan);
+ System.out.println(StrUtil.format("第 {} 页数据: {}", page, JSONUtil.toJsonPrettyStr(first)));
+ Assertions.assertThat(first).isNotNull();
+ scan.setScrollRow(first.getScrollRow());
while (true) {
+ page++;
ScrollData next = HBASE_TEMPLATE.scroll(scan);
if (next == null || CollectionUtil.isEmpty(next.getContent())) {
break;
}
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(next)));
+ System.out.println(StrUtil.format("第 {} 页数据: {}", page, JSONUtil.toJsonPrettyStr(first)));
scan.setScrollRow(next.getScrollRow());
}
}
@Test
@DisplayName("多列族滚动查询")
- public void scroll2() throws IOException {
- List userFields = Stream.of(ReflectUtil.getFields(User.class))
- .map(Field::getName).collect(Collectors.toList());
- List productFields = Stream.of(ReflectUtil.getFields(Product.class))
- .map(Field::getName).collect(Collectors.toList());
+ public void test05() throws IOException {
Map> familyColumnMap = new HashMap<>();
- familyColumnMap.put("f1", userFields);
- familyColumnMap.put("f2", productFields);
+ familyColumnMap.put("f1", CollectionUtil.newArrayList("id", "name", "price"));
+ familyColumnMap.put("f2", CollectionUtil.newArrayList("id", "name"));
MultiFamilyScan scan = new MultiFamilyScan();
scan.setFamilyColumnMap(familyColumnMap)
.setTableName(TABLE_NAME)
- .setSize(1)
- .setStartRow("test-key-1")
- .setStopRow("test-key-9")
+ .setSize(10)
.setReversed(true);
- ScrollData data = HBASE_TEMPLATE.scroll(scan);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(data)));
- Assertions.assertThat(data).isNotNull();
- scan.setScrollRow(data.getScrollRow());
+
+ ScrollData first = HBASE_TEMPLATE.scroll(scan);
+ System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(first)));
+ Assertions.assertThat(first).isNotNull();
+ scan.setScrollRow(first.getScrollRow());
while (true) {
ScrollData next = HBASE_TEMPLATE.scroll(scan);
@@ -155,28 +187,55 @@ public void scroll2() throws IOException {
@Test
@DisplayName("滚动查询实体")
- public void getEntityScroll() throws IOException {
+ public void test06() throws IOException {
SingleFamilyScan scan = new SingleFamilyScan();
scan.setFamily("f1")
.setTableName(TABLE_NAME)
- .setSize(1)
- .setStartRow("test-key-1")
- .setStopRow("test-key-9")
+ .setSize(10)
.setReversed(false);
- ScrollData data = HBASE_TEMPLATE.getEntityScroll(scan, User.class);
- System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(data)));
- Assertions.assertThat(data).isNotNull();
- scan.setScrollRow(data.getScrollRow());
+ ScrollData first = HBASE_TEMPLATE.getEntityScroll(scan, Product.class);
+ System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(first)));
+ Assertions.assertThat(first).isNotNull();
+ scan.setScrollRow(first.getScrollRow());
while (true) {
- ScrollData next = HBASE_TEMPLATE.getEntityScroll(scan, User.class);
+ ScrollData next = HBASE_TEMPLATE.getEntityScroll(scan, Product.class);
+ if (next == null || CollectionUtil.isEmpty(next.getContent())) {
+ break;
+ }
+ System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(next)));
+ scan.setScrollRow(next.getScrollRow());
+ }
+ }
+
+ @Test
+ @DisplayName("滚动删除全部记录")
+ public void clear() throws IOException, InterruptedException {
+
+ SingleFamilyScan scan = new SingleFamilyScan();
+ scan.setFamily("f1")
+ .setTableName(TABLE_NAME)
+ .setSize(100)
+ .setReversed(false);
+
+ ScrollData first = HBASE_TEMPLATE.scroll(scan);
+ System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(first)));
+ Assertions.assertThat(first).isNotNull();
+ scan.setScrollRow(first.getScrollRow());
+ HBASE_TEMPLATE.batchDelete(TABLE_NAME,
+ first.getContent().stream().map(RowDo::getRow).distinct().toArray(String[]::new));
+
+ while (true) {
+ ScrollData next = HBASE_TEMPLATE.scroll(scan);
if (next == null || CollectionUtil.isEmpty(next.getContent())) {
break;
}
System.out.println(StrUtil.format("查询实体: {}", JSONUtil.toJsonPrettyStr(next)));
scan.setScrollRow(next.getScrollRow());
+ HBASE_TEMPLATE.batchDelete(TABLE_NAME,
+ next.getContent().stream().map(RowDo::getRow).distinct().toArray(String[]::new));
}
}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Order.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Order.java
new file mode 100644
index 00000000..53ab7787
--- /dev/null
+++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Order.java
@@ -0,0 +1,34 @@
+package io.github.dunwu.javadb.hbase;
+
+import io.github.dunwu.javadb.hbase.annotation.RowKeyRule;
+import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 较为复杂的 Java 实体
+ *
+ * @author Zhang Peng
+ * @date 2023-11-20
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@RowKeyRule(pk = "id", length = 20)
+public class Order implements BaseHbaseEntity {
+
+ private String id;
+ private User user;
+ private List products;
+ private String desc;
+ private Date date;
+ private Map tags;
+
+}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/ProductMapper.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/OrderMapper.java
similarity index 53%
rename from codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/ProductMapper.java
rename to codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/OrderMapper.java
index b95c1eca..f6639380 100644
--- a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/ProductMapper.java
+++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/OrderMapper.java
@@ -1,13 +1,15 @@
package io.github.dunwu.javadb.hbase;
+import io.github.dunwu.javadb.hbase.mapper.BaseHbaseMapper;
+
/**
* @author Zhang Peng
* @date 2023-11-15
*/
-public class ProductMapper extends BaseHbaseMapper {
+public class OrderMapper extends BaseHbaseMapper {
- public ProductMapper(HbaseTemplate hbaseTemplate, HbaseAdmin hbaseAdmin) {
- super(hbaseTemplate, hbaseAdmin);
+ public OrderMapper(HbaseTemplate hbaseTemplate) {
+ super(hbaseTemplate);
}
@Override
@@ -21,8 +23,8 @@ public String getFamily() {
}
@Override
- public Class getEntityClass() {
- return Product.class;
+ public Class getEntityClass() {
+ return Order.class;
}
}
diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Product.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Product.java
index ec15a1dd..f26ae56b 100644
--- a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Product.java
+++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/Product.java
@@ -4,25 +4,22 @@
import io.github.dunwu.javadb.hbase.entity.BaseHbaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
-import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
-
/**
* 产品实体
*
* @author Zhang Peng
* @date 2023-11-15
*/
-@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
-public class Product extends BaseHbaseEntity {
+@RowKeyRule(pk = "id", length = 10)
+public class Product implements BaseHbaseEntity {
- @RowKeyRule(length = 20)
private String id;
private String name;
private BigDecimal price;