背景
最近有个需求是写一个数据查询接口,数据存放在CDH搭建的Hadoop集群HBase中。一直以来是个坚定的Pythoner(其实是懒),不过今年以来逐步接触和尝试Go之后觉得很对胃口,再加上公司令人作呕的运维管控机制,使用Go的项目静态编译为单个文件,可以将运维依赖降到最低,所以对Go越发心仪。
用net/http配合gorilla/mux包轻车熟路的实现了一套简单的查询接口之后,本以为轻松完成HBase的DAL就可以打包测试了,但是要是coding都这么一帆风顺那就显示不出什么技(bi)术(ge)了。所以意料之外又情理之中的,用Go获取HBase中并没有那么简单。
本以为HBase这么成熟的数据库,Go会有很方便实用的官方或第三方库方便访问,但在搜寻一番之后,发现只有两个选择:HBase提供的Thrift,以及这个 仍被开发者标记为Beta版本的第三方库GoHbase。在起初调试Thrift始终无果的情况下,笔者尝试了GoHbase,使用简单,可以成功获取HBase数据。考虑到这是线上的项目,本着认(zhe)真(teng)负(dao)责(di)的态度,经过了又一番调试,总算用Thrift达成了目的,下面流水账记录一下具体过程。
使用的软件环境:
- go version go1.7.4 linux/amd64 & windows/amd64*
- Thrift version 0.10.0*
- HBase 1.2.0-cdh5.7.2
步骤
- 确定HBase安装目录,启动命令
- 用Thrift生成HBase SDK
- 实现客户端代码
具体过程
查询HBase目录及运行命令
HBase提供了两套thrift接口,首先要确定启动的hbase thrift server使用了哪套接口, 比如我的:

是第一套接口,如果参数是thrift2,就是第二套接口。
进入HBase的目录,找到Thrift文件:
[[email protected] thrift]$ ls -l /opt/cloudera/parcels/CDH/lib/hbase/include/thrift
total 44
-rw-r–r– 1 root root 24870 Jul 23 2016 hbase1.thrift
-rw-r–r– 1 root root 15126 Jul 23 2016 hbase2.thrift
用Thrift生成代码
在上一步中找到对应的thrift文件,将文件拷贝到个人目录下,运行:
thrift -out . -r hbase–gen go ${THRIFT}
生成的代码目录如下:
其中hbase-remote目录为生成的客户端测试代码,但如果直接运行,会得到一堆报错:
..\hbase1.go:1662: cannot use temp (type Text) as type string in assignment
..\hbase1.go:11229: cannot use temp (type Text) as type string in assignment
..\hbase1.go:12252: cannot use temp (type Text) as type string in assignment
..\hbase1.go:12669: cannot use temp (type Text) as type string in assignment
..\hbase1.go:13121: cannot use temp (type Text) as type string in assignment
..\hbase1.go:13531: cannot use temp (type Text) as type string in assignment
..\hbase1.go:13925: cannot use temp (type Text) as type string in assignment
..\hbase1.go:14330: cannot use temp (type Text) as type string in assignment
..\hbase1.go:14759: cannot use temp (type Text) as type string in assignment
..\hbase1.go:15173: cannot use temp (type Text) as type string in assignment
..\hbase1.go:15173: too many errors
错误: 进程退出代码 2.
可能是thrift版本不兼容造成的,在代码中发现如下定义:
type Text []byte
定位到报错的位置:
var _key1 string
if v, err := iprot.ReadString(); err != nil {
return thrift.PrependError(“error reading field 0: “, err)
} else {
temp := Text(v)
_key1 = temp
}
发现temp被赋值给string类型的_key1没有做类型转换,手动把所有报错位置都修改如下:
temp := Text(v)
_key1 = string(temp)
修改代码中host和port为实际地址,再次运行:
[[email protected] hbase-remote]$ go run hbase-remote.go
Usage of /tmp/go-build890271332/command-line-arguments/_obj/exe/hbase-remote
[-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2…]]:
-P string Specify the protocol (binary, compact, simplejson, json) (default “binary”)
-framed Use framed transport
-h string Specify host and port (default “10.59.74.135”)
-http Use http
-p int Specify port (default 9090)
-u string Specify the url
…….
错误解决。
现在可以将生成的hbase目录拷贝到$GOPATH/src中。
实现客户端
简单的示例代码如下:
package main
import (
"fmt"
"net"
"os"
"hbase1"
"github.com/apache/thrift/lib/go/thrift"
)
func main() {
host := "10.59.74.135"
port := "9090"
trans, err := thrift.NewTSocket(net.JoinHostPort(host, port))
if err != nil {
fmt.Println("Build socked failed: ", err)
os.Exit)
}
defer trans.Close()
var protocolFactory thrift.TProtocolFactory
//protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
client := hbase1.NewHbaseClientFactory(trans, protocolFactory)
if err := trans.Open(); err != nil {
fmt.Println("Opening socket failed: ", err)
os.Exit)
}
tableName := "agentBasicInfo" // tablename
rowKey := "1970010121012971" // rowkey
family := "basicinfo:entry_date" // column
tables, err := client.GetTableNames()
if err != nil {
fmt.Println("Get tables failed: ", err)
os.Exit)
}
for _, table := range tables {
fmt.Println("table: ", string(table))
}
fmt.Println("-------------------")
fmt.Printf("trying to get table: {%s}, rowkey: {%s}\n", tableName, rowKey)
//attr := map[string]hbase1.Text {"basicinfo":[]byte("entry_date")}
data, err := client.Get([]byte(tableName), []byte(rowKey), []byte(family), nil)
if err != nil {
fmt.Println("Get data failed: ", err)
}
for _, ele := range data {
fmt.Println("value: ", ele.Timestamp, " ", string(ele.Value))
}
}
执行结果如下:
[[email protected] test_hbase]$ go run test_thrift.go
table: KYLIN_010EV7WZQ6
table: KYLIN_228LAP2P5A
table: KYLIN_3AYUR4WPJW
table: KYLIN_4DX8LTMC7A
table: KYLIN_4XR1LT20V4
table: KYLIN_959ZEKZBEM
table: KYLIN_9OHU8KSWI3
table: KYLIN_A6DW68YNOX
table: KYLIN_A6JKAAU8KS
table: KYLIN_BB5KKOWPCN
table: KYLIN_BUNDHMMD78
table: KYLIN_BZTUAMVLK6
table: KYLIN_CMQF0PAX8T
table: KYLIN_DK8AAXFNR7
table: KYLIN_DPFEWKDP5N
……
Python客户端
其实Hbase源码包中已经有很多语言客户端的示例代码
[[email protected] repos]$ ls hbase-1.2.0-cdh5.7.2/hbase-examples/src/main
cpp java perl php protobuf python ruby sh
python客户端示例文件:
[email protected] python]$ tree .
├── thrift1
│ ├── DemoClient.py
│ └── gen-py
│ └── hbase
│ ├── constants.py
│ ├── Hbase.py
│ ├── Hbase.pyc
│ ├── Hbase-remote
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── ttypes.py
│ └── ttypes.pyc
└── thrift2
├── DemoClient.py
└── gen-py
└── hbase
├── constants.py
├── __init__.py
├── __init__.pyc
├── THBaseService.py
├── THBaseService.pyc
├── THBaseService-remote
├── ttypes.py
└── ttypes.pyc
分别对应两个版本的Thrift接口,参考其中DemoClient.py,即可实现自己的HBase客户端。
目录
-
- 背景
- 步骤
- 具体过程
- 查询HBase目录及运行命令
- 用Thrift生成代码
- 实现客户端
- Python客户端
- 目录
- 背景