c# 访问hbase_C#教程之通过Thrift实现C#与Hbase交流
近期著手的一個項目需要將我方數(shù)據(jù)存儲到Hadoop的大數(shù)據(jù)環(huán)境,由于本人是.net平臺的開發(fā)者,沒有怎么接觸過大數(shù)據(jù)(因為他實(shí)在是太高大尚了)。但還好baidu, google后,還是很找到了解決辦法,就是C#寫數(shù)據(jù)到hbase,然后大數(shù)據(jù)開發(fā)者在從hbase讀取數(shù)據(jù)進(jìn)行多維度處理,如將一部分歷史數(shù)據(jù)轉(zhuǎn)移到hive,或者是將一部分?jǐn)?shù)據(jù)推送到機(jī)器學(xué)習(xí)庫進(jìn)行學(xué)習(xí)。
一、獲取hbase的thrift定義,并生成c#類
1.1 到hbase源碼地址獲取thrift的定義
注意Hbase的版本一定要與運(yùn)行的Hbase對應(yīng)。
請選擇thrift,而不是thrift2,原因thrift接口看上更為好用
1.2 獲取thrift,生成c#代碼
thrift生成代碼可以參考的我《半小時入Thrift》
二、啟動hbase的thrift服務(wù)
輸入如下命令:
hbase-daemon.shstart thrift
hbase-daemons.sh start thrift (集群版本)
默認(rèn)的thrift端口是9090,可以在hbase-site.xml配置文件中修改默認(rèn)端口。
三、用C#編寫測試代碼
var transport = new TSocket("10.34.51.62", 9090);
TProtocol protocol= newTBinaryProtocol(transport);var client = newHbase.Client(protocol);
transport.Open();var tabls =client.getTableNames();foreach(var t intabls)
{
Console.WriteLine(Encoding.Default.GetString(t));
}//寫
Mutation mutation = newMutation();
mutation.Column= Encoding.UTF8.GetBytes("personal_data:abc");
mutation.Value= Encoding.UTF8.GetBytes("Hello");
client.mutateRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), new List { mutation }, null);
Console.WriteLine("add success");var row = client.getRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), null);foreach (var r inrow)
{
Console.WriteLine(Encoding.UTF8.GetString(r.Row));foreach(var c inr.Columns)
{
Console.WriteLine("--" + Encoding.UTF8.GetString(c.Key) + ":" +Encoding.UTF8.GetString(c.Value.Value));
}
}
client.deleteAllRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), null);
Console.WriteLine("delete success");
Console.ReadKey();
transport.Close();
上述代碼實(shí)現(xiàn)了hbase寫入,讀取,刪除等功用。
四、測試結(jié)果
成功寫入。
五、總結(jié)
1. 注意選擇thrift和版本,通過查看maven的依賴獲得
2. 使用thirft版本,而不是thrift2
3. hbase的rowKey+列Key相同時,會對數(shù)據(jù)進(jìn)行修改
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的c# 访问hbase_C#教程之通过Thrift实现C#与Hbase交流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中3个单引号_Python中
- 下一篇: c# hash 泛型_C# 泛型Dict