/**
-
该类主要以下几点功能:
-
连接复用
-
多线程操作
-
批量写入
-
WAL 关闭
-
指定范围扫描
-
过滤器扫描
*/
public class HBaseClient {
private static TableName TABLE_NAME = TableName.valueOf(“riemann”);
private static byte[] COL_FAMILY = Bytes.toBytes(“cf”);
private static byte[] QUALIFY_NAME = Bytes.toBytes(“name”);
private static byte[] QUALIFY_CLASS = Bytes.toBytes(“class”);
/**
-
HBase Table 不是线程安全类
-
在多线程的场合,不能多线程共享相同 Table 实例
-
@throws IOException
*/
public static void safePut() throws IOException {
Put riemann = new Put(Bytes.toBytes(“id_1”));
riemann.addColumn(COL_FAMILY, QUALIFY_NAME, Bytes.toBytes(“riemann”));
riemann.addColumn(COL_FAMILY, QUALIFY_CLASS, Bytes.toBytes(“classA”));
HBaseUtil.put(TABLE_NAME, riemann);
}
/**
-
批量写入,可以提高写入性能(减少 RPC)
-
HBase 客户端会自动打包并请求相应的 RegionServer
-
@throws IOException
-
@throws InterruptedException
*/
public static void batchPut() throws IOException, InterruptedException {
int batchSize = 10;
List batch = new ArrayList();
for (int i = 0; i < batchSize; i++) {
Put put = new Put(Bytes.toBytes(“id_” + i));
put.addColumn(COL_FAMILY, QUALIFY_NAME, Bytes.toBytes(“name_” + i));
put.addColumn(COL_FAMILY, QUALIFY_CLASS, Bytes.toBytes(“class_” + i));
batch.add(put);
}
HBaseUtil.put(TABLE_NAME, batch);
}
/**
-
WAL 作用是当 RegionServer 宕机是可以 replay 来恢复数据(即提高可靠性)
-
如果写入数据容忍丢失,那么可以选择关闭 WAL,写入性能可以提升 2~3 倍
-
@throws IOException
-
@throws InterruptedException
*/
public static void walOffPut() throws IOException, InterruptedException {
int batchSize = 10;
List batch = new ArrayList();
for (int i = 0; i < batchSize; i++) {
Put put = new Put(Bytes.toBytes(“id_” + i));
put.addColumn(COL_FAMILY, QUALIFY_NAME, Bytes.toBytes(“name_” + i));
put.addColumn(COL_FAMILY, QUALIFY_CLASS, Bytes.toBytes(“class_” + i));
//关闭 WAL,可以提升2~3倍的性能,但有数据丢失风险
put.setDurability(Durability.SKIP_WAL);
batch.add(put);
}
HBaseUtil.put(TABLE_NAME, batch);
}
/**
-
扫描时指定开始行和结束行,避免全表扫描
-
在表记录数非常多的情况下,效果非常明显
-
@throws IOException
*/
public static void rangeScan() throws IOException {
Scan scan = new Scan();
//指定扫描 [id_11,id_22) 之间的数据
byte[] startRow = Bytes.toBytes(“id_11”);
byte[] stopRow = Bytes.toBytes(“id_22”);
scan.setStartRow(startRow); //指定扫描开始行
最后
很多程序员,整天沉浸在业务代码的 CRUD 中,业务中没有大量数据做并发,缺少实战经验,对并发仅仅停留在了解,做不到精通,所以总是与大厂擦肩而过。
我把私藏的这套并发体系的笔记和思维脑图分享出来,理论知识与项目实战的结合,我觉得只要你肯花时间用心学完这些,一定可以快速掌握并发编程。
不管是查缺补漏还是深度学习都能有非常不错的成效,需要的话记得帮忙点个赞支持一下
整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~