goHbase库的使用

import库:

import ("context""errors""fmt""github.com/jeanphorn/log4go""github.com/tsuna/gohbase""github.com/tsuna/gohbase/hrpc""io"
)

数据结构:

type HBHelper struct {HbaseUrl  stringZookeeperRoot stringOption  string_client   gohbase.Client
}

连接hbase函数:

func (hb *HBHelper) ConnectHBase(hbaseurl string,zkroot string) {hb.HbaseUrl = hbaseurlhb.ZookeeperRoot = zkroothb._client = gohbase.NewClient(hbaseurl,gohbase.ZookeeperRoot(zkroot))
}

指定时间戳写入一行数据:

//通过hb.PutsByRowkeyVersion(table, rowkey, values, hrpc.Timestamp(timestamp))调用,其中timestamp是time.Time类型,options也可以是其他 func(hrpc.Call)的函数
func (hb *HBHelper) PutsByRowkeyVersion(table, rowKey string, values map[string]map[string][]byte,options func(hrpc.Call) error) (err error) {putRequest, err := hrpc.NewPutStr(context.Background(), table, rowKey, values,options)if err != nil {log4go.Error("hrpc.NewPutStr: %s", err.Error())}_, err = hb._client.Put(putRequest)if err != nil {log4go.Error("hbase clients: %s", err.Error())}return
}

指定列按rowkey读取一条数据:

//指定表,通过options筛选数据,例如Families函数,或者filter函数
func (hb *HBHelper) GetsByOption(table string ,rowkey string, options func(hrpc.Call) error)(*hrpc.Result, error){getRequest, err := hrpc.NewGetStr(context.Background(), table,rowkey, options)if err != nil {log4go.Error("hrpc.NewGetStr: %s", err.Error())}res, err :=  hb._client.Get(getRequest)if err != nil {log4go.Error("hbase clients: %s", err.Error())}defer func() {if errs := recover(); errs != nil {switch fmt.Sprintf("%v", errs) {case "runtime error: index out of range":err = errors.New("NoSuchRowKeyOrQualifierException")case "runtime error: invalid memory address or nil pointer dereference":err = errors.New("NoSuchColFamilyException")default:err = fmt.Errorf("%v", errs)}}}()return res, nil
}

返回*hrpc.Result数据,使用方式如下:

 f := map[string][]string{"cf": []string{"name"}}getres, err := hb.GetsByOption("test",rowkey, hrpc.Families(f))if err != nil {fmt.Println(err)return}for i, v := range getres.Cells {row := string(v.Row[:])fam := string(v.Family[:])qua := string(v.Qualifier[:])value := string(v.Value[:])str := fmt.Sprintf("index:%d      Rowkey: %s      family: %s      qualifies:%s        value:%s", i, row, fam, qua, value)fmt.Println(str)}
package hrpc
// Result holds a slice of Cells as well as miscellaneous information about the response.
type Result struct {Cells   []*CellStale   boolPartial bool// Exists is only set if existance_only was set in the request query.Exists *bool
}type Cell pb.Cellpackage pbtype Cell struct {Row              []byte    `protobuf:"bytes,1,opt,name=row" json:"row,omitempty"`Family           []byte    `protobuf:"bytes,2,opt,name=family" json:"family,omitempty"`Qualifier        []byte    `protobuf:"bytes,3,opt,name=qualifier" json:"qualifier,omitempty"`Timestamp        *uint64   `protobuf:"varint,4,opt,name=timestamp" json:"timestamp,omitempty"`CellType         *CellType `protobuf:"varint,5,opt,name=cell_type,json=cellType,enum=pb.CellType" json:"cell_type,omitempty"`Value            []byte    `protobuf:"bytes,6,opt,name=value" json:"value,omitempty"`Tags             []byte    `protobuf:"bytes,7,opt,name=tags" json:"tags,omitempty"`XXX_unrecognized []byte    `json:"-"`
}

以上为源码的数据结构

Scan读取一列:

func (hb *HBHelper) ScanCol(table string, option1 func(hrpc.Call) error) (rsp []*hrpc.Result, err error) {var (scanRequest *hrpc.Scanres         *hrpc.Result)scanRequest, err = hrpc.NewScanStr(context.Background(), table, option1)if err != nil {log4go.Error("hrpc.NewScanStr: %s", err.Error())}scanner := hb._client.Scan(scanRequest)for {res, err = scanner.Next()if err == io.EOF || res == nil {break}if err != nil {log4go.Error("hrpc.Scan: %s", err.Error())}rsp = append(rsp, res)}return rsp, err
}

使用:

scanres, err := hb.ScanCol("test", hrpc.Families(f))
for i, arr := range scanres {for j, v := range arr.Cells {row := string(v.Row[:])fam := string(v.Family[:])qua := string(v.Qualifier[:])value := string(v.Value[:])str := fmt.Sprintf("indexA:%d        indexA:%d       Rowkey: %s      family: %s      qualifies:%s        value:%s", i, j, row, fam, qua, value)fmt.Println(str)}}           

gohbase的使用相关推荐

  1. golang开发需要掌握的核心包以及中间件,涵盖项目的各个领域

    常用包 常用包 说明 fmt 实现格式化的输入输出操作,其中的fmt.Printf()和fmt.Println()是开发者使用最为频繁的函数. io 实现了一系列非平台相关的IO相关接口和实现,比如提 ...

最新文章

  1. bitcoin全节点部署及bitcoind bitcoin-cli命令使用解释
  2. XML Schema用法
  3. python四舍五入round_四舍五入就用round( )?Python四舍五入的正确打开方式!
  4. MySQL松散索引扫描与紧凑索引扫描
  5. winserver的consul部署实践与.net core客户端使用(附demo源码)
  6. ros melodic 版本sudo rosdep init和rosdep upgrade失败的解决办法
  7. 英文XP系统安装中文包
  8. AVR-GCC与AVR单片机C语言开发,[推荐]AVR 单片机与GCC 编程 教程
  9. Java applet详解
  10. Java调用WebService接口
  11. vue3.0版本下动态修改表格数据
  12. 使用ps修改图片大小不影响清晰度的方法
  13. Heartbeat简介
  14. android 名片识别 简书,nodejs实现名片识别
  15. webpack自定义loader
  16. 分享几个有趣实用的网站
  17. 第1章关键角色及其职责——明确职责
  18. 基于Python的银行信用卡欺诈预测模型设计 文档+任务书+项目源码及数据
  19. 《javascript语言精粹》读书笔记——函数
  20. 如何把股票数据导出excel?导出股票历史数据到Excel的方法

热门文章

  1. 金融级IT架构:网商银行是如何进行数字化落地的
  2. 《网络运维基础知识手册》
  3. C语言编程题——杨氏矩阵
  4. 安装多个Vivado版本后,双击.xpr如何启动最新版本
  5. [生存志] 第93节 孟子齐魏立说
  6. 交通运输相关机器学习/深度学习相关资源列表,关于交通运输场景的机器学习/深度学习的资源这里都有
  7. coursera python_Python爬取Coursera课程资源的详细过程
  8. 看过千万的风景 比不上故乡的美丽
  9. 深度解析京东个性化推荐系统
  10. android 关机消息广播,Android开机、关机播音监听