Hbase 工具类


/**
 * 创建表、删除表、新增数据、查询数据(get、scan、scan filter)
 */
public class HBaseUtil {
    private static Connection connection;

    public static ArrayList<ArrayList<Map<String, String>>> scan(String tName, String startKey, String endKey, Filter filter) throws IOException {
        Table table = getConnection().getTable(TableName.valueOf(tName));
        Scan scan = new Scan();
        if (startKey != null && endKey != null) {
            scan.setStartRow(Bytes.toBytes(startKey));
            scan.setStopRow(Bytes.toBytes(endKey));
        }
        if (filter != null) {
            scan.setFilter(filter);
        }
        Result result = null;
        ResultScanner scanner = table.getScanner(scan);
        ArrayList<ArrayList<Map<String, String>>> arrayLists = new ArrayList<ArrayList<Map<String, String>>>();
        while ((result = scanner.next()) != null) {
            ArrayList<Map<String, String>> value = getValue(result);
            arrayLists.add(value);
        }
        return arrayLists;
    }

    /**
     * 查询方法
     * 封装了get方法
     *
     * @param tName  表名
     * @param rowkey 行键值
     * @param cf     列簇名
     * @param field  列名,字段
     */
    public static ArrayList<Map<String, String>> get(String tName, String rowkey, String cf, String field) throws IOException {

        // user,rowkey,cf:username:value
        Table table = getConnection().getTable(TableName.valueOf(tName));
        Get get = new Get(Bytes.toBytes(rowkey));
        if (cf != null) {
            get.addFamily(Bytes.toBytes(cf));
        }
        if (field != null) {
            get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(field));
        }
        Result result = table.get(get);
        return getValue( result);
    }

    private static ArrayList<Map<String, String>> getValue( Result result) {
        ArrayList<Map<String, String>> maps = new ArrayList<Map<String, String>>();
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("RowKey", Bytes.toString(CellUtil.cloneRow(cell)));
            hashMap.put("Family", Bytes.toString(CellUtil.cloneFamily(cell)));
            hashMap.put("Field", Bytes.toString(CellUtil.cloneQualifier(cell)));
            hashMap.put("Value", Bytes.toString(CellUtil.cloneValue(cell)));
            maps.add(hashMap);
        }
        return maps;
    }


    /**
     * 得到一个put方法
     * 这个方法不支持大量数据的传入
     *
     * @param tName  表名
     * @param rowkey 行键值
     * @param cf     列簇名
     * @param filed  字段名,列名
     * @param value  列的值
     * @throws IOException
     */
    public static void put(String tName, String rowkey, String cf, String filed, String value) throws IOException {
        Table table = getConnection().getTable(TableName.valueOf(tName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(filed), Bytes.toBytes(value));
        table.put(put);
        table.close();
    }

    /**
     * 该api提供的创建表的方式,必须是表不存在。
     * 如果表存在,就需要hbase的管理员去通过hbase shell命令去删除。
     *
     * @throws IOException
     */
    public static void createTable(String tName, String... familyNames) throws IOException {
        Admin admin = getConnection().getAdmin();
        TableName tableName = TableName.valueOf(tName);
        if (!admin.tableExists(tableName)) {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String familyName : familyNames) {
                hTableDescriptor.addFamily(new HColumnDescriptor(familyName));
            }
            admin.createTable(hTableDescriptor);
        }
        admin.close();
    }

    /**
     * 获取hbase的连接
     *
     * @return
     * @throws IOException
     */
    private static Connection getConnection() throws IOException {
        if (connection == null) {
            Configuration config = HBaseConfiguration.create();
            connection = ConnectionFactory.createConnection(config, Executors.newFixedThreadPool(30));
        }
        return connection;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值