Thrift2相比于Thrift 1改動較大,這里不去描述改動的地方,但是它的改動確實比Thrift1方便了很多。但是不能理解的是Thrift2網上的資料和文檔相當的少,就以Thrift2操作Hbase為例,Thrift2提供的crud操作主要有Put, Get, Delete, Scan和Increment,網上及官網上對其使用也比較簡單,對于實現一些復雜的操作無從下手,面對這么囧的狀況,沒辦法,只能去研究源碼了。通過研究源碼知道了Put, Get, Delete, Scan和Increment下一些復雜操作的使用,現以get為例進行描述,其他的都和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中get部分代碼如下:
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個參數,分別為row、columns、timestamp、timeRange、maxVersions、filterString、attributes。
如果只是簡單的在Hbase取某一行的數據,代碼如下:
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
});
當然如果想對輸出數據的個數加以限制的話,可以通過maxVersions的值來設定,這里就不解釋了。但是對于復雜的情況,例如我想查詢指定時間戳范圍內的data,該怎么辦?
方法就會用到timeRange參數,至于timeRange的形式如何寫就要看源碼了,經過調試,最終timeRange的使用方法的代碼實現如下:
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
});
其他的參數的使用方法可通過上述介紹的方法看源碼就可以很快的寫出相應的形式,希望上述介紹的方法能幫到你,歡迎轉載,轉載請注明出處http://www.cnblogs.com/cocos2014/p/4539092.html。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

