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 getgit.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 main import ( // "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,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,(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!")},Qualifier: []byte("create"),Value: []byte("user5")},Qualifier: []byte("create_time"),Value: []byte("2017-03-21 16:17:26")},Qualifier: []byte("tags"),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,(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,(tmpendTime - tmpstartTime)) } //struct func 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,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参加我的下载链接: 原文链接:https://www.f2er.com/go/188770.html