栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

使用HBaseTestingUtility进行本地单元测试

使用HBaseTestingUtility进行本地单元测试

文章目录
    • 1:HbaseTestingUtility简介
      • 1.1:maven依赖
      • 1.2:注意事项
    • 2:开发demo
      • 1.1:启动本地客户端
      • 1.2:创建表测试demo

hbase单元测试参考

1:HbaseTestingUtility简介

在调试MR任务或者操作Hbase表时,往往我们需要将本地代码打成Jar包,然后上传到Hadoop集群上去跑,这样不仅麻烦,还不方便调试,Hadoop开发团队提供了在本地调试代码的API,就是MiniHbaseCluster, 在本机JVM中模拟一个Hadoop集群,与真实环境的Hadoop集群并没有区别,方便我们提交任务和Debug。
HbaseTestingUtility类;这个类提供了启动、停止、创建、配置等实用方法来管理Mini Cluster;

1.1:maven依赖

还要加入其它hbase依赖查看,对于需要junit等单元测试,自行引入依赖即可


    
      org.apache.hbase
      hbase-testing-util
      2.2.2
    
1.2:注意事项

1:不支持对列簇设置压缩级别,表关闭WAL,设置块大小等参数配置。

 表: TableDescriptorBuilder.setDurability(Durability.SKIP_WAL);   //关闭wal预写日志不支持,会报错
 列簇: cf.setMaxVersions(1);     //不支持设置版本,块大小等
2:开发demo 1.1:启动本地客户端
  	private static Connection connection = null;

    private static HbaseTestingUtility hbaseTesting = null;

    
    private static Map adminMap = new ConcurrentHashMap();

    static {
        hbaseTesting = new HbaseTestingUtility();
        try {
            //启动本地微型客户端
            hbaseTesting.startMiniCluster();
            if (connection == null) {
                connection = hbaseTesting.getConnection();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeConnextion() {
        try {
            if (connection != null) {
                connection.close();
            }
            if (hbaseTesting != null) {
                hbaseTesting.shutdownMiniCluster();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
1.2:创建表测试demo
 
    @Before
    public void createTableTest() throws IOException {

        ArrayList list = new ArrayList<>();
        list.add(columnFamily);
        hbaseLocalTestingUtil.createTable(tableName, list);
        boolean b = hbaseLocalTestingUtil.exitsTable(tableName);
        System.out.println(" table test1 status==" + b);
        //断言判断,结果为true表示创建成功
        Assert.assertTrue(b);
        System.out.println("test create table success !");
    }
 public static void createTable(String tableName, List list)
            throws IOException {
        Admin admin = hbaseLocalTestingUtil.getHbaseAdmin();

        if (hbaseLocalTestingUtil.exitsTable(tableName)) {
            System.out.println("table already existed");
            return;
        }
        TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));

        List colFamilyList = new ArrayList<>();
        for (String columnFamilys : list) {
            ColumnFamilyDescriptorBuilder c = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamilys));
            colFamilyList.add(c.build());
        }
        tableDesc.setColumnFamilies(colFamilyList);

        //创建表方式1:直接创建
        admin.createTable(tableDesc.build());
        //创建表方式2:预分区创建
        //  admin.createTable(tableDesc.build(), new RegionSplitter.HexStringSplit().split(splitNum));
        LOG.info(tableName + " is created");
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/457806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号