首先通過連結 http://archive.apache.org/dist/thrift/0.12.0/thrift-0.12.0.tar.gz
下載下傳 thrift 0.12.0 版本的 tar 包
通過 visual studio IDE 建立一個 .net framework 項目的類庫解決方案

thrift-0.12.0\thrift-0.12.0\lib\csharp\src
下,可以看到我們需要的檔案 下載下傳 hbase 的 thrift 定義檔案。
下載下傳完成後,打開指令行,跳轉到對應的目錄下,執行 thrift-0.12.0.exe --gen csharp hbase.thrift,這個指令會生成一個 gen-csharp 的檔案夾
\bin\Debug
下能找到)
生成 aaa.dll 和 bbb.dll 之後,便能夠編寫簡單的 demo 進行通路了。還是通過 visual studio 建立一個 .net framework 的控制台應用,在引用處,添加 aaa.dll 和 bbb.dll。下面我給出簡單的 demo 代碼,大緻做了以下幾件事:
- 初始化用戶端
- 建立 namespace
- 建立 table
- put 一條資料
- get 一條資料并且列印出來
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Protocol;
using Thrift.Transport;
namespace hbaseDemo
{
class Program
{
static void Main(string[] args)
{
try
{
Uri uri = new Uri("http://ld-bp1u7jrc0818gtyql-proxy-hbaseue-pub.hbaseue.rds.aliyuncs.com:9190");
THttpClient transport = new THttpClient(uri);
IDictionary<string, string> header = transport.CustomHeaders;
header.Add("ACCESSKEYID", "root");
header.Add("ACCESSSIGNATURE", "root");
TProtocol tProtocol = new TBinaryProtocol(transport);
var client = new THBaseService.Client(tProtocol);
transport.Open();
String ns = "guchi5";
client.createNamespace(new TNamespaceDescriptor(ns));
byte[] tableNameBytes = Encoding.UTF8.GetBytes("tablename");
byte[] familyByte = Encoding.UTF8.GetBytes("f");
TTableName tTableName = new TTableName(tableNameBytes);
tTableName.Ns = Encoding.UTF8.GetBytes(ns);
TTableDescriptor tTableDescriptor = new TTableDescriptor(tTableName);
List<TColumnFamilyDescriptor> columns = new List<TColumnFamilyDescriptor>();
columns.Add(new TColumnFamilyDescriptor(Encoding.UTF8.GetBytes("f")));
tTableDescriptor.Columns = columns;
client.createTable(tTableDescriptor, null);
byte[] rowByte = Encoding.UTF8.GetBytes("row");
List<TColumnValue> list = new List<TColumnValue>();
list.Add(new TColumnValue(familyByte, Encoding.UTF8.GetBytes("q1"), Encoding.UTF8.GetBytes("value-guchi")));
string namespaceAndTableName = ns + ":tablename";
byte[] namespaceAndTableNameBytes = Encoding.UTF8.GetBytes(namespaceAndTableName);
client.put(namespaceAndTableNameBytes, new TPut(rowByte, list));
TGet tget = new TGet(rowByte);
TResult result = client.get(namespaceAndTableNameBytes, tget);
Console.WriteLine("RowKey:\n{0}", Encoding.UTF8.GetString(result.Row));
//列印Qualifier和對應的Value
foreach (var k in result.ColumnValues)
{
Console.WriteLine("Family:Qualifier:" + "\n" + Encoding.UTF8.GetString(k.Qualifier));
Console.WriteLine("Value:" + Encoding.UTF8.GetString(k.Value));
}
Console.WriteLine("done");
} catch (Exception e)
{
System.Console.WriteLine(e);
}
}
}
}
運作上述代碼能看到以下輸出
需要注意以下 2 點:
- 替換代碼中的 url 部分,換成你自己的執行個體對應的公網連接配接位址
- 參考文檔,對你需要進行通路的執行個體開通白名單和公網通路位址 https://help.aliyun.com/document_detail/119565.html?spm=a2c4g.11186623.6.560.1de042ddV8UbdR