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。

