yaml
hbase:zookeeper:quorum: 192.168.56.120property:clientPort: 2181
Maven
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.4.4</version></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,
@Value("${hbase.zookeeper.property.clientPort}") String clientPort) throws IOException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", zookeeperQuorum);conf.set("hbase.zookeeper.property.clientPort", clientPort);con = ConnectionFactory.createConnection(conf);
}
/**
- 创建表 *
- @param tableName 表名称
@param cf 列族 */ public static void createTable(String tableName, String[] cf) { TableName tb = TableName.valueOf(tableName); try {
admin = con.getAdmin();if (admin.tableExists(tb)) {System.out.println("talbe is exists!");} else {TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tb);List<ColumnFamilyDescriptor> list = new ArrayList<>();// 添加列族for (String entry : cf) {list.add(ColumnFamilyDescriptorBuilder.newBuilder(entry.getBytes(StandardCharsets.UTF_8)).build());}tableDescriptorBuilder.setColumnFamilies(list);admin.createTable(tableDescriptorBuilder.build());}
} catch (IOException e) {
e.printStackTrace();
} }
/**
查看已有表 */ public static TableName[] listTables() { TableName[] tableNames = new TableName[0]; try {
admin = con.getAdmin();tableNames = admin.listTableNames();
} catch (IOException e) {
e.printStackTrace();
} return tableNames; }
/**
删除表 */ public static void deleteTable(String tableName) { try {
TableName tn = TableName.valueOf(tableName);admin = con.getAdmin();if (admin.tableExists(tn)) {admin.disableTable(tn);admin.deleteTable(tn);}admin.close();
} catch (IOException e) {
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 {
Table table = con.getTable(TableName.valueOf(tableName));Put put = new Put(Bytes.toBytes(rowkey));put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));table.put(put);table.close();
} catch (IOException e) {
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
Table table = con.getTable(tb);Scan scan = new Scan();scanner = table.getScanner(scan);table.close();
} catch (IOException e) {
e.printStackTrace();
} List
for (Result result : scanner) {Map<String, Object> map = new HashMap<>(8);//展示数据for (Cell cell : result.rawCells()) {map.put("rowKey", Bytes.toString(CellUtil.cloneRow(cell)));map.put("family", Bytes.toString(CellUtil.cloneFamily(cell)));map.put("column", Bytes.toString(CellUtil.cloneQualifier(cell)));map.put("value", Bytes.toString(CellUtil.cloneValue(cell)));}list.add(map);}
} return list; }
/**
- 根据 rowkey 查询一条数据 *
- @param tableName 表
- @param rowKey rewkey
*/
public static Result getByRowkey(String tableName, String rowKey) {
try {
} catch (IOException e) {Table table = con.getTable(TableName.valueOf(tableName));Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));Result result = table.get(get);table.close();if (!result.isEmpty()) {return result;}
} return null; } /**e.printStackTrace();
- 根据 rowkey 查询一条数据 *
- @param tableName 表
- @param rowKey rewkey
- @param cf columnfamily
*/
public static Result getByRowkey(String tableName, String rowKey,String cf) {
try {
} catch (IOException e) {Table table = con.getTable(TableName.valueOf(tableName));Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));get.addFamily(cf.getBytes(StandardCharsets.UTF_8));Result result = table.get(get);table.close();if (!result.isEmpty()) {return result;}
} return null; } /**e.printStackTrace();
- 根据 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 {
Table table = con.getTable(TableName.valueOf(tableName));Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));get.addColumn(cf.getBytes(StandardCharsets.UTF_8),column.getBytes(StandardCharsets.UTF_8));Result result = table.get(get);table.close();if (!result.isEmpty()) {return result;}
} catch (IOException e) {
e.printStackTrace();
} return null; }
/**
- rowkey 模糊查询
- @param tableName 表
@param pattern 正则表达式 */ public static Iterator
getByRowkeyPattern(String tableName,String pattern){ try { Table table = con.getTable(TableName.valueOf(tableName));Scan scan = new Scan();RowFilter mykey = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(pattern));scan.setFilter(mykey);Iterator<Result> iterator = table.getScanner(scan).iterator();table.close();return iterator;
} catch (IOException e) {
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 { Table table = con.getTable(TableName.valueOf(tableName));List<Filter> list = new ArrayList<>();Scan scan = new Scan();RowFilter mykey1 = new RowFilter(CompareOperator.GREATER, new BinaryComparator(start.getBytes(StandardCharsets.UTF_8)));list.add(mykey1);RowFilter mykey2 = new RowFilter(CompareOperator.LESS, new BinaryComparator(end.getBytes(StandardCharsets.UTF_8)));list.add(mykey2);scan.setFilter(new FilterList(list));Iterator<Result> iterator = table.getScanner(scan).iterator();table.close();return iterator;
} catch (IOException e) {
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 { Table table = con.getTable(TableName.valueOf(tableName));Scan scan = new Scan();scan.setTimeRange(start,end);Iterator<Result> iterator = table.getScanner(scan).iterator();table.close();return iterator;
} catch (IOException e) {
e.printStackTrace();
} return null; }
/**
- 根据 rowkey删除一条记录
- @param tableName 表
@param rowKey rewkey */ public static void deleteByRowkey(String tableName, String rowKey){ try {
Table table = con.getTable(TableName.valueOf(tableName));Delete delete = new Delete(rowKey.getBytes(StandardCharsets.UTF_8));table.delete(delete);table.close();
} catch (IOException e) {
e.printStackTrace();
} }
/**
- 根据 rowkey 删除多条记录
- @param tableName 表
- @param rowKey rewkey
*/
public static void batchDeleteByRowkey(String tableName, String[] rowKey){
try {
} catch (IOException e) {Table table = con.getTable(TableName.valueOf(tableName));List<Delete> list = new ArrayList<>();for(String s : rowKey){Delete delete = new Delete(s.getBytes(StandardCharsets.UTF_8));list.add(delete);}table.delete(list);table.close();
} } }e.printStackTrace();
<a name="q4unF"></a>## Snapshot```bash## 创建快照,数据保存在 --> hdfs:xxx/hbase/.hbase-snapshot/ docker容器中,默认 /hbase-data/.hbase-snapshot/hbase> snapshot 'src_table', 'snapshot_src_table'## 查看快照hbase> list_snapshots## 删除快照hbase> delete_snapshot 'snapshot_src_table'## 数据迁移hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot -snapshot snapshot_src_table -copy-to hdfs://192.168.1.132:8020/hbase -mappers 16 -bandwidth 20## 数据恢复hbase> clone_snapshot 'snapshot_src_table' , 'new_table_name'
