让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> 数据库 -> golang使用阿里云的代码连接hbase thrift

golang使用阿里云的代码连接hbase thrift

点击数:2217 发表时间:2020-11-09 19:58:43 作者: 来源链接:
分享到:
分享到微信

运行环境

  • golang 1.13.5

  • hbase  2.2.5

  • thrift  0.12.0

golang和hbase的安装不再赘述。

服务器端:

打开hbase的thrift2服务,默认端口9090。

/hbase目录/bin/hbase-daemon.sh start thrift2


客户端:
安装golang-thrift库。本地下载指定版本的库文件代码,https://github.com/apache/thrift/tree/0.12.0

放到本机配置的go path里。


使用阿里云已经生成好的go-thrift-hbase库代码,把hbase目录放入工程中。

https://github.com/aliyun/aliyun-apsaradb-hbase-demo/tree/master/hbase/thrift2/go


测试代码如下。测试指定表是否记录,其他的请自己撸。

import (
    ctx "context"
    "github.com/apache/thrift/lib/go/thrift"
    "hbase"
)

func main(){
    table := "test_table" //检查某个表里是否有某个记录
    rowkey := "12345"
    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
    transport, err := thrift.NewTSocket(net.JoinHostPort("192.168.0.88", "9090"))
    if err != nil {
        panic(err)
    }
    
    client := hbase.NewTHBaseServiceClientFactory(transport, protocolFactory)
    if err := transport.Open(); err != nil {
        panic(err)
    }
    defer transport.Close()
    
    isexists, err := client.Exists(ctx.Background(), []byte(table), &hbase.TGet{Row: []byte(rowkey)})
    if err != nil {
        panic(err)
    }
    fmt.Printf("rowkey{%s} in table{%s} Exists:%t\n", rowkey, table, isexists)
}


4
很 好
4
一 般
4
差 劲
热门新闻
相关文章
上一篇: golang使用github的sdming/goh库连接hbase的thrift服务
下一篇: SQL语句优化技术分析
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号