最近要进行大数据相关的任务,牛刀小试,先将最基础的HBase搭建并实践起来。本文借用docker,快速搭建HBase基础环境,并使用go结合thrift调用相关API进行数据操作。
0、方便起见,需要一个docker环境,安装配置略过不表。
1、拉取image

docker pull harisekhon/hbase

2、修改entrypoint.sh,启用thrift2

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

3、挂载修改后的启动文件,启动hbase,并暴露thrift2端口

docker run -d -p 9090:9090 -v `pwd`/entrypoint.sh:/entrypoint.sh --name hbase harisekhon/hbase

4、运行hbase shell,建表

docker exec -it hbase bash

hbase shell

// 建表

create 'elvizlai_test',{NAME => 'f1', VERSIONS => 2},{NAME => 'f2', VERSIONS => 2}

// 删除表

disable 'elvizlai_test'

drop 'elvizlai_test

4、thrift for mac安装,感谢brew
brew install thrift

5、下载hbase thrift2对应的hbase.thrift文件,生成go package

wget https://raw.githubusercontent.com/apache/hbase/master/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift

thrift -r -out . --gen go *.thrift

6、撰写main.go一探究竟吧

package main

import (

 "encoding/binary"

 "fmt"

 "hbase"

 "reflect"

 "strconv"

 "time"

 "git.apache.org/thrift.git/lib/go/thrift"

)

const HOST = "127.0.0.1"

const PORT = "9090"

const TESTRECORD = 10

func main() {

 startTime := currentTimeMillis()

 logformatstr_ := "----%sn"

 logformatstr := "----%s 用时:%d-%d=%d毫秒nn"

 logformattitle := "建立连接"

 table := "elvizlai_test"

 rowkey := "1"

 family := "f1"

 protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

 transport, err := thrift.NewTSocket(HOST + ":" + PORT)

 if err != nil {

 panic(err)

 }

 client := hbase.NewTHBaseServiceClientFactory(transport, protocolFactory)

 if err := transport.Open(); err != nil {

 panic(err)

 }

 tmpendTime := currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, startTime, (tmpendTime - startTime))

 defer transport.Close()

 //--------------Exists

 logformattitle = "调用Exists方法"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime := currentTimeMillis()

 isexists, err := client.Exists([]byte(table), &hbase.TGet{Row: []byte(rowkey)})

 fmt.Printf("rowkey{%s} in table{%s} Exists:%tn", rowkey, table, isexists)

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //--------------Put

 logformattitle = "调用Put方法写数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 cvarr := []*hbase.TColumnValue{

 {

 Family: []byte(family),

 Qualifier: []byte("idoall.org"),

 Value: []byte("welcome idoall.org"),

 },

 }

 temptput := hbase.TPut{Row: []byte(rowkey), ColumnValues: cvarr}

 err = client.Put([]byte(table), &temptput)

 if err != nil {

 fmt.Printf("Put err:%sn", err)

 } else {

 fmt.Println("Put done")

 }

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //------------Get---------------

 logformattitle = "调用Get方法获取新增加的数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 result, err := client.Get([]byte(table), &hbase.TGet{Row: []byte(rowkey)})

 if err != nil {

 fmt.Printf("Get err:%sn", err)

 } else {

 fmt.Println("Rowkey:" + string(result.Row))

 for _, cv := range result.ColumnValues {

 printscruct(cv)

 }

 }

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //--------------put update

 logformattitle = "调用Put update方法'修改'数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 cvarr = []*hbase.TColumnValue{

 {

 Family: []byte(family),

 Qualifier: []byte("idoall.org"),

 Value: []byte("welcome idoall.org---update"),

 },

 }

 temptput = hbase.TPut{Row: []byte(rowkey), ColumnValues: cvarr}

 err = client.Put([]byte(table), &temptput)

 if err != nil {

 fmt.Printf("Put update err:%sn", err)

 } else {

 fmt.Println("Put update done")

 }

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //------------Get update---------------

 logformattitle = "调用Get方法获取'修改'后的数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 //

 result, err = (client.Get([]byte(table), &hbase.TGet{Row: []byte(rowkey)}))

 if err != nil {

 fmt.Printf("Get update err:%sn", err)

 } else {

 fmt.Println("update Rowkey:" + string(result.Row))

 for _, cv := range result.ColumnValues {

 printscruct(cv)

 }

 }

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //------------DeleteSingle------------

 logformattitle = "调用DeleteSingle方法删除一条数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 tdelete := hbase.TDelete{Row: []byte(rowkey)}

 err = client.DeleteSingle([]byte(table), &tdelete)

 if err != nil {

 fmt.Printf("DeleteSingle err:%sn", err)

 } else {

 fmt.Print("DeleteSingel donen")

 }

 tmpendTime = currentTimeMillis()

 fmt.Printf(logformatstr, logformattitle, tmpendTime, tmpstartTime, (tmpendTime - tmpstartTime))

 //-------------PutMultiple----------------

 logformattitle = "调用PutMultiple方法添加" + strconv.Itoa(TESTRECORD) + "条数据"

 fmt.Printf(logformatstr_, logformattitle)

 tmpstartTime = currentTimeMillis()

 var tputArr []*hbase.TPut

 for i := 0; i 

文章来源于互联网:HBase golang初探

发表评论