hbase

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

MGin HBase 插件 (github.com/maczh/hbase)

MGin 的 Apache HBase 客户端插件,基于纯 Go 的 gohbase 客户端实现。 通过 ZooKeeper 发现 HBase 集群,直连 RegionServer 进行读写,无需 Thrift 网关。

插件架构与 github.com/maczh/nats、github.com/maczh/hive 保持一致:

  • 全局实例 var HBASE = &Hbase{}
  • 生命周期方法 Init(configData []byte) / Check() error / Close()(Init 签名与 NATS 插件一致,以兼容同一版 MGIN)
  • 单连接("0")与多连接(conns: c1,c2)两种模式
  • koanf 读取 go.data.hbase.* 配置;统一 gologger 日志;断线自动重连

安装

go get github.com/maczh/hbase

在 MGin 中注册

import (
    "github.com/maczh/hbase"
    "github.com/maczh/mgin"
)

func main() {
    app := mgin.Default()
    // 本地配置: go.config.used: hbase  /  prefix.hbase: hbase
    // 配置中心文件: hbase-test.yml
    app.MGin.UsePlugin("hbase", hbase.Hbase)
}

配置

单连接
go:
  data:
    hbase:
      zkquorum: 127.0.0.1:2181   # zookeeper quorum, 逗号分隔, 必填
      zkroot: /hbase             # 可选, zookeeper znode 父节点, 默认 /hbase
      user: ""                   # 可选, effective user
      readTimeout: 30            # 可选, Region 读超时(秒)
      lookupTimeout: 30          # 可选, Region 定位超时(秒)
      zkTimeout: 30              # 可选, ZooKeeper 会话超时(秒)
多连接
go:
  data:
    hbase:
      multi: true
      conns: c1,c2
      c1:
        zkquorum: 10.0.0.1:2181
        zkroot: /hbase
      c2:
        zkquorum: 10.0.0.2:2181
        zkroot: /hbase

多连接模式下所有读写方法都必须显式传入 tag(如 Hbase.Get("c1", "t", "rk"));单连接模式 tag 传空串即可。

API

数据值统一使用 map[string]map[string][]byte 表示:列族 → 列限定符 → 值(与 hrpc.NewPutStr 入参一致,Get 返回值可直接回灌 Put)。

写入 / 读取
values := map[string]map[string][]byte{
    "cf": {"name": []byte("maczh"), "age": []byte("18")},
}

// 写入
err := hbase.Hbase.Put("", "user", "rk1", values)

// 读取整行
row, err := hbase.Hbase.Get("", "user", "rk1")
// row => map[string]map[string][]byte{"cf": {"name": []byte("maczh"), "age": []byte("18")}}

// 只读指定列族/列
row, err = hbase.Hbase.Get("", "user", "rk1", hbase.WithFamilies(map[string][]string{"cf": {"name"}}))

// 前缀过滤器扫描(需 github.com/tsuna/gohbase/filter)
rows, err := hbase.Hbase.Scan("", "user", hbase.WithFilter(filter.NewPrefixFilter([]byte("rk"))))
for _, r := range rows {
    fmt.Println(r.Key, r.Values) // r.Key 为行键
}
删除
// 删除整行
err := hbase.Hbase.Delete("", "user", "rk1", nil)

// 仅删除指定 cells
err = hbase.Hbase.Delete("", "user", "rk1", map[string]map[string][]byte{"cf": {"age": nil}})
原子操作
// 追加(字节拼接)
newVals, err := hbase.Hbase.Append("", "user", "rk1", map[string]map[string][]byte{"cf": {"visit": []byte("1")}})

// 计数器自增(值为 8 字节大端编码的增量, 返回自增后结果)
cnt, err := hbase.Hbase.Increment("", "counter", "page", map[string]map[string][]byte{"cf": {"hits": encodeInt64(1)}})

// CAS 写入: 仅当 cf:name == []byte("maczh") 时写入
ok, err := hbase.Hbase.CheckAndPut("", "user", "rk1", values, "cf", "name", []byte("maczh"))
管理操作
// 建表
err := hbase.Hbase.CreateTable("", "user", map[string]map[string]string{
    "cf": {"MAX_VERSIONS": "1"},
})

// 删表
err = hbase.Hbase.DeleteTable("", "user")
底层客户端逃逸
// 原生 gohbase.Client, 可用于批量(SendBatch)、原生 Scan 等高级用法
cli, err := hbase.Hbase.Client("")
// 原生 gohbase.AdminClient, 用于快照、集群状态等
admin, err := hbase.Hbase.AdminClient("")

// 健康探测
err = hbase.Hbase.Ping("")

所有读写方法均有对应的 ...Context(ctx context.Context, ...) 变体(如 GetContext、PutContext、ScanContext、DeleteContext、AppendContext、IncrementContext),用于传递超时与取消信号。Init/Check/Close 与 NATS 插件签名一致,由 MGIN 框架自动回调。

前置条件

  • HBase ≥ 1.0 集群,且 ZooKeeper 可达(gohbase 通过 ZK 发现 RegionServer,不依赖 Thrift 网关)。
  • HBase 服务端开启原生客户端 RPC 端口(默认 60020)。

测试

go test ./... -run TestHbase -v

无 HBase 集群时测试会自动跳过;连接被拒不致失败。

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Hbase = &hbase{}

Hbase 是插件全局实例, 在 main.go 中通过 mgin.MGin.UsePlugin("hbase", hbase.Hbase) 注册。

Functions

This section is empty.

Types

type QueryOption

type QueryOption = func(hrpc.Call) error

QueryOption 是 Get/Scan 等查询类 RPC 的可选参数(列族过滤、过滤器等)。

func WithFamilies

func WithFamilies(families map[string][]string) QueryOption

WithFamilies 限定只返回指定的列族/列限定符, families 格式为 列族 -> []列限定符。

func WithFilter

func WithFilter(f filter.Filter) QueryOption

WithFilter 给查询附加 HBase 过滤器(前缀过滤、行键过滤等), 见 github.com/tsuna/gohbase/filter。

type Row

type Row struct {
	Key    string                       // 行键
	Values map[string]map[string][]byte // 列族 -> (列限定符 -> 值)
}

Row 表示 HBase 中一行的数据。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址