文章目录
- 1. 建立连接
- 2. 创建表
- 3. 插入记录
- 4. 删除记录
- 5. 查询记录
- 5.1 根据RowKey查询
- 5.2 scan范围查询
- 5.3 复杂查询(过滤器的使用)
- 5.3.1 比较过滤器
- 5.3.2 行过滤器
- 5.3.3 列过滤器
- 5.4.3 列族过滤器
1. 建立连接
import (
"context"
"github.com/tsuna/gohbase"
"github.com/tsuna/gohbase/hrpc"
"github.com/tsuna/gohbase/filter"
)
client := gohbase.NewClient(host)
adminClient := gohbase.NewAdminClient(host)
2. 创建表
tableName := "test"
families := map[string]map[string]string{
"cf": {
"BLOOMFILTER": "ROW",
"VERSIONS": "3",
"IN_MEMORY": "false",
"KEEP_DELETED_CELLS": "false",
"DATA_BLOCK_ENCODING": "FAST_DIFF",
"TTL": "2147483647",
"COMPRESSION": "NONE",
"MIN_VERSIONS": "0",
"BLOCKCACHE": "true",
"BLOCKSIZE": "65536",
"REPLICATION_SCOPE": "0",
}
}
ct := hrpc.NewCreateTable(context.Background(), []byte(tableName), families)
err := adminClient.CreateTable(ct)
3. 插入记录
rowKey := "18hd9j9aisdj9j9832dn9ooijd"
value := map[string]map[string][]byte{
"cf" {
"col1": []byte("val1"),
"col2": []byte("val2"),
"col3": []byte("val3"),
}
}
putRequest, err := hrpc.NewPutStr(context.Background(), tableName, rowKey, value)
res := client.Put(putRequest)
4. 删除记录
req, err := hrpc.NewDelStr(context.Background(), tableName, rowKey, value)
res, err := client.Delete(req)
5. 查询记录
5.1 根据RowKey查询
getRequest, err = hrpc.NewGetStr(context.Background(), tableName, rowKey)
getRes, err = client.Get(getRequest)
5.2 scan范围查询
getRequest, err := hrpc.NewScanRangeStr(context.Background(), tableName, startRow, stopRow)
scan := client.Scan(getRequest)
var res []*hrpc.Result
for {
getRsp, err := scan.Next()
if err == io.EOF || getRsp == nil {
break
}
if err != nil {
log.Print(err, "scan.Next")
}
res = append(res, getRsp)
}
5.3 复杂查询(过滤器的使用)
var hf []func(hrpc.Call) error
var Families map[string][]string
hf = append(hf, hrpc.Families(Families))
hf = append(hf, hrpc.Filters(f))
getRequest, err = hrpc.NewGetStr(context.Background(), tableName, rowKey, hf...)
getRequest, err := hrpc.NewScanRangeStr(context.Background(), tableName, startRow, stopRow, hf...)
5.3.1 比较过滤器
比较过滤器不单独使用, 需配合其它过滤器使用
value := "aaaaa"
comparable := filter.NewByteArrayComparable([]byte(value))
var fc filter.Comparator
fc = filter.NewBinaryComparator(comparable)
fc = filter.NewLongComparator(comparable)
fc = filter.NewBinaryPrefixComparator(comparable)
bitwiseOpInt := 1
bitwiseOp := filter.BitComparatorBitwiseOp(bitwiseOpInt)
fc = filter.NewBitComparator(bitwiseOp, comparable)
fc = filter.NewNullComparator()
PatternFlags := 2
Charset := "utf-8"
Engin := "JAVA"
fc = filter.NewRegexStringComparator(value, PatternFlags, Charset, Engine)
fc = filter.NewSubstringComparator(value)
CompareOp := 2
var f *filter.CompareFilter
f = filter.NewCompareFilter(filter.CompareType(CompareOp), fc)
5.3.2 行过滤器
var f filter.Filter
f = filter.NewPageFilter(100)
f = filter.NewPrefixFilter([]byte("prefix"))
f = filter.NewQualifierFilter(f.(*filter.CompareFilter))
f = filter.NewRandomRowFilter(0.8)
f = filter.NewRowFilter(f.(*filter.CompareFilter))
f = filter.NewTimestampsFilter(r.Timestamps)
f = filter.NewValueFilter(f.(*filter.CompareFilter))
f = filter.NewRowRange([]byte(startRow), []byte(stopRow), true, false)
var rowRangeList *filter.RowRange
f = filter.NewMultiRowRangeFilter(rowRangeList)
f = filter.NewInclusiveStopFilter([]byte(stopRow))
var bbp []*filter.BytesBytesPair
bbp = append(bbp, filter.NewBytesBytesPair([]byte, []byte))
f = filter.NewFuzzyRowFilter(bbp)
5.3.3 列过滤器
f = filter.NewColumnCountGetFilter(10)
f = filter.NewColumnPaginationFilter(limit, offset, []byte(columnOffset))
f = filter.NewColumnPrefixFilter([]byte(prefix))
f = filter.NewColumnRangeFilter([]byte(minColumn), []byte(maxColumn), true, false)
f = filter.NewDependentColumnFilter(f.(*filter.CompareFilter), []byte(c.ColumnFamily), []byte(c.ColumnQualifier), true)
f = filter.NewMultipleColumnPrefixFilter(sortedPrefix)
f = filter.NewSingleColumnValueFilter([]byte(ColumnFamily),
[]byte(ColumnQualifier), compareOP, fc,
FilterIfMissing, LatestVersionOnly)
f = filter.NewSingleColumnValueExcludeFilter(f.(*filter.SingleColumnValueFilter))
f = filter.NewKeyOnlyFilter(true)
f = filter.NewFirstKeyOnlyFilter()
f = filter.NewFirstKeyValueMatchingQualifiersFilter([][]byte)
5.4.3 列族过滤器
f = filter.NewFamilyFilter(f.(*filter.CompareFilter))