使用HBase Client访问阿里云NoSQL数据库表格存储
Apache HBase
- Apache HBase是Hadoop database,屬于Hadoop生態(tài)系統(tǒng)。
- 自從十四年前Google相繼發(fā)布論文:《The Google File System》、《MapReduce: Simplified Data Processing on Large Clusters》和《Bigtable: A Distributed Storage System for Structured Data》后,開源界開始模仿論文設(shè)計(jì)開源版本的這三個(gè)系統(tǒng),其中佼佼者就是Hadoop生態(tài),分別對(duì)應(yīng)于Hadoop,HDFS和HBase。
- 經(jīng)過十幾年業(yè)界大規(guī)模的使用和錘煉,目前Hadoop生態(tài)已經(jīng)成為一種事實(shí)上的業(yè)界規(guī)范,導(dǎo)致NoSQL的鼻祖Google的Bigtable都支持HBase wrapper,提供了Bigtable HBase client。
Tablestore
- Tablestore,中文名表格存儲(chǔ),是阿里云自主研發(fā)的NoSQL數(shù)據(jù)庫(kù),不同于HBase使用了Java,表格存儲(chǔ)和Bigtable一樣使用了C++語言來開發(fā)。
- 作為同類型的NoSQL數(shù)據(jù)庫(kù),HBase的大部分功能也同樣存在于表格存儲(chǔ)中,甚至大部分場(chǎng)景下性能更優(yōu),但是表格存儲(chǔ)還是不同于HBase,有部分高級(jí)功能,HBase并不擁有,這個(gè)后面單獨(dú)文章介紹。
HBase client
- HBase client是HBase提供的便于用戶訪問HBase的客戶端,支持讀、寫、掃描、批量、表管理等功能。
場(chǎng)景
Hadoop生態(tài)作為長(zhǎng)久以來唯一的開源大數(shù)據(jù)解決方案,被廣泛用于各個(gè)公司中。目前,大部分自建數(shù)據(jù)處理系統(tǒng)的公司使用了HBase等Hadoop生態(tài)的系統(tǒng)。當(dāng)這部分用戶想將數(shù)據(jù)遷移到阿里云的NoSQL數(shù)據(jù)庫(kù)表格存儲(chǔ)時(shí),之前都需要用戶重寫客戶端代碼才能使用表格存儲(chǔ),雖然這種方式性能更好,對(duì)后續(xù)的使用也更友好的,但是還是初期比較耗時(shí)間,為了解決這個(gè)問題,表格存儲(chǔ)年前也推出了TableStore HBase client。
如何使用
用戶如果之前使用HBase Client訪問HBase,現(xiàn)在只需要在項(xiàng)目中將對(duì)HBase Client的依賴 修改為對(duì)Tablestore HBase client的依賴,同時(shí)修改hbase-site.xml中的hbase.client.connection.impl值為com.alicloud.tablestore.hbase.TablestoreConnection即可。其他代碼都不需要任何改動(dòng)。
例子
代碼位置
當(dāng)前示例程序使用了HBase API訪問表格存儲(chǔ)服務(wù),完整的示例程序位于Github的Aliyun Tablestore HBase client for Java項(xiàng)目中,目錄位置是src/test/java/samples/HelloWorld.java。
配置項(xiàng)目依賴
Maven的依賴配置如下:
<dependencies><dependency><groupId>com.aliyun.openservices</groupId><artifactId>tablestore-hbase-client</artifactId><version>1.2.0</version></dependency></dependencies>配置文件
hbase-site.xml中增加下列配置項(xiàng):
<configuration><property><name>hbase.client.connection.impl</name><value>com.alicloud.tablestore.hbase.TablestoreConnection</value></property><property><name>tablestore.client.endpoint</name><value>endpoint</value></property><property><name>tablestore.client.instancename</name><value>instance_name</value></property><property><name>tablestore.client.accesskeyid</name><value>access_key_id</value></property><property><name>tablestore.client.accesskeysecret</name><value>access_key_secret</value></property><property><name>hbase.client.tablestore.family</name><value>f1</value></property><property><name>hbase.client.tablestore.table</name><value>ots_adaptor</value></property> </configuration>連接表格存儲(chǔ)
通過創(chuàng)建一個(gè)TableStoreConnection對(duì)象鏈接表格存儲(chǔ)服務(wù)。
Configuration config = HBaseConfiguration.create();// 創(chuàng)建一個(gè)Tablestore ConnectionConnection connection = ConnectionFactory.createConnection(config);// Admin 負(fù)責(zé)創(chuàng)建、管理、刪除等Admin admin = connection.getAdmin();創(chuàng)建表
通過指定表名創(chuàng)建一張表,MaxVersion和TimeToLive都是用默認(rèn)值。
// 創(chuàng)建一個(gè)HTableDescriptor,只有一個(gè)列族HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));// 創(chuàng)建一個(gè)列族,MaxVersion和TimeToLive使用默認(rèn)值,MaxVersion默認(rèn)值是1,TimeToLive默認(rèn)值是Integer.INF_MAX。descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));// 通過Admin的createTable接口創(chuàng)建表System.out.println("Create table " + descriptor.getNameAsString());admin.createTable(descriptor);寫數(shù)據(jù)
寫入一行數(shù)據(jù)到表格存儲(chǔ)。
// 創(chuàng)建一個(gè)TablestoreTable,用于單個(gè)表上的讀寫更新刪除等操作Table table = connection.getTable(TableName.valueOf(TABLE_NAME));// 創(chuàng)建一個(gè)Put對(duì)象,主鍵是row_1System.out.println("Write one row to the table");Put put = new Put(ROW_KEY);// 增加一列,表格存儲(chǔ)只支持單列族,列族名稱在hbase-site.xml中配置,如果沒有配置默認(rèn)是“f”,所以寫入數(shù)據(jù)時(shí)COLUMN_FAMILY_NAME可以是空值put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);// 執(zhí)行Table的put操作,使用Hbase API將這一行數(shù)據(jù)寫入表格存儲(chǔ)table.put(put);讀數(shù)據(jù)
讀取指定行的數(shù)據(jù)。
// 創(chuàng)建一個(gè)Get對(duì)象,讀取主鍵為ROW_KEY的行Result getResult = table.get(new Get(ROW_KEY));Result result = table.get(get);// 打印結(jié)果String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));System.out.println("Get one row by row key");System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);掃描數(shù)據(jù)
范圍讀取數(shù)據(jù)。
掃描全表所有行數(shù)據(jù)System.out.println("Scan for all rows:");Scan scan = new Scan();ResultScanner scanner = table.getScanner(scan);// 循環(huán)打印結(jié)果for (Result row : scanner) {byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);System.out.println('\t' + Bytes.toString(valueBytes));}刪表
使用Admin API刪除一張表。
print("Delete the table");admin.disableTable(table.getName());admin.deleteTable(table.getName());完結(jié)
按照上面的步驟就可以使用Tablestore HBase client了,目前,已經(jīng)有用戶開始使用,后續(xù),我們會(huì)根據(jù)用戶的反饋持續(xù)優(yōu)化Tablestore HBase client,使其性能能追趕到原生的表格存儲(chǔ)(Tablestore)。
雖然通過Tablestore HBase Client也可以訪問表格存儲(chǔ),但是還是建議用戶直接使用表格存儲(chǔ)的SDK訪問表格存儲(chǔ),這樣性能更好,功能更多,價(jià)格更便宜。表格存儲(chǔ)的官方網(wǎng)站:https://cn.aliyun.com/product/ots
總結(jié)
以上是生活随笔為你收集整理的使用HBase Client访问阿里云NoSQL数据库表格存储的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 源代码在线(http://l
- 下一篇: MySQL INSERT INTO...