在基于HBase数据库的开发中,对应Java语言来说,可以直接使用HBase的原生API来操作HBase表数据,当然你要是不嫌麻烦可以使用Thrift客户端Java API,这里有我曾经使用过的 HBase Thrift客户端Java API实践,可以参考。对于具有其他编程语言背景的开发人员,为
在基于HBase数据库的开发中,对应Java语言来说,可以直接使用HBase的原生API来操作HBase表数据,当然你要是不嫌麻烦可以使用Thrift客户端Java API,这里有我曾经使用过的 HBase Thrift客户端Java API实践,可以参考。对于具有其他编程语言背景的开发人员,为了获取HBase带来的好处,那么就可以选择使用HBase Thrift客户端对应编程语言的API,来实现与HBase的交互。
这里,我们使用C#客户端来操作HBase。HBase的Thrift接口的定义,可以通过链接http://svn.apache.org/viewvc/hbase/trunk/hbase-server/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift?view=markup看到,我们需要安装Thrift编译器,才能生成HBase跨语言的API,这里,我使用的版本是0.9.0。需要注意的是,一定要保证,安装了某个版本Thrift的Thrift编译器,在导入对应语言库的时候,版本一定要统一,否则就会出现各种各样的问题,因为不同Thrift版本,对应编程语言的库API可能有变化。
首先,下载上面链接的内容,保存为Hbase.thrift。
然后,执行如下命令,生成C#编程语言的HBase Thrift客户端API:
[hadoop@master hbase]$ thrift --gen csharp Hbase.thrift[hadoop@master hbase]$ lsgen-csharp
这里,我们基于C#语言,使用HBase 的Thrift 客户端API访问HBase表。事实上,如果使用Java来实现对HBase表的操作,最好是使用HBase的原生API,无论从性能还是便利性方面,都会提供更好的体验。使用Thrift API访问,实际也是在HBase API之上进行了一层封装,可能初次使用Thrift API感觉很别扭,有时候还要参考Thrift服务端的实现代码。
准备工作如下:
- 下载Thrift软件包,解压缩后,拷贝thrift-0.9.0/lib/java/src下面的代码到工作区(开发工具中)
- 将上面生成的gen-csharp目录中代码拷贝到工作区
- 保证HBase集群正常运行,接着启动HBase的Thrift服务,执行如下命令:
bin/hbase thrift -b master -p 9090 start
上面,HBase的Thrift服务端口为9090,下面通过Thrift API访问的时候,需要用到,而不是HBase的服务端口(默认60000)。
接着,实现一个简单的例子,访问Hbase表。
首先,我们通过HBase Shell创建一个表:
create 'test_info', 'info'
表名为test_info,列簇名称为info。
然后,我们开始基于上面生成的Thrift代码来实现对HBase表的操作。
这里,我们实际上是对HBase Thrift客户端Java API实践中的Java代码进行了翻译,改写成C#语言的相关操作。我们在客户端,进行了一层抽象,更加便于传递各种参数,抽象类为AbstractHBaseThriftService,对应的命名空间为HbaseThrift.HBase.Thrift,该类实现代码如下所示:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Thrift.Transport;using Thrift.Protocol;namespace HbaseThrift.HBase.Thrift{ public abstract class AbstractHBaseThriftService { protected static readonly string CHARSET = "UTF-8"; private string host = "localhost"; private int port = 9090; private readonly TTransport transport; protected readonly Hbase.Client client; public AbstractHBaseThriftService() : this("localhost", 9090) { } public AbstractHBaseThriftService(string host, int port) { this.host = host; this.port = port; transport = new TSocket(host, port); TProtocol protocol = new TBinaryProtocol(transport, true, true); client = new Hbase.Client(protocol); } public void Open() { if (transport != null) { transport.Open(); } } public void Close() { if (transport != null) { transport.Close(); } } public abstract List GetTables(); public abstract void Update(string table, string rowKey, bool writeToWal, string fieldName, string fieldValue, Dictionary attributes); public abstract void Update(string table, string rowKey, bool writeToWal, Dictionary fieldNameValues,<p>本文来源gao!%daima.com搞$代*!码$网9</p> Dictionary attributes); public abstract void DeleteCell(string table, string rowKey, bool writeToWal, string column, Dictionary attributes); public abstract void DeleteCells(string table, string rowKey, bool writeToWal, List columns, Dictionary attributes); public abstract void DeleteRow(string table, string rowKey, Dictionary attributes); public abstract int ScannerOpen(string table, string startRow, List columns, Dictionary attributes); public abstract int ScannerOpen(string table, string startRow, string stopRow, List columns, Dictionary attributes); public abstract int ScannerOpenWithPrefix(string table, string startAndPrefix, List columns, Dictionary attributes); public abstract int ScannerOpenTs(string table, string startRow, List columns, long timestamp, Dictionary attributes); public abstract int ScannerOpenTs(string table, string startRow, string stopRow, List columns, long timestamp, Dictionary attributes); public abstract List ScannerGetList(int id, int nbRows); public abstract List ScannerGet(int id); public abstract List GetRow(string table, string row, Dictionary attributes); public abstract List GetRows(string table, List rows, Dictionary attributes); public abstract List GetRowsWithColumns(string table, List rows, List columns, Dictionary attributes); public abstract void ScannerClose(int id); /** * Iterate result rows(just for test purpose) * @param result */ public abstract void IterateResults(TRowResult result); }}