登录
首页 >  Golang >  Go问答

迭代 BigTable 行上的单元格版本

来源:stackoverflow

时间:2024-05-01 12:09:35 223浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《迭代 BigTable 行上的单元格版本》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我有一行包含特定单元格的多个版本。 我想获取并迭代此单元格的所有现有版本。像这样的东西:

for _, cellversion := range row["columnfamily"]["columnqualifier"]{
   // (...)
}

我尝试使用函数 readrow 但它返回一个 map[string][]readitem ,显然,这是一个一维元胞数组。

row, err := table.ReadRow(ctx, rowKey)
row["MyColumnFamily"] // -> array of unidimensional cells

解决方案


我的问题的原因是 bigtable 数据模型支持每个单元的多个版本。过于简单化:每个单元格都是相似数据的不同版本的多维数组。

python sdk 以非常简单的方式实现了这个概念:每个单元格都表示为一个项目列表(可能版本的列表)。

>>> row = table.read_row(row_key)
>>> data = row.to_dict()
>>> cell = data[<column_family:column_qualifier>]
[<cell value=b'bla1' timestamp=2020-01-16 21:45:39.921000>, <cell value=b'bla2' timestamp=2020-01-16 21:57:31>...]

然而,在golang sdk中,所有单元格的所有版本都位于同一个数组中。因此,如果您想要一个包含同一单元的所有版本的数组,则必须手动对其进行分组。

row, _ := r.table.readrow(ctx, row_key)
columns, ok := row[<column_family>]

values := make(map[string][]bigtable.readitem, 0)

for _, cell := range columns {
    _, ok := values[cell.column]
    if !ok {
        values[cell.column] = make([]bigtable.readitem, 0)
    }

    values[cell.column] = append(values[cell.column], cell)
 }

现在我们可以访问同一单元的不同版本的列表:

cell := values[<column_family:column_qualifier>]
for _, cellversion := range cell{
    ...
}

您应该能够获取特定列的行,然后迭代所有结果:

rowkey := "YOUR_KEY"
row, err := tbl.ReadRow(ctx, rowkey, bigtable.RowFilter(bigtable.ColumnFilter("ColumnQualifier")))
if err != nil {
    log.Fatalf("Could not read row with key %s: %v", rowkey, err)
}

printRow(w, row)

func printRow(w io.Writer, row bigtable.Row) {
    fmt.Fprintf(w, "Reading data for %s:\n", row.Key())
    for columnFamily, cols := range row {
        fmt.Fprintf(w, "Column Family %s\n", columnFamily)
        for _, col := range cols {
            qualifier := col.Column[strings.IndexByte(col.Column, ':')+1:]
            fmt.Fprintf(w, "\t%s: %s @%d\n", qualifier, col.Value, col.Timestamp)
        }
    }
    fmt.Fprintln(w)
}

以上就是《迭代 BigTable 行上的单元格版本》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>