Thrift2相比于Thrift 1改動(dòng)較大,這里不去描述改動(dòng)的地方,但是它的改動(dòng)確實(shí)比Thrift1方便了很多。但是不能理解的是Thrift2網(wǎng)上的資料和文檔相當(dāng)?shù)纳伲鸵訲hrift2操作Hbase為例,Thrift2提供的crud操作主要有Put, Get, Delete, Scan和Increment,網(wǎng)上及官網(wǎng)上對(duì)其使用也比較簡(jiǎn)單,對(duì)于實(shí)現(xiàn)一些復(fù)雜的操作無(wú)從下手,面對(duì)這么囧的狀況,沒(méi)辦法,只能去研究源碼了。通過(guò)研究源碼知道了Put, Get, Delete, Scan和Increment下一些復(fù)雜操作的使用,現(xiàn)以get為例進(jìn)行描述,其他的都和get相似。
先看hbase_types.js中TGet:
1 TGet = module.exports.TGet = function (args) { 2 this .row = null ; 3 this .columns = null ; 4 this .timestamp = null ; 5 this .timeRange = null ; 6 this .maxVersions = null ; 7 this .filterString = null ; 8 this .attributes = null ; 9 if (args) { 10 if (args.row !== undefined) { 11 this .row = args.row; 12 } 13 if (args.columns !== undefined) { 14 this .columns = args.columns; 15 } 16 if (args.timestamp !== undefined) { 17 this .timestamp = args.timestamp; 18 } 19 if (args.timeRange !== undefined) { 20 this .timeRange = args.timeRange; 21 } 22 if (args.maxVersions !== undefined) { 23 this .maxVersions = args.maxVersions; 24 } 25 if (args.filterString !== undefined) { 26 this .filterString = args.filterString; 27 } 28 if (args.attributes !== undefined) { 29 this .attributes = args.attributes; 30 } 31 } 32 };
THBase_Severce.js中g(shù)et部分代碼如下:
1 THBaseService_get_args = function (args) { 2 this .table = null ; 3 this .get = null ; 4 if (args) { 5 if (args.table !== undefined) { 6 this .table = args.table; 7 } 8 if (args.get !== undefined) { 9 this .get = args.get; 10 } 11 } 12 };
由以上的部分源碼可知,使用get要用到TGet,TGet中有7個(gè)參數(shù),分別為row、columns、timestamp、timeRange、maxVersions、filterString、attributes。
如果只是簡(jiǎn)單的在Hbase取某一行的數(shù)據(jù),代碼如下:
1 var thrift = require('thrift' ); 2 var HBase = require('./gen-nodejs/THBaseService' ); 3 var HBaseTypes = require('./gen-nodejs/hbase_types' ); 4 5 var connection = thrift.createConnection('localhost', 9090 , { 6 transport: thrift.TFramedTransport, 7 protocol: thrift.TBinaryProtocol 8 }); 9 10 connection.on('connect', function () { 11 console.log('connected' ); 12 var client = thrift.createClient(HBase, connection); 13 14 15 var tGet = new HBaseTypes.TGet({row: '10_20121208' , 16 columns: [ new HBaseTypes.TColumn({family: 'DATA' })]}); 17 client.get('tablename', tGet, function (err, data) { 18 if (err) { 19 console.log(err); 20 } else { 21 console.log(data); 22 } 23 connection.end(); 24 }); 25 26 }); 27 28 connection.on('error', function (err){ 29 console.log('error' , err); 30 });
當(dāng)然如果想對(duì)輸出數(shù)據(jù)的個(gè)數(shù)加以限制的話,可以通過(guò)maxVersions的值來(lái)設(shè)定,這里就不解釋了。但是對(duì)于復(fù)雜的情況,例如我想查詢(xún)指定時(shí)間戳范圍內(nèi)的data,該怎么辦?
方法就會(huì)用到timeRange參數(shù),至于timeRange的形式如何寫(xiě)就要看源碼了,經(jīng)過(guò)調(diào)試,最終timeRange的使用方法的代碼實(shí)現(xiàn)如下:
1 var thrift = require('thrift' ); 2 var HBase = require('./gen-nodejs/THBaseService' ); 3 var HBaseTypes = require('./gen-nodejs/hbase_types' ); 4 5 var connection = thrift.createConnection('localhost', 9090 , { 6 transport: thrift.TFramedTransport, 7 protocol: thrift.TBinaryProtocol 8 }); 9 10 connection.on('connect', function () { 11 console.log('connected' ); 12 var client = thrift.createClient(HBase, connection); 13 14 15 var tGet = new HBaseTypes.TGet({row: '10_20121002' , 16 columns: [ new HBaseTypes.TColumn({family: 'PLATE' })], 17 timeRange: new HBaseTypes.TTimeRange({minStamp:1349138457,maxStamp:1349153466 }) 18 }); 19 client.get('rdga_by_ymd', tGet, function (err, data) { 20 if (err) { 21 console.log(err); 22 } else { 23 console.log(data); 24 } 25 connection.end(); 26 }); 27 28 }); 29 30 connection.on('error', function (err){ 31 console.log('error' , err); 32 });
其他的參數(shù)的使用方法可通過(guò)上述介紹的方法看源碼就可以很快的寫(xiě)出相應(yīng)的形式,希望上述介紹的方法能幫到你,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處http://www.cnblogs.com/cocos2014/p/4539092.html。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
