HBase 清空表数据

    public int clearTableByTableName(String tableName) throws Exception {
        logger.debug("======InitHbaseServiceImpl clearTableByTableName=======");
        int count = 0;
        List<HBaseRow> rowsList = hbaseUtil.getRowsByStartAndEnd(tableName, null, null, HBaseConsts.FILTER_RECORD_COUNT);
        count += deleteData(tableName, rowsList, 0); //解决返回查找第一条为前一页最后一条数据
        while (rowsList.size() == HBaseConsts.FILTER_RECORD_COUNT) {
            String startKey = rowsList.get(rowsList.size() - 1).getRowKey();
            rowsList.clear();
            try {
                rowsList = hbaseUtil.getRowsByStartAndEnd(tableName, startKey, null, HBaseConsts.FILTER_RECORD_COUNT);
            } catch (Exception e) {
                logger.error("==========获取" + tableName + "数据异常==========");
            }
            count += deleteData(tableName, rowsList, 1);
            if (rowsList.size() < HBaseConsts.FILTER_RECORD_COUNT) {
                break;
            }
        }
        return count;
    }

    /**
     * 删除数据
     *
     * @param tableName
     * @param rowsList
     * @param startIdx
     * @throws Exception
     */
    private int deleteData(String tableName, List<HBaseRow> rowsList, int startIdx) throws Exception {
        logger.debug("=======deleteData rowsList:{}========", rowsList.size());
        int successCount = 0;
        for (int i = startIdx; i < rowsList.size(); i++) {
            HBaseRow baseRow = rowsList.get(i);
            String rowKey = baseRow.getRowKey();
            try {
                hbaseUtil.deleteRow(tableName, rowKey);
                successCount++;
            } catch (Exception e) {
                logger.error("=====清数据出现异常bug=====");
                throw new InvokeException(ResultEnum.FAILED.getCode(), "清数据出现异常bug");
            }

        }
        logger.debug("==========deleteData========本次删除成功数据:{}", successCount);
        return successCount;
    }

 

posted @ 2018-12-29 10:39  诸葛子房  阅读(2850)  评论(0编辑  收藏  举报