5.0 nodejs通过thrift连接hbase

1、首先运行hbase thrift服务

./hbase-daemon.sh start thrift

2、由于我使用的是windows,所以需要下载thrift.exe,并将其配置到环境变量Path中

thrift下载地址:https://www.apache.org/dyn/closer.cgi?path=/thrift/0.16.0/thrift-0.16.0.exe

  3、复制Hbase.thrift到项目跟目录,并使用thrift命令生成文件

thrift有两个版本,thrift1和thrift2,这边使用是thrift1

hbase.thrift下载地址:https://github.com/apache/hbase/tree/rel/2.4.4/hbase-thrift/src/main/resources/org/apache/hadoop/hbase

//在cmd中执行

thrift --gen js:node Hbase.thrift

4、nodejs测试代码

 hbase中,已经预建了3张表,我准备把它打印出来,却发现,什么都没有输出

const thrift = require('thrift'),
    HBase = require('../../gen-nodejs/Hbase.js')

const connection = thrift.createConnection('10.7.128.18',9090,{ transport: thrift.TFramedTransport,protocol:thrift.TBinaryProtocol });

connection.on('connect',function(){
    console.log('connected');
    let client = thrift.createClient(HBase,connection);
    client.getTableNames(function(err,data){
        if(err)
            console.log('there was an error:',err);
        else
            console.log('hbase tables:',data.toString());
    });
});

connection.on('error',function(err){
    console.log('error',err);
});

5、使用python

安装happybase,pip3 install happybase,安装happybase的过程中会自动安装thrift

示例代码

import happybase

con = happybase.Connection('10.7.128.18', port=9090)
t = con.tables()
print(t)

成功输出了3张表名 

 那为啥nodejs就不行呢

6、经过一些资料查询,现得出解决方案

//在hbase的bin目录下,执行jps,查看正在运行的服务

停止ThriftServer服务,并重新启用服务,详细操作如下

jps  

./hbase-daemon.sh stop thrift

./hbase-daemon.sh start thrift -f 

 注意这里启动服务的命令增加了-f参数

再次运行nodejs测试代码,成功输出表名

 再次运行python代码,就会出现报错

 总结一下,如果是nodejs连,启动hbase thrift服务需要加-f参数,如果是python的happybase连的话不需要-f参数,上述的验证都是基于thrift1的实验

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值