HBase新版本Java API

HBase新版本Java API

之前沒有碼全,這次增刪改查全乎了,網(wǎng)上有很多例子,自己根據(jù)實際在用的收集總結了一下

  1. 導入的包
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;
  1. 初始化連接對象
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();
  1. 創(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);
  1. 向表中單行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);
  1. 批量插入
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()
  1. 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);
    }
}
  1. 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);
}
  1. 刪除數(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);
  1. 關閉
//5.關閉
if (admin != null){
    admin.close();
}
if(con != null){
    con.close();
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 最近在逐步跟進Hbase的相關工作,由于之前對Hbase并不怎么了解,因此系統(tǒng)地學習了下Hbase,為了加深對Hb...
    飛鴻無痕閱讀 50,575評論 19 272
  • 參考:http://www.itdecent.cn/p/569106a3008f 最近在逐步跟進Hbase的相關...
    博弈史密斯閱讀 927評論 1 1
  • Hbase的主要客戶端接口通過org.apache.hadoop.hbase.client包中的HTable類來實...
    baboq閱讀 1,005評論 0 1
  • 大喜大悲… 四個字概括今天一整天… 一些場景,看到了小時候的我… 原來,小時候的我,也是那樣的無助…
    S_O_S_O_爸爸閱讀 133評論 0 0
  • 一,昨天我是第一次聽到這樣的消息,讓我有點震驚,但也讓我慶幸我的弟弟也知道自己處於叛逆期,這還是説有得補救,關鍵是...
    小倖運5686閱讀 176評論 0 0

友情鏈接更多精彩內容