【工具类】系列三 HBase访问工具类 HBaseUtil

这是对HBase访问的简单封装,主要是Spark executor上使用,就没有注意多线程安全了。若有需要自己优化。

 

直接贴代码:

/**
 * HBase Utility class, HBase Design document see: <br/>
 * Attention: put/get/del is not thread safe due to use HBase Table interface.
 *
 * @author adore.chen
 * @date 2020-04-30
 */
@Slf4j
public class HBaseUtil {

    private static final Configuration hbaseConf = HBaseConfiguration.create();

    static {
        hbaseConf.set(ZOOKEEPER_QUORUM, Config.getString(ZOOKEEPER_QUORUM));
        hbaseConf.set(ZOOKEEPER_CLIENT_PORT, Config.getString(ZOOKEEPER_CLIENT_PORT));
    }

    /**
     * lazy singleton pattern for HBase Connection or Table interface.
     */
    private static class Helper {

        static Connection CONNECT;

        static {
            try {
                // init connection
                CONNECT = ConnectionFactory.createConnection(hbaseConf);
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    public void run() {
                        // close connection
                        if (CONNECT != null && !CONNECT.isClosed()) {
                            try {
                                CONNECT.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            } catch (IOException e) {
                log.error("create HBase connection error", e);
            }
        }
    }

    /**
     * return map-reduce job configuration for HBase.
     * @return
     * @throws IOException
     */
    public static Configuration getJobConfig(String tableName) throws IOException {

        Job job = Job.getInstance(hbaseConf);
        job.setInputFormatClass(TableInputFormat.class);
        job.setOutputFormatClass(TableOutputFormat.class);
        job.setOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(Put.class);

        Configuration conf = job.getConfiguration();
        conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);
        conf.set(OUTPUT_DIR, "/tmp");

        return conf;
    }

    /**
     * put all Put objects into HBase.
     * @param htable
     * @param puts
     */
    public static void put(HTable htable, List<Put> puts) {
        try (Table table = getTable(htable)) {
            table.put(puts);
        } catch (IOException e) {
            log.error("put data into HBase error", e);
        }
    }

    /**
     * do Del objects from HBase.
     * @param htable
     * @param del
     */
    public static void del(HTable htable, Delete del) {
        try (Table table = getTable(htable)) {
            table.delete(del);
        } catch (IOException e) {
            log.error("delete data from HBase error", e);
        }
    }

    /**
     * Get from HBase.
     * @param htable
     * @param get
     * @return
     */
    public static Result[] get(HTable htable, Get get) {
        return get(htable, Arrays.asList(get));
    }

    /**
     * get a list of value from HBase.
     *
     * @param htable
     * @param gets
     * @return
     */
    public static Result[] get(HTable htable, List<Get> gets) {

        try (Table table = getTable(htable)) {
            return table.get(gets);
        } catch (IOException e) {
            log.error("get data from HBase error", e);
        }

        return new Result[0];
    }

    private static Table getTable(HTable htable) throws IOException {
        TableName tableName = TableName.valueOf(htable.getTableName());
        return Helper.CONNECT.getTable(tableName);
    }

    private HBaseUtil() {

    }

}

 

CellUtil 方便测试使用,生产使用时注意性能。

/**
 * HBase Cell Util.
 *
 * @author adore.chen
 * @date 2020-05-01
 */
public class CellUtil {

    public static String getRowKey(Cell cell) {
        return Bytes.toStringBinary(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
    }

    public static String getColumnFamily(Cell cell) {
        return Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
    }

    public static String getQualifier(Cell cell) {
        return Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
    }

    /**
     * get cell value according to input class.
     * @param cell
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getValue(Cell cell, Class<T> clazz) {
        Object value;

        if (clazz == String.class) {
            value = Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        } else if (clazz == int.class || clazz == Integer.class) {
            value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        } else if (clazz == long.class || clazz == Long.class) {
            value = Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        } else {
           throw new IllegalArgumentException("Unsupported class type " + clazz.getSimpleName());
        }

        return (T) value;
    }

    public static <T> void displayResult(Result result, Class<T> valueClas) throws IOException {
        CellScanner scanner = result.cellScanner();
        while (scanner.advance())   {
            Cell cell = scanner.current();
            String rk = CellUtil.getRowKey(cell);
            String cf = CellUtil.getColumnFamily(cell);
            String column = CellUtil.getQualifier(cell);
            T value = CellUtil.getValue(cell, valueClas);
            long timestamp = cell.getTimestamp();
            System.out.printf("rowkey => %s, columnFamily => %s, column => %s, "
                    + "value => %s, timestamp => %d\n", rk, cf, column, value, timestamp);
        }
    }

    private CellUtil() {

    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值