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