+ * HBaseAdminHelper 是针对 {@link Admin} 常用 API 的封装工具类 + */ +public class HBaseAdminHelperTests { + + private static HBaseAdminHelper hbaseAdminHelper = null; + + @BeforeAll + public static void init() throws IOException { + Configuration conf = HBaseConfiguration.create(); + conf.set("hbase.zookeeper.quorum", "localhost"); + conf.set("hbase.zookeeper.port", "2181"); + hbaseAdminHelper = HBaseAdminHelper.newInstance(conf); + } + + @AfterAll + public static void destroy() { + IoUtil.close(hbaseAdminHelper); + } + + @Test + @DisplayName("创建、删除、查看命名空间") + public void testNamespace() throws IOException { + // 创建命名空间 + hbaseAdminHelper.createNamespace("temp"); + dumpNamespaces(); + // 删除命名空间 + hbaseAdminHelper.dropNamespace("temp", true); + dumpNamespaces(); + } + + private void dumpNamespaces() throws IOException { + String[] namespaces = hbaseAdminHelper.listNamespaces(); + System.out.println("命名空间:"); + if (ArrayUtil.isNotEmpty(namespaces)) { + Stream.of(namespaces).forEach(System.out::println); + } + } + + @Test + @DisplayName("创建、删除、启用、禁用查看表") + public void testTable() throws IOException { + // 创建命名空间 + hbaseAdminHelper.createNamespace("temp"); + // 创建名为 test 的表,并含有两个列族 d 和 b + hbaseAdminHelper.createTable(TableName.valueOf("temp:test"), "d", "b"); + // 查看表 + dumpTablesInNamespace("temp"); + // 禁用表 + hbaseAdminHelper.disableTable(TableName.valueOf("temp:test")); + // 启用表 + hbaseAdminHelper.enableTable(TableName.valueOf("temp:test")); + // 删除表 + hbaseAdminHelper.dropTable(TableName.valueOf("temp:test")); + // 查看表 + dumpTablesInNamespace("temp"); + // 删除命名空间 + hbaseAdminHelper.dropNamespace("temp", true); + } + + private void dumpTablesInNamespace(String namespace) throws IOException { + TableName[] tableNames = hbaseAdminHelper.listTableNamesByNamespace(namespace); + System.out.println("表:"); + if (ArrayUtil.isNotEmpty(tableNames)) { + Stream.of(tableNames).forEach(System.out::println); + } + } + +} diff --git a/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HBaseHelperTests.java b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HBaseHelperTests.java new file mode 100644 index 0000000..c9f4afe --- /dev/null +++ b/codes/javadb/hbase/src/test/java/io/github/dunwu/javadb/hbase/HBaseHelperTests.java @@ -0,0 +1,129 @@ +package io.github.dunwu.javadb.hbase; + +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.IdUtil; +import cn.hutool.json.JSONUtil; +import io.github.dunwu.javadb.hbase.entity.HBaseFamilyRequest; +import io.github.dunwu.javadb.hbase.entity.HBaseMultiFamilyRequest; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Table; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * HBase {@link Table} API 测试例 + *
+ * {@link HBaseHelper} 是针对 {@link Table} 常用 API 的封装工具类
+ */
+public class HBaseHelperTests {
+
+ private static HBaseHelper hbaseHelper = null;
+ private static HBaseAdminHelper hbaseAdminHelper = null;
+
+ public static final String TABLE_NAME = "test:log";
+
+ @BeforeAll
+ public static void init() throws IOException {
+
+ Configuration conf = HBaseConfiguration.create();
+ conf.set("hbase.zookeeper.quorum", "localhost");
+ conf.set("hbase.zookeeper.port", "2181");
+ hbaseHelper = HBaseHelper.newInstance(conf);
+ hbaseAdminHelper = HBaseAdminHelper.newInstance(conf);
+
+ // 创建命名空间
+ hbaseAdminHelper.createNamespace("test");
+ // 创建名为 test 的表,并含有两个列族 d 和 b
+ hbaseAdminHelper.createTable(TableName.valueOf(TABLE_NAME), "d", "b");
+ }
+
+ @AfterAll
+ public static void destroy() throws IOException {
+ hbaseAdminHelper.dropTable(TableName.valueOf(TABLE_NAME));
+ hbaseAdminHelper.dropNamespace("test", true);
+ IoUtil.close(hbaseAdminHelper);
+ IoUtil.close(hbaseHelper);
+ }
+
+ @Test
+ public void test() throws Exception {
+ String uuid = IdUtil.fastUUID();
+ CommonInfo commonInfo = new CommonInfo(this.getClass().getCanonicalName(), "test", 100);
+ LogInfo logInfo = new LogInfo("INFO", "hello world");
+ hbaseHelper.put(TableName.valueOf(TABLE_NAME), uuid, "d", System.currentTimeMillis(), commonInfo);
+ hbaseHelper.put(TableName.valueOf(TABLE_NAME), uuid, "b", System.currentTimeMillis(), logInfo);
+
+ // 查单列的值
+ String msg = hbaseHelper.getColumn(TableName.valueOf(TABLE_NAME), uuid, "b", "msg");
+ String level = hbaseHelper.getColumn(TableName.valueOf(TABLE_NAME), uuid, "b", "level");
+ Assertions.assertThat(msg).isEqualTo("hello world");
+ Assertions.assertThat(level).isEqualTo("INFO");
+ String className = hbaseHelper.getColumn(TableName.valueOf(TABLE_NAME), uuid, "d", "className");
+ String methodName = hbaseHelper.getColumn(TableName.valueOf(TABLE_NAME), uuid, "d", "methodName");
+ String lineNum = hbaseHelper.getColumn(TableName.valueOf(TABLE_NAME), uuid, "d", "lineNum");
+ Assertions.assertThat(className).isEqualTo(this.getClass().getCanonicalName());
+ Assertions.assertThat(methodName).isEqualTo("test");
+ Assertions.assertThat(lineNum).isEqualTo("100");
+
+ // 查单列族数据
+ Map