1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| package hbase
import ( "context" "github.com/golang/glog" "github.com/tsuna/gohbase" "github.com/tsuna/gohbase/hrpc" "io" )
type HbaseClient struct { host string option string client gohbase.Client adminClient gohbase.AdminClient }
func NewHbaseClient(host string) *HbaseClient { return &HbaseClient{ host: host, } }
func (hb *HbaseClient) Connect() { hb.client = gohbase.NewClient(hb.host) hb.adminClient = gohbase.NewAdminClient(hb.host) }
func (hb *HbaseClient) CreateTable(table string, families map[string]map[string]string) (err error) { createRequest := hrpc.NewCreateTable(context.Background(), []byte(table), families) if err = hb.adminClient.CreateTable(createRequest); err != nil { glog.Errorf("[CreateTable] CreateTable error! table->%s, error->%v", table, err) } return err }
func (hb *HbaseClient) DeleteTable(table string) (err error) { delRequest := hrpc.NewDeleteTable(context.Background(), []byte(table)) if err = hb.adminClient.DeleteTable(delRequest); err != nil { glog.Errorf("[DeleteTable] DeleteTable error! table->%s, error->%v", table, err) } return err }
func (hb *HbaseClient) PutsByRowkey(table, rowKey string, values map[string]map[string][]byte) (err error) { putRequest, err := hrpc.NewPutStr(context.Background(), table, rowKey, values) if err != nil { glog.Errorf("[PutsByRowkey] NewPutStr error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return err } _, err = hb.client.Put(putRequest) if err != nil { glog.Errorf("[PutsByRowkey] Put error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return err } return nil }
func (hb *HbaseClient) UpdateByRowkey(table, rowKey string, values map[string]map[string][]byte) (err error) { putRequest, err := hrpc.NewPutStr(context.Background(), table, rowKey, values) if err != nil { glog.Errorf("[UpdateByRowkey] NewPutStr error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return err } _, err = hb.client.Put(putRequest) if err != nil { glog.Errorf("[UpdateByRowkey] Put error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return err } return }
func (hb *HbaseClient) GetsByRowkey(table, rowKey string) (*hrpc.Result, error) { getRequest, err := hrpc.NewGetStr(context.Background(), table, rowKey) if err != nil { glog.Errorf("[GetsByRowkey] NewGetStr error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil, err } res, err := hb.client.Get(getRequest) if err != nil { glog.Errorf("[GetsByRowkey] Get error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil, err } return res, nil }
func (hb *HbaseClient) GetsByRowkeyCF(table, rowKey string, families map[string][]string) (*hrpc.Result, error) { getRequest, err := hrpc.NewGetStr(context.Background(), table, rowKey, hrpc.Families(families)) if err != nil { glog.Errorf("[GetsByRowkeyCF] NewGetStr error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil, err } res, err := hb.client.Get(getRequest) if err != nil { glog.Errorf("[GetsByRowkeyCF] Get error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil, err } return res, nil }
func (hb *HbaseClient) DeleteByRowkey(table, rowKey string, value map[string]map[string][]byte) (err error) { delRequest, err := hrpc.NewDelStr(context.Background(), table, rowKey, value) if err != nil { glog.Errorf("[DeleteByRowkey] NewDelStr error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil }
_, err = hb.client.Delete(delRequest) if err != nil { glog.Errorf("[DeleteByRowkey] Delete error! table->%s, rowKey->%s, error->%v", table, rowKey, err) return nil } return }
func (hb *HbaseClient) ScanByTable(table, startRow, stopRow string) ([]*hrpc.Result, error) { scanRequest, err := hrpc.NewScanRangeStr(context.Background(), table, startRow, stopRow) if err != nil { glog.Errorf("[ScanByTable] NewScanRangeStr error! table->%s, startRow->%s, endRow->%s, error->%v", table, startRow, stopRow, err) return nil, err } scan := hb.client.Scan(scanRequest) var res []*hrpc.Result for { getRsp, err := scan.Next() if err == io.EOF || getRsp == nil { break } if err != nil { glog.Errorf("[ScanByTable] scan.Next error! table->%s, error->%v", table, err) } res = append(res, getRsp) } return res, nil }
func (hb *HbaseClient) IsExistRowKey(table, rowKey string) bool { res, err := hb.GetsByRowkey(table, rowKey) if err != nil { return false } return len(res.Cells) > 0 }
|