• yaml

      1. hbase:
      2. zookeeper:
      3. quorum: 192.168.56.120
      4. property:
      5. clientPort: 2181
    • Maven

      1. <dependency>
      2. <groupId>org.apache.hbase</groupId>
      3. <artifactId>hbase-client</artifactId>
      4. <version>2.4.4</version>
      5. </dependency>
    • HbaseUtils ```java import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.; import org.apache.hadoop.hbase.client.; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.util.Bytes; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;

    import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.*;

    /**

    • Created on 2021/7/17—20:57.
    • hbase 工具类
    • @author fengyuhao
    • @Description */ @Component public class HbaseUtil { private static Admin admin = null; private static Connection con = null;

      private HbaseUtil(@Value(“${hbase.zookeeper.quorum}”) String zookeeperQuorum,

      1. @Value("${hbase.zookeeper.property.clientPort}") String clientPort) throws IOException {
      2. Configuration conf = HBaseConfiguration.create();
      3. conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
      4. conf.set("hbase.zookeeper.property.clientPort", clientPort);
      5. con = ConnectionFactory.createConnection(conf);

      }

      /**

      • 创建表 *
      • @param tableName 表名称
      • @param cf 列族 */ public static void createTable(String tableName, String[] cf) { TableName tb = TableName.valueOf(tableName); try {

        1. admin = con.getAdmin();
        2. if (admin.tableExists(tb)) {
        3. System.out.println("talbe is exists!");
        4. } else {
        5. TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tb);
        6. List<ColumnFamilyDescriptor> list = new ArrayList<>();
        7. // 添加列族
        8. for (String entry : cf) {
        9. list.add(ColumnFamilyDescriptorBuilder.newBuilder(entry.getBytes(StandardCharsets.UTF_8)).build());
        10. }
        11. tableDescriptorBuilder.setColumnFamilies(list);
        12. admin.createTable(tableDescriptorBuilder.build());
        13. }

        } catch (IOException e) {

        1. e.printStackTrace();

        } }

        /**

      • 查看已有表 */ public static TableName[] listTables() { TableName[] tableNames = new TableName[0]; try {

        1. admin = con.getAdmin();
        2. tableNames = admin.listTableNames();

        } catch (IOException e) {

        1. e.printStackTrace();

        } return tableNames; }

        /**

      • 删除表 */ public static void deleteTable(String tableName) { try {

        1. TableName tn = TableName.valueOf(tableName);
        2. admin = con.getAdmin();
        3. if (admin.tableExists(tn)) {
        4. admin.disableTable(tn);
        5. admin.deleteTable(tn);
        6. }
        7. admin.close();

        } catch (IOException e) {

        1. e.printStackTrace();

        } }

        /**

      • 添加(更新)一条记录 *
      • @param tableName 表名
      • @param rowkey rowkey
      • @param cf columnfamily
      • @param column column
      • @param value value */ public static void put(String tableName, String rowkey, String cf, String column, String value) { try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Put put = new Put(Bytes.toBytes(rowkey));
        3. put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));
        4. table.put(put);
        5. table.close();

        } catch (IOException e) {

        1. e.printStackTrace();

        } }

        /**

      • 批量添加(更新)数据 *
      • @param tableName 表名
      • @param data 插入的数据,
      • Map 结构 —>Map(rowkey,Map(cf:column,value)) */ public static void batchPut(String tableName, Map> data) { try { ObjectMapper objectMapper = new ObjectMapper(); Table table = con.getTable(TableName.valueOf(tableName)); List list = new ArrayList<>(); for (Map.Entry> entry : data.entrySet()) { System.out.println(entry.getKey()); // rowkey for (Map.Entry map : entry.getValue().entrySet()) { Put put = new Put(Bytes.toBytes(entry.getKey())); String[] keys = map.getKey().split(“:”); if (keys.length > 1) { put.addColumn(Bytes.toBytes(keys[0]), Bytes.toBytes(keys[1]), Bytes.toBytes(objectMapper.writeValueAsString(map.getValue()))); } else { put.addColumn(Bytes.toBytes(keys[0]), null, Bytes.toBytes(objectMapper.writeValueAsString(map.getValue()))); } list.add(put); } } table.put(list); table.close(); } catch (IOException e) { e.printStackTrace(); } }

        /**

      • 全表扫描 *
      • @param tableName 表名 */ public static List> scanTable(String tableName) { TableName tb = TableName.valueOf(tableName); ResultScanner scanner = null; try {

        1. Table table = con.getTable(tb);
        2. Scan scan = new Scan();
        3. scanner = table.getScanner(scan);
        4. table.close();

        } catch (IOException e) {

        1. e.printStackTrace();

        } List> list = new ArrayList<>(); if (scanner != null) {

        1. for (Result result : scanner) {
        2. Map<String, Object> map = new HashMap<>(8);
        3. //展示数据
        4. for (Cell cell : result.rawCells()) {
        5. map.put("rowKey", Bytes.toString(CellUtil.cloneRow(cell)));
        6. map.put("family", Bytes.toString(CellUtil.cloneFamily(cell)));
        7. map.put("column", Bytes.toString(CellUtil.cloneQualifier(cell)));
        8. map.put("value", Bytes.toString(CellUtil.cloneValue(cell)));
        9. }
        10. list.add(map);
        11. }

        } return list; }

        /**

      • 根据 rowkey 查询一条数据 *
      • @param tableName 表
      • @param rowKey rewkey */ public static Result getByRowkey(String tableName, String rowKey) { try {
        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));
        3. Result result = table.get(get);
        4. table.close();
        5. if (!result.isEmpty()) {
        6. return result;
        7. }
        } catch (IOException e) {
        1. e.printStackTrace();
        } return null; } /**
      • 根据 rowkey 查询一条数据 *
      • @param tableName 表
      • @param rowKey rewkey
      • @param cf columnfamily */ public static Result getByRowkey(String tableName, String rowKey,String cf) { try {
        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));
        3. get.addFamily(cf.getBytes(StandardCharsets.UTF_8));
        4. Result result = table.get(get);
        5. table.close();
        6. if (!result.isEmpty()) {
        7. return result;
        8. }
        } catch (IOException e) {
        1. e.printStackTrace();
        } return null; } /**
      • 根据 rowkey 查询一条数据 *
      • @param tableName 表
      • @param rowKey rewkey
      • @param cf columnfamily
      • @param column column */ public static Result getByRowkey(String tableName, String rowKey,String cf,String column) { try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));
        3. get.addColumn(cf.getBytes(StandardCharsets.UTF_8),column.getBytes(StandardCharsets.UTF_8));
        4. Result result = table.get(get);
        5. table.close();
        6. if (!result.isEmpty()) {
        7. return result;
        8. }

        } catch (IOException e) {

        1. e.printStackTrace();

        } return null; }

        /**

      • rowkey 模糊查询
      • @param tableName 表
      • @param pattern 正则表达式 */ public static Iterator getByRowkeyPattern(String tableName,String pattern){ try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Scan scan = new Scan();
        3. RowFilter mykey = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(pattern));
        4. scan.setFilter(mykey);
        5. Iterator<Result> iterator = table.getScanner(scan).iterator();
        6. table.close();
        7. return iterator;

        } catch (IOException e) {

        1. e.printStackTrace();

        } return null; }

        /**

      • rowkey 范围查询 ( start < result < end)
      • @param tableName 表
      • @param start 开始 key
      • @param end 结束 key */ public static Iterator getByRowkeyRange(String tableName,String start,String end){ try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. List<Filter> list = new ArrayList<>();
        3. Scan scan = new Scan();
        4. RowFilter mykey1 = new RowFilter(CompareOperator.GREATER, new BinaryComparator(start.getBytes(StandardCharsets.UTF_8)));
        5. list.add(mykey1);
        6. RowFilter mykey2 = new RowFilter(CompareOperator.LESS, new BinaryComparator(end.getBytes(StandardCharsets.UTF_8)));
        7. list.add(mykey2);
        8. scan.setFilter(new FilterList(list));
        9. Iterator<Result> iterator = table.getScanner(scan).iterator();
        10. table.close();
        11. return iterator;

        } catch (IOException e) {

        1. e.printStackTrace();

        } return null; }

        /**

      • 时间戳范围查询 ( start <= result < end)
      • @param tableName 表
      • @param start 开始 key
      • @param end 结束 key */ public static Iterator getByTimestamp(String tableName,Long start,Long end){ try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Scan scan = new Scan();
        3. scan.setTimeRange(start,end);
        4. Iterator<Result> iterator = table.getScanner(scan).iterator();
        5. table.close();
        6. return iterator;

        } catch (IOException e) {

        1. e.printStackTrace();

        } return null; }

        /**

      • 根据 rowkey删除一条记录
      • @param tableName 表
      • @param rowKey rewkey */ public static void deleteByRowkey(String tableName, String rowKey){ try {

        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. Delete delete = new Delete(rowKey.getBytes(StandardCharsets.UTF_8));
        3. table.delete(delete);
        4. table.close();

        } catch (IOException e) {

        1. e.printStackTrace();

        } }

        /**

      • 根据 rowkey 删除多条记录
      • @param tableName 表
      • @param rowKey rewkey */ public static void batchDeleteByRowkey(String tableName, String[] rowKey){ try {
        1. Table table = con.getTable(TableName.valueOf(tableName));
        2. List<Delete> list = new ArrayList<>();
        3. for(String s : rowKey){
        4. Delete delete = new Delete(s.getBytes(StandardCharsets.UTF_8));
        5. list.add(delete);
        6. }
        7. table.delete(list);
        8. table.close();
        } catch (IOException e) {
        1. e.printStackTrace();
        } } }
        1. <a name="q4unF"></a>
        2. ## Snapshot
        3. ```bash
        4. ## 创建快照,数据保存在 --> hdfs:xxx/hbase/.hbase-snapshot/ docker容器中,默认 /hbase-data/.hbase-snapshot/
        5. hbase> snapshot 'src_table', 'snapshot_src_table'
        6. ## 查看快照
        7. hbase> list_snapshots
        8. ## 删除快照
        9. hbase> delete_snapshot 'snapshot_src_table'
        10. ## 数据迁移
        11. hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot -snapshot snapshot_src_table -copy-to hdfs://192.168.1.132:8020/hbase -mappers 16 -bandwidth 20
        12. ## 数据恢复
        13. hbase> clone_snapshot 'snapshot_src_table' , 'new_table_name'