设为首页 加入收藏

TOP

'林子雨大数据' 实验3 HBase操作与接口编程(六)
2023-07-23 13:30:32 】 浏览:105
Tags:' 林子雨 实验 HBase
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) } image-20230402192646580 image-20230402192803294

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
首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go map 竟然也会发生内存泄漏? 下一篇好用的在线客服系统Go语言源码-GO..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目