准備工作:
下載 Thrift compiler for Windows版本的EXE文件(thrift-0.7.0.exe)
下載 SVN源碼地址:http://svn.apache.org/repos/asf/thrift/attic/trunk/lib/csharp/ (使用svn來下載)
1.將 hbase-0.94.4\src\main\resources\org\apache\hadoop\hbase\thrift2\hbase.thrift 到 thrift-0.7.0.exe 相同目錄下
進入命令行,到 thrift-0.7.0.exe 目錄下
執行 thrift-0.7.0.exe -gen csharp hbase.thrift
會生成gen-csharp文件夾,里面是thrift接口實現的類
2使用svn獲取源碼(主要為了生成Thrift.dll,也方面查看code),在src里面提取Thrift項目
3新建項目 ThriftHbaseCommon加入剛才生成的代碼,也將svn上的Thrift項目加入
4編寫訪問集群的Hbase代碼(當然你的集群及hbase應該是OK的)
TTransport transport = new TSocket("IP", 9090); TProtocol tProtocol = new TBinaryProtocol(transport); var client = new Hbase.Client(tProtocol); transport.Open(); List<TRowResult> reslut= client.getRow(Encoding.UTF8.GetBytes("TableName"),Encoding.UTF8.GetBytes("Rows.key"),null); foreach (var key in reslut) { Console.WriteLine(Encoding.UTF8.GetString(key.Row)); foreach (var k in key.Columns) { Console.Write(Encoding.UTF8.GetString(k.Key)+"\t"); Console.WriteLine(Encoding.UTF8.GetString(k.Value.Value)); } } Console.ReadLine();
需要注意的是,你要啟動對應IP的Thrift服務,命令為:hbase-daemon.sh start thrift