hbase訪問方式之java api
Hbase的訪問方式
1、Native Java API:最常規(guī)和高效的訪問方式;
2、HBase Shell:HBase的命令行工具,最簡單的接口,適合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技術(shù),支持C++,PHP,Python等多種語言,適合其他異構(gòu)系統(tǒng)在線訪問HBase表數(shù)據(jù);
4、REST Gateway:支持REST 風(fēng)格的Http API訪問HBase, 解除了語言限制;
5、MapReduce:直接使用MapReduce作業(yè)處理Hbase數(shù)據(jù);
6、使用Pig/hive處理Hbase數(shù)據(jù)。
常用Java API的用法:
1、加載配置
Configuration config = HBaseConfiguration.create();
//可以自定義配置,也可以從自定義配置文件中讀取
/*config.set("hbase.zookeeper.property.clientPort", "4181");
config.set("hbase.zookeeper.quorum", "hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com");
config.set("hbase.master", "hadoop.datanode3.com\\:600000");*/
2、表的創(chuàng)建、表信息修改、表刪除
HBaseAdmin admin = new HBaseAdmin(config);
//創(chuàng)建表
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor("cf1"));
htd.addFamily(new HColumnDescriptor("cf2"));
admin.createTable(htd);
//修改表信息
admin.disableTable(tableName);
// modifying existing ColumnFamily
admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
admin.enableTable(tableName);
//刪除表
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
3、添加記錄
/** 在多次使用時(shí),建議用HTablePool
HTable table = new HTable(config, tableName);
=>
HTablePool pool = new HTablePool(config, 1000);
HTableInterface table = pool.getTable(tableName);*/
HTable table = new HTable(config, tableName);
/**
* 在插入操作時(shí),默認(rèn)不適用任何緩存
* 可自定義使用緩存,以及緩存大小
* 每個(gè)任務(wù)最后需要手工調(diào)用 flushCommits();
*/
/*table.setAutoFlush(false);
table.setWriteBufferSize(1024);*/
Put put1 = new Put(Bytes.toBytes(rowKey));
if (ts == 0) {
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
} else {
//自定義版本時(shí),從自定義的版本號(hào),類型為long
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));
}
table.put(put1);
//table.flushCommits();
4、查詢,根據(jù)Rowkey查詢
Get get1 = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get1);
System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
Result[] result = table.get(List<Get>);//查詢指定Rowkey的多條記錄
5、查詢,指定條件和rowkey區(qū)間查詢
Scan scan = new Scan();
//默認(rèn)緩存大小為1,設(shè)置成一個(gè)合理的值,可以減少scan過程中next()的時(shí)間開銷,代價(jià)是客戶端的內(nèi)存
scan.setCaching(500);
scan.setCacheBlocks(false);
//根據(jù)startRowKey、endRowKey查詢
//Scan scan = new Scan(Bytes.toBytes("startRowKey"), Bytes.toBytes("endRowKey"));
//rowKey之外的過濾條件,在List中可以add;
/**List<Filter> filters = new ArrayList<Filter>();
Filter filter = new SingleColumnValueFilter("familyName".getBytes(),
"qualifierName".getBytes(),
CompareOp.EQUAL,
Bytes.toBytes("value"));
filters.add(filter);
scan.setFilter(new FilterList(filters));*/
ResultScanner scanner = table.getScanner(scan);
System.out.println("scan result list:");
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data1"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("data2"))));
}
scanner.close();
總結(jié)
以上所述是小編給大家介紹的hbase訪問方式之java api,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
最新springboot中必須要了解的自動(dòng)裝配原理
本文給大家介紹springboot中必須要了解的自動(dòng)裝配原理,spring-boot-dependencies:核心依賴都在父工程中,這個(gè)里面主要是管理項(xiàng)目的資源過濾及插件,本文對(duì)springboot自動(dòng)裝配原理給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-05-05
java語言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)
在本篇文章里小編給大家整理的是一篇關(guān)于java語言與平臺(tái)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2019-11-11
springboot讀取resource配置文件生成容器對(duì)象的示例代碼
這篇文章主要介紹了springboot讀取resource配置文件生成容器對(duì)象的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java List集合去重的多種實(shí)現(xiàn)方法
Java中List集合去重的多種方法,包括使用循環(huán)、HashSet、保持順序去重、contain方法去重等,注意在刪除元素時(shí),直接操作會(huì)導(dǎo)致ConcurrentModificationException,應(yīng)使用傳統(tǒng)for循環(huán)或倒序刪除2025-02-02
idea中打開項(xiàng)目時(shí)import project和open區(qū)別詳解
本文主要介紹了idea中打開項(xiàng)目時(shí)import project和open區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
解析rainbond以應(yīng)用為中心的架構(gòu)設(shè)計(jì)原理
這篇文章主要為大家介紹了rainbond以應(yīng)用為中心的架構(gòu)設(shè)計(jì)實(shí)現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02

