byte(vs), &values)
checkError(err, c)
if len(fields) != len(values) {
checkError(fmt.Errorf("数量不一致"), c)
}
var item map[string]map[string][]byte = make(map[string]map[string][]byte)
for i, v := range fields {
vs := strings.Split(v, ":")
item[vs[0]] = make(map[string][]byte)
if len(vs) > 1 {
item[vs[0]][vs[1]] = []byte(values[i])
} else {
item[vs[0]][""] = []byte(values[i])
}
}
fmt.Println(item)
t, err := hrpc.NewPutStr(context.Background(), table, rowKey, item)
checkError(err, c)
res, err := variable.Client.Put(t)
checkError(err, c)
c.JSON(200, res)
}
3.3 通过列过滤数据
使用scanner实现,也可以通过过滤器实现。
// TODO USE FILTER
type item struct {
Row string `json:"row"`
Family string `json:"family"`
Qualifier string `json:"qualifier"`
Timestamp *uint64 `json:"timestamp"`
Cell_type *pb.CellType `json:"cell_type"`
Value string `json:"value"`
}
func TableColumnScan(c *gin.Context) {
table := c.Query("table")
column := c.Query("column")
vs := strings.Split(column, ":")
var items []item
t, err := hrpc.NewScan(context.Background(), []byte(table))
checkError(err, c)
res := variable.Client.Scan(t)
row, err := res.Next()
for err != io.EOF && row != nil {
for _, v := range row.Cells {
if string(v.Family) != vs[0] {
continue
}
if len(vs) > 1 {
if string(v.Qualifier) != vs[1] {
continue
}
}
fmt.Println(row.Cells)
items = append(items, item{
Row: string(v.Row),
Family: string(v.Family),
Qualifier: string(v.Qualifier),
Timestamp: v.Timestamp,
Cell_type: v.CellType,
Value: string(v.Value),
})
}
row, err = res.Next()
}
c.JSON(200, items)
}
再执行一遍1.3添加列的函数,调用接口,执行结果如下。
localhost:1313/TableColumnScan?table=course&column=C_Credit
[
{
"row": "c001",
"family": "C_Credit",
"qualifier": "",
"timestamp": 1680431640294,
"cell_type": 4,
"value": "2.0"
},
{
"row": "c001",
"family": "C_Credit",
"qualifier": "new",
"timestamp": 1680434951646,
"cell_type": 4,
"value": "5.0"
},
{
"row": "c002",
"family": "C_Credit",
"qualifier": "",
"timestamp": 1680431640328,
"cell_type": 4,
"value": "5.0"
},
{
"row": "c003",
"family": "C_Credit",
"qualifier": "",
"timestamp": 1680431640363,
"cell_type": 4,
"value": "3.0"
}
]
localhost:1313/TableColumnScan?table=course&column=C_Credit:new
[
{
"row": "c001",
"family": "C_Credit",
"qualifier": "new",
"timestamp": 1680434951646,
"cell_type": 4,
"value": "5.0"
}
]
3.4 修改行数据
与 1.3 函数代码一致
3.5 删除表指定记录
package controller
import (
"context"
"github.com/gin-go