string hbaseCluster = "https://charju.azurehdinsight.net"; string hadoopUsername = "帳戶名字"; string hadoopPassword = "密碼"; ClusterCredentials creds = new ClusterCredentials(new Uri(hbaseCluster), hadoopUsername, hadoopPassword); var hbaseClient = new HBaseClient(creds); // No response when GetVersion var version = hbaseClient.GetVersion(); Console.WriteLine(Convert.ToString(version));
首先上代碼,這個太特麼的坑爹了!代碼在winform中是沒法運行滴!!!在命令行應用中是能夠的!!!(浪費了老子好幾天的時間……)shell
在winform中,經過windbg調試,發如今GetVersion的時候,主線程起了一個Task,而後等待Task的完成。在Task運行初期(大概1分鐘內),會有另一個線程,在WaitHandle,而後等一段時間,該線程消失。主線程中開始Retries調用,而後,就沒有而後了……apache
Anyway,命令行中,代碼是OK的。app
個人例子,是利用新浪上的API來獲得股票信息,好比說:http://hq.sinajs.cn/list=sz000977,sh600718,我每秒鐘調用一次,而後這些數據刷到hbase裏面去。ide
股票的實體類定義oop
public class StockEntity { public string Name { get; set; } public double TodayOpeningPrice { get; set; } public double YesterdayClosingPrice { get; set; } public double CurrentPrice { get; set; } public double TodayMaxPrice { get; set; } public double TodayMinPrice { get; set; } public double BidPriceBuy { get; set; } public double BidPriceSell { get; set; } public int FixtureNumber { get; set; } public double FixtureAmount { get; set; } public int Buy1Number { get; set; } public double Buy1Price { get; set; } public int Buy2Number { get; set; } public double Buy2Price { get; set; } public int Buy3Number { get; set; } public double Buy3Price { get; set; } public int Buy4Number { get; set; } public double Buy4Price { get; set; } public int Buy5Number { get; set; } public double Buy5Price { get; set; } public int Sell1Number { get; set; } public double Sell1Price { get; set; } public int Sell2Number { get; set; } public double Sell2Price { get; set; } public int Sell3Number { get; set; } public double Sell3Price { get; set; } public int Sell4Number { get; set; } public double Sell4Price { get; set; } public int Sell5Number { get; set; } public double Sell5Price { get; set; } public DateTime TransactionTime { get; set; } }
數據拉下來以後,新開一個線程,讓它去寫到hbase中。網站
ThreadPool.QueueUserWorkItem(new WaitCallback(SaveStockDataToHbase), se);
具體幹活代碼以下:ui
1 private void SaveStockDataToHbase(object state) 2 { 3 StockEntity se = state as StockEntity; 4 5 // Insert data into the HBase table. 6 string rowKey = Guid.NewGuid().ToString(); 7 8 CellSet cellSet = new CellSet(); 9 CellSet.Row cellSetRow = new CellSet.Row { key = Encoding.UTF8.GetBytes(rowKey) }; 10 cellSet.rows.Add(cellSetRow); 11 12 13 Type t = typeof(StockEntity); 14 15 foreach (string colname in stockEntityColumns) 16 { 17 var pi = t.GetProperty(colname); 18 object val = pi.GetValue(se); 19 20 Cell value = new Cell { column = Encoding.UTF8.GetBytes("charju:" + colname), data = Encoding.UTF8.GetBytes(Convert.ToString(val)) }; 21 cellSetRow.values.Add(value); 22 } 23 24 try 25 { 26 hbaseClient.StoreCells(hbaseStockTableName, cellSet); 27 } 28 catch (Exception ex) 29 { 30 Console.WriteLine(ex.Message); 31 } 32 }
6~10行,是生成一個新Row。20行,是反射實體類的每個Property 定義,來取對應的值(不然我要寫一坨重複的代碼)。21行,把對應的該列數據寫到這個行上。spa
26行,就是真正的放到hbase中。.net
上面20行,你可能會注意到:charju,這是個人column family的名字。回過頭來,看看hbase中的表是怎麼創建的命令行
string hbaseCluster = "https://charju.azurehdinsight.net"; string hadoopUsername = "<your name>"; string hadoopPassword = "<your password>"; string hbaseStockTableName = "StockInformation"; HBaseClient hbaseClient; public void CreateHbaseTable() { // Create a new HBase table. - StockInformation TableSchema stockTableSchema = new TableSchema(); stockTableSchema.name = hbaseStockTableName; stockTableSchema.columns.Add(new ColumnSchema() { name = "charju" }); hbaseClient.CreateTable(stockTableSchema); }
而hbaseClient的實例化,是在這裏:
ClusterCredentials creds = new ClusterCredentials(new Uri(hbaseCluster), hadoopUsername, hadoopPassword); hbaseClient = new HBaseClient(creds);
數據寫入後,咱們能夠有幾個方式來。一是在hbase中配置一下,容許RDP,而後remote上去跑hbase shell命令,惋惜我虛機裏面RDP總失敗,不知道爲啥。第二種方式,就是用HIVE來查。
鏈接到hbase的網站後,在hive editor那個界面中,先建立對應的表
CREATE EXTERNAL TABLE StockInformation(rowkey STRING, TodayOpeningPrice STRING, YesterdayClosingPrice STRING, CurrentPrice STRING, TodayMaxPrice STRING, TodayMinPrice STRING, BidPriceBuy STRING, BidPriceSell STRING, FixtureNumber STRING, FixtureAmount STRING, Buy1Number STRING, Buy1Price STRING, Buy2Number STRING, Buy2Price STRING, Buy3Number STRING, Buy3Price STRING, Buy4Number STRING, Buy4Price STRING, Buy5Number STRING, Buy5Price STRING, Sell1Number STRING, Sell1Price STRING, Sell2Number STRING, Sell2Price STRING, Sell3Number STRING, Sell3Price STRING, Sell4Number STRING, Sell4Price STRING, Sell5Number STRING, Sell5Price STRING, TransactionTime STRING) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ('hbase.columns.mapping' = ':key,charju:TodayOpeningPrice ,charju:YesterdayClosingPrice ,charju:CurrentPrice ,charju:TodayMaxPrice ,charju:TodayMinPrice ,charju:BidPriceBuy ,charju:BidPriceSell ,charju:FixtureNumber ,charju:FixtureAmount ,charju:Buy1Number ,charju:Buy1Price ,charju:Buy2Number ,charju:Buy2Price ,charju:Buy3Number ,charju:Buy3Price ,charju:Buy4Number ,charju:Buy4Price ,charju:Buy5Number ,charju:Buy5Price ,charju:Sell1Number ,charju:Sell1Price ,charju:Sell2Number ,charju:Sell2Price ,charju:Sell3Number ,charju:Sell3Price ,charju:Sell4Number ,charju:Sell4Price ,charju:Sell5Number ,charju:Sell5Price ,charju:TransactionTime') TBLPROPERTIES ('hbase.table.name' = 'StockInformation');
建立成功後,而後就能夠跑SQL了,好比說:
select * from StockInformation where buy1number=9800 order by transactiontime
今天小浪的最大一筆買入。固然,相似於select count(0) 之類的更OK了。
有用的鏈接:
https://azure.microsoft.com/en-us/documentation/articles/hdinsight-hbase-tutorial-get-started/