thrfit + nodejs + HBase

1,981 阅读2分钟

使用nodejs访问HBase,利用thriftt【可伸缩的跨语言服务的发展软件框架】。由于一开始对thrift理解错位,一直没能够正确生成gen-nodejs文件。

本文记录一下在window上,nodejs 通过thrift访问HBase的正解。

第一步,安装thrift

1.1  下载

打开thrift官网下载地址:http://thrift.apache.org/download

选择第二个.exe文件【红框】,并下载。

下载的文件是一个名为 thrift-0.11.0.exe 可执行文件,不用点击运行哦!

1.2 重命名并更改存储路径

将该文件重命名为 thrift.exe,为了方便我也这样做了。

然后将重命名的文件放在window的某个目录下。我将文件放在 D:\soft\ Thrift\

1.3 配置环境变量

直接上图,简单明了


1.4 检测安装是否成功

输入thrift -version,安装成功!


2 生成thrift 的nodejs接口文件

http://thrift.apache.org/ 【官网地址】


纠结官网中红色标注的第二步很长时间。感觉很复杂的样子,window需要创建一个compiler 并且install it 吗?

要写一个.thrift文件吗?目前的Hbase(0.11.0,本文即基于此版本)有两套thrift接口(可以叫thrift1和thrift2),它们并不兼容(随意性太强,这可能是所有开源软件都具有的问题)。由于thrift2接口设计上要比Thrfit更优雅,或者说和现在的API更贴近, 因此thrift1有可能被淘汰掉。

git thrift2的地址:  https://github.com/apache/hbase/blob/master/hbase-thrift/src/main

/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift

下载该项目,将hbase.thrift 放在项目文件夹中,cmd命令行在项目内

运行 thrift --gen js:node hbase.thrift

目录中则多出文件夹gen-nodejs,其中包含如图所示的两个文件。

基本完成啦。

3. 如何开发与连接呢?

下面给出一个简单的例子

const thrift = require('thrift');
const HBase = require('../hbases/gen-nodejs/THBaseService');
exports.hbase= {    connection: () => {        let options = {            hb: {native_parser: true},            server: {                poolSize: 6,                connectTimeoutMS: 30000,                socketOptions: {keepAlive: 120},                reconnectTries: Number.MAX_VALUE            },            replset: {                rs_name: 'rs0',                socketOptions: {keepAlive: 120}            },            transport: thrift.TBufferedTransport,            protocol: thrift.TBinaryProtocol        };        return thrift.createConnection('127.0.0.1', '9090', options);    },    connect () {        this.connection()            .on('connect', () => {
                console.info('hbase hb connected.');                let client = thrift.createClient(HBase, this.connection())                // ....下面是client对数据库进行操作啦,不在此文说明了            })            .on('close', () => {                console.info('HB is closed');            })            .on('error', (err) => {                console.debug('error '+err.message);            });    },};