这是对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() { } }