HBase新版本Java API
之前沒有碼全,這次增刪改查全乎了,網(wǎng)上有很多例子,自己根據(jù)實際在用的收集總結了一下
- 導入的包
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
- 初始化連接對象
Admin admin=null;
Connection con=null;
try {
//1.獲得配置文件對象
Configuration conf=HBaseConfiguration.create();
//設置配置參數(shù)
conf.set("hbase.zookeeper.quorum", "192.168.52.140");
//2.建立連接
con=ConnectionFactory.createConnection(conf);
//3.獲得會話
admin=con.getAdmin();
- 創(chuàng)建表、刪除表
//創(chuàng)建表名對象
TableName tn=TableName.valueOf("stu");
//a.判斷數(shù)據(jù)庫是否存在
if(admin.tableExists(tn)){
System.out.println("====> 表存在,刪除表....");
//先使表設置為不可編輯,關閉表
admin.disableTable(tn);
//刪除表
admin.deleteTable(tn);
System.out.println("表刪除成功.....");
}
//創(chuàng)建表結構對象
HTableDescriptor htd=new HTableDescriptor(tn);
//創(chuàng)建列族結構對象
HColumnDescriptor hcd1=new HColumnDescriptor("fm1");
HColumnDescriptor hcd2=new HColumnDescriptor("fm2");
htd.addFamily(hcd1);
htd.addFamily(hcd2);
//創(chuàng)建表
admin.createTable(htd);
- 向表中單行put數(shù)據(jù)
//-------過時代碼------------
var hTable = new HTable(hbaseConf, TableName.valueOf(hbaseTable))
val put: Put = new Put(Bytes.toBytes(rowKey))
put.addColumn(Bytes.toBytes("列族"), Bytes.toBytes("列"), Bytes.toBytes("值"))
// 當你進行大量的Put的時候,要確認你的HTable的setAutoFlush是關閉著的。
// 否則的話,每執(zhí)行一個Put就要想?yún)^(qū)域服務器發(fā)一個請求。
// 否則的話,每執(zhí)行一個Put就要想?yún)^(qū)域服務器發(fā)一個請求。
// autoFlush = false,要等到寫緩沖都填滿的時候才會發(fā)起請求。
hTable.setAutoFlush(false, false)
hTable.setWriteBufferSize(30 * 1024 * 1024) //5M
//put可以放到List里批量提交
hTable.put(put)
hTable.flushCommits()
//---------------舊api會在將來版本刪除------------------
//單個插入
Put put =new Put(Bytes.toBytes("row01"));//參數(shù)是行健row01
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col1"), Bytes.toBytes("value01"));
//獲得表對象
Table table=con.getTable(tn);
table.put(put);
- 批量插入
Put put01 =new Put(Bytes.toBytes("row02"));//參數(shù)是行健
put01.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col2"), Bytes.toBytes("value02"))
.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col3"), Bytes.toBytes("value03"));
Put put02 =new Put(Bytes.toBytes("row03"));//參數(shù)是行健
put02.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col4"), Bytes.toBytes("value04"));
List<Put> puts=Arrays.asList(put01,put02);
//獲得表對象
Table table=con.getTable(tn);
table.put(puts);
- 還有種批量異步插入方式,效率更高
//第二種插入方式,也可批量插入
//設置緩存1m,當達到1m時數(shù)據(jù)會自動刷到hbase,替代了hTable.setWriteBufferSize(30 * 1024 * 1024)
val params = new BufferedMutatorParams(TableName.valueOf(hbaseTable)).writeBufferSize(1024 * 1024) //字節(jié)
//創(chuàng)建一個批量異步與hbase通信的對象
var mutatorTest: BufferedMutator = connection.getBufferedMutator(params)
val put: Put = new Put(Bytes.toBytes(rowKey))
put.addColumn(Bytes.toBytes("列族"), Bytes.toBytes("列"), Bytes.toBytes("值"))
//向hbase插入數(shù)據(jù),達到緩存會自動提交,這里也可以通過傳入List<put>的方式批量插入
mutatorTest.mutate(put)
//不用每次put后就調用flush,最后調用就行,這個方法替代了舊api的hTable.setAutoFlush(false, false)
mutator.flush()
mutator.close()
connection.close()
- scan讀取數(shù)據(jù)
Scan scan=new Scan();
//獲得表對象
Table table=con.getTable(tn);
//得到掃描的結果集
ResultScanner rs=table.getScanner(scan);
for(Result result:rs){
//得到單元格集合
List<Cell> cs=result.listCells();
for(Cell cell:cs){
//取行健
String rowKey=Bytes.toString(CellUtil.cloneRow(cell));
//取到時間戳
long timestamp = cell.getTimestamp();
//取到族列
String family = Bytes.toString(CellUtil.cloneFamily(cell));
//取到修飾名
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
//取到值
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(" ===> rowKey : " + rowKey + ", timestamp : " +
timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value);
}
}
- get讀取數(shù)據(jù)
Get get = new Get(Bytes.toBytes("row01"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col2"));
Table table = con.getTable(tn);
Result r = table.get(get);
List<Cell> cs = r.listCells();
for (Cell cell : cs) {
String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行鍵
long timestamp = cell.getTimestamp(); //取到時間戳
String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修飾名
String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值
System.out.println(" ===> rowKey : " + rowKey + ", timestamp : " +
timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value);
}
- 刪除數(shù)據(jù)
//刪除數(shù)據(jù)
Delete delete = new Delete(Bytes.toBytes("row02"));
delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("col2"));
Table table = con.getTable(tn);
table.delete(delete);
- 關閉
//5.關閉
if (admin != null){
admin.close();
}
if(con != null){
con.close();
}