Hbase Api常规操作记录
这个是之前整理出来做到一系列接口
本文主要是在增删改查到基础上,记录原子操作(checkAndPut)以及batch操作(混合put/get/delete)到使用
废话不多说直接上代码
package com.bigdata.hbase.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HBaseAPITest {
private static Connection connection;
public static void main(String[] args) throws IOException, InterruptedException {
Table table = getTable("school:stu_info");
// OnePut(table);
// BatchPut(table);
// batchPutList(table);
// atomOperate(table);
// getData(table);
// getBYFamily(table);
// getBatchData(table);
// deleteData(table);
// scanTable(table);
multiBatch(table);
table.close();
}
// 创建table的实例对象
private static Table getTable(String tablename) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorm", "hadoop01"); // zookeeper 用户需要client来访问到,应为zookeeper可以保留region信息
connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tablename));
return table;
}
/**
*
*/
private static void OnePut(Table table) throws IOException {
// put 数据
Put put = new Put(Bytes.toBytes("1002"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xiaozhang"));
table.put(put); // RPC操作 100万次就建立100万rpc请求,不要这样操作
}
/**
* 缓冲区批量插入数据
*/
private static void BatchPut(Table table) throws IOException {
// put 数据
Put put = new Put(Bytes.toBytes("1003"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xiaoli"));
// 旧版本table.setAutoFlush(false) 写缓存开启,table.isAutoFlush 查看状态_info
// Table.setWriteBufferSize
BufferedMutatorParams bufferedMutatorParams = new BufferedMutatorParams(TableName.valueOf("school:stu"));
bufferedMutatorParams.writeBufferSize(2048); // 设置为2M
BufferedMutator bufferedMutator = connection.getBufferedMutator(TableName.valueOf("school:stu_info"));
long writeBufferSize = bufferedMutator.getWriteBufferSize();
System.out.println(writeBufferSize);
bufferedMutator.mutate(put);
// 需要强制刷新
bufferedMutator.flush();
}
/**
* puts方式
* @param table
* @throws IOException
*/
private static void batchPutList(Table table) throws IOException {
List<Put> puts = new ArrayList<Put>();
Put put = new Put(Bytes.toBytes("1005"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xiaoli5"));
Put put2 = new Put(Bytes.toBytes("1006"));
put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xiaoli6"));
puts.add(put);
puts.add(put2);
table.put(puts);
}
/**
* 原子操作,事务处理
*/
private static void atomOperate(Table table) throws IOException {
Put put = new Put(Bytes.toBytes("1007"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("112"));
// 检查写操作 先锁一个, 前面三个不存在到时候才会被提交
boolean b = table.checkAndPut(Bytes.toBytes("1007"), Bytes.toBytes("info"), Bytes.toBytes("name")
, null, put);
// value 是11 的时候才会被提交 ,也就是update数据为112
boolean b2 = table.checkAndPut(Bytes.toBytes("1007"), Bytes.toBytes("info"), Bytes.toBytes("name")
, Bytes.toBytes("11"), put);
System.out.println(b);
}
/**
* 根据列族和列查询数据
* @param table
* @throws IOException
*/
private static void getData(Table table) throws IOException {
Get get = new Get(Bytes.toBytes("1002"));
get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
long timestamp = cell.getTimestamp();
System.out.println("rowkey: " + rowkey + "; family: " + family + "; qualifier :" + qualifier + "; version: " + timestamp + "; value:" + value);
}
};
/**
* 根据列族查询数据
*/
private static void getBYFamily(Table table) throws IOException {
Get get = new Get(Bytes.toBytes("1002"));
get.addFamily(Bytes.toBytes("info"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
long timestamp = cell.getTimestamp();
System.out.println("rowkey: " + rowkey + "; family: " + family + "; qualifier :" + qualifier + "; version: " + timestamp + "; value:" + value);
}
}
/**
* 使用get批量查询数据
*/
private static void getBatchData(Table table) throws IOException {
List<Get> gets = new ArrayList<Get>();
Get get1 = new Get(Bytes.toBytes("1002"));
get1.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
Get get2 = new Get(Bytes.toBytes("1003"));
get2.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
gets.add(get1);
gets.add(get2);
Result[] results = table.get(gets);
for (Result result : results) {
for (Cell cell : result.rawCells()) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
long timestamp = cell.getTimestamp();
System.out.println("rowkey: " + rowkey + "; family: " + family + "; qualifier :" + qualifier + "; version: " + timestamp + "; value:" + value);
}
}
}
/**
* delete 用法演示
*
*/
private static void deleteData(Table table) throws IOException {
Delete delete = new Delete(Bytes.toBytes("1003"));
// 删除最新版本到数据
// delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
// 删除所有版本
// delete.addColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));
// 指定版本
// delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),1560787574568l);
// 范围删除
table.delete(delete);
// 批量删除 list
}
private static void scanTable(Table table) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
long timestamp = cell.getTimestamp();
System.out.println("rowkey: " + rowkey + "; family: " + family + "; qualifier :" + qualifier + "; version: " + timestamp + "; value:" + value);
}
}
}
/**
* 混合批处理(put delete get)
*/
public static void multiBatch(Table table) throws IOException, InterruptedException {
List<Row> list = new ArrayList<Row>();
Put put = new Put(Bytes.toBytes("1008"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("112"));
Get get2 = new Get(Bytes.toBytes("1006"));
get2.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
Delete delete = new Delete(Bytes.toBytes("1005"));
delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
list.add(put);
list.add(get2);
list.add(delete);
Object[] objects = new Object[list.size()];
table.batch(list, objects);
for (Object object : objects) {
System.out.println((Result)object);
}
}
}