golang使用thrift2协议connect hbase
来源:互联网 发布:淘宝划线价 编辑:程序博客网 时间:2024/06/02 05:34
1. 我当前的环境是:
go version
go version go1.6 windows/amd64
hbase(main):001:0> version
1.2.4, r67592f3d062743907f8c5ae00dbbe1ae4f69e5af, Tue Oct 25 18:10:20 CDT 2016
创建,测试表 “tb_test”
hbase(main):003:0* create 'tb_test','cf', SPLITS=>['10','20','30','40']
hbase(main):003:0* desc "tb_test"
Table tb_test is ENABLED
tb_test
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '5', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'N
ONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE =>
'0'}
1 row(s) in 0.6630 seconds
在hadoop集群中启动thrfit2 Server:
./hbase-daemon.sh start thrift2
2. 准备golang客户端
2.1. 和python类似,下载thrift2库:http://thrift.apache.org/ 下载最新的 0.10 src版本,如果要以前的版本,也可到 http://archive.apache.org/dist/thrift 去下载 。
2.2. 编译安装:具体有多种方法,Maven,./configure make make install ...
2.3. 生成go代码:thrift -o <output directory{默认当前目录gen-py}> -gen go {对应版本的hbase源码地址}\src\main\resources\org\apache\Hadoop\hbase\thrift2
2.4. 将对应版本中golang接口code复制到当前golang安装目录,{$GOROOT}\src或者{$GOPATH}\src...
2.5. 再通过git获取外部资源git.apache.org/thrift.git/lib/go/thrift : go get git.apache.org/thrift.git/lib/go/thrift 或者直接到 https://github.com/apache/thrift 直接下载zip包,将至放在$GPPATH\src\git.apache.org\thrift.git\ 目录下即可。
3. 编写客户端代码
/** @Author: lesorb.cn* @Date: 2017-03-21 10:41:04* @Last Modified by: lesorb.cn* @Last Modified time: 2017-03-21 15:08:27*/package mainimport ( // "encoding/binary" "fmt" "git.apache.org/thrift.git/lib/go/thrift" "hbase" "net" "os" "reflect"// "strconv" "time")const ( HOST = "datanode1.hadoop" PORT = "9090" TESTRECORD = 10)func main() { startTime := currentTimeMillis() logformatstr_ := "----%s\n" logformatstr := "----%s Cut times :%d-%d=%d MS \n\n" logformattitle := "create connection " rowkey := "row_154092606735603" temptable := "tb_test" protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() transport, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT)) if err != nil { fmt.Fprintln(os.Stderr, "error resolving address:", err) os.Exit(1) } client := hbase.NewTHBaseServiceClientFactory(transport, protocolFactory) if err := transport.Open(); err != nil { fmt.Fprintln(os.Stderr, "Error opening socket to "+HOST+":"+PORT, " ", err) os.Exit(1) } tmpendTime := currentTimeMillis() fmt.Printf(logformatstr, logformattitle, tmpendTime, startTime, (tmpendTime - startTime)) defer transport.Close() //--------------Exists-------------------- logformattitle = " call exist method : " fmt.Printf(logformatstr_, logformattitle) tmpstartTime := currentTimeMillis() // isexists, err := (client.Exists([]byte(temptable), &hbase.TGet{Row: []byte(rowkey)})) fmt.Printf("rowkey{%s} in table{%s} Exists:%t\t", rowkey, temptable, isexists) if err != nil { fmt.Printf("Exists err:%s\n", err) } fmt.Println("") tmpendTime = currentTimeMillis() fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime)) //------------Get--------------- logformattitle = "call get method to retrieve data:" fmt.Printf(logformatstr_, logformattitle) tmpstartTime = currentTimeMillis() // result, err := (client.Get([]byte(temptable), &hbase.TGet{Row: []byte(rowkey)})) if err != nil { fmt.Printf("Get err:%s\n", err) } else { fmt.Println("Rowkey:" + string(result.Row)) for _, cv := range result.ColumnValues { printStruct(cv) } } tmpendTime = currentTimeMillis() fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime)) //--------------put------------------------ logformattitle = "call Put method to write data : " rowkey = "row_154092606735604" fmt.Printf(logformatstr_, logformattitle) tmpstartTime = currentTimeMillis() cvarr := []*hbase.TColumnValue{ &hbase.TColumnValue{ Family: []byte("cf"), Qualifier: []byte("title"), Value: []byte("welcome to lesorb.cn")}, &hbase.TColumnValue{ Family: []byte("cf"), Qualifier: []byte("content"), Value: []byte("welcome, why are u here!")}, &hbase.TColumnValue{ Family: []byte("cf"), Qualifier: []byte("create"), Value: []byte("user5")}, &hbase.TColumnValue{ Family: []byte("cf"), Qualifier: []byte("create_time"), Value: []byte("2017-03-21 16:17:26")}, &hbase.TColumnValue{ Family: []byte("cf"), Qualifier: []byte("tags"), Value: []byte("welcome,lesorb")}} temptput := hbase.TPut{Row: []byte(rowkey), ColumnValues: cvarr} err = client.Put([]byte(temptable), &temptput) if err != nil { fmt.Printf("Put err:%s\n", err) } else { fmt.Println("Put done") } tmpendTime = currentTimeMillis() fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime)) //------------DeleteSingle------------ logformattitle = "call DeleteSingle method to delete a data: " fmt.Printf(logformatstr_, logformattitle) tmpstartTime = currentTimeMillis() tdelete := hbase.TDelete{Row: []byte(rowkey)} err = client.DeleteSingle([]byte(temptable), &tdelete) if err != nil { fmt.Printf("DeleteSingle err:%s\n", err) } else { fmt.Printf("DeleteSingel done\n") } tmpendTime = currentTimeMillis() fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))}//structfunc printStruct(cv interface{}) { switch reflect.ValueOf(cv).Interface().(type) { case *hbase.TColumnValue: s := reflect.ValueOf(cv).Elem() typeOfT := s.Type() //get Thrift2 field for i := 0; i < s.NumField(); i++ { f := s.Field(i) fileldformatstr := "\t%d: %s(%s)= %v\n" switch f.Interface().(type) { case []uint8: fmt.Printf(fileldformatstr, i, typeOfT.Field(i).Name, f.Type(), string(f.Interface().([]uint8))) case *int64: var tempint64 int64 if f.Interface().(*int64) == nil { tempint64 = 0 } else { tempint64 = *f.Interface().(*int64) } fmt.Printf(fileldformatstr, i, typeOfT.Field(i).Name, f.Type(), tempint64) default: fmt.Printf("I don't know") } } default: fmt.Printf("I don't know") fmt.Print(reflect.ValueOf(cv)) }}func currentTimeMillis() int64 { return time.Now().UnixNano() / 1000000}
运行结果如下:
----create connection Cut times :1490084989059-1490084989042=17 MS---- call exist method :rowkey{row_154092606735603} in table{tb_test} Exists:true---- call exist method : Cut times :1490084989073-1490084989060=13 MS----call get method to retrieve data:Rowkey:row_154092606735603 0: Family([]uint8)= cf 1: Qualifier([]uint8)= content 2: Value([]uint8)= He's full of bad ideas.! 3: Timestamp(*int64)= 1489489549481 4: Tags([]uint8)= 0: Family([]uint8)= cf 1: Qualifier([]uint8)= create 2: Value([]uint8)= user4 3: Timestamp(*int64)= 1489489549481 4: Tags([]uint8)= 0: Family([]uint8)= cf 1: Qualifier([]uint8)= create_time 2: Value([]uint8)= 2017-03-14 11:04:58 3: Timestamp(*int64)= 1489489549481 4: Tags([]uint8)= 0: Family([]uint8)= cf 1: Qualifier([]uint8)= title 2: Value([]uint8)= idea 3: Timestamp(*int64)= 1489489549481 4: Tags([]uint8)=----call get method to retrieve data: Cut times :1490084989089-1490084989073=16 MS----call Put method to write data :Put done----call Put method to write data : Cut times :1490084989177-1490084989089=88 MS----call DeleteSingle method to delete a data:DeleteSingel done----call DeleteSingle method to delete a data: Cut times :1490084989195-1490084989178=17 MS
相关code参加我的下载链接:
http://download.csdn.net/detail/lesorb/9788720
- golang使用thrift2协议connect hbase
- python使用thrift2协议connect hbase
- thrift2访问hbase
- HBase thrift/thrift2 使用指南
- HBase thrift2 TIOError
- ambari 安装hbase thrift2服务
- 【hbase】thrift2 集群 && 指定端口
- PHP和Golang使用Thrift1和Thrift2访问Hbase0.96.2(ubuntu12.04)
- Thrift2
- 用c++ 通过thrift2 访问hbase数据库
- hbase thrift2 server , Bad version in readMessageBegin
- HBase Thrift2 CPU过高问题分析
- UDP协议中是否可以使用connect
- golang配置https协议
- 【golang】golang redis 的使用
- [Golang]网络游戏协议处理框架
- Golang网络游戏协议处理框架
- hbase客户端协议修改
- config server高可用的怀疑(非副本集模式)
- leetcode note
- SFTP之上传下载<单个>文件
- Lwip--ARP协议(ARP数据包处理)
- 蓝桥杯2014省赛
- golang使用thrift2协议connect hbase
- nmap学习之主机发现
- jpa 主键生成策略
- Nginx负载均衡与高可用的实现
- 权限的部分认识
- WaitHandle 中 set() reSet() waione关系
- FFmpeg发送流媒体的命令(UDP,RTP,RTMP)
- bat常用语法
- Linux下Nginx服务安装配置