运行环境

  • 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目录放入工程中。


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

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)
}