【问题标题】:Best way to get an entire row, where a certain column = certain value?获取整行的最佳方法,其中某列 = 某个值?
【发布时间】:2021-01-20 08:31:58
【问题描述】:

我想查询某列等于某个值的整行数据。

chain1 = row_filters.RowFilterChain(
    filters=[
        row_filters.ColumnQualifierRegexFilter("VOLTAGE SCREW CONVEYOR DRIVE"),
        row_filters.ValueRangeFilter(b"0.693", b"1")
    ]
)

这是我的过滤器,但这只会获取 1 个单元格。我想得到整行。任何人都知道如何实现这一点的任何主要技巧?

我设法通过两次调用 Bigtable 来实现它,但我认为它根本没有效率。

chain1 = row_filters.RowFilterChain(
    filters=[
        row_filters.ColumnQualifierRegexFilter("VOLTAGE SCREW CONVEYOR DRIVE"),
        row_filters.ValueRangeFilter(b"0.693", b"1"),
        row_filters.StripValueTransformerFilter(True)
    ]
)

rows = table.read_rows(row_set=row_set, 
                        filter_=chain1)

# Get the rowkeys
rowkeys = [row.row_key for row in rows]

# Get the rows for these rowkeys
row_set = RowSet()
for rk in rowkeys:
    row_set.add_row_key(rk)

# Final soln
rows = table.read_rows(row_set=row_set)

【问题讨论】:

    标签: google-cloud-bigtable bigtable


    【解决方案1】:

    您可以使用conditional row filter,它将一个基本过滤器与一个真过滤器和一个假过滤器结合起来。如果基本过滤器发出任何单元格,则使用真过滤器,否则使用错误过滤器。按照你的例子,它会是这样的

    condition = RowFilterChain(
        filters=[
            ColumnQualifierRegexFilter("VOLTAGE SCREW CONVEYOR DRIVE"),
            ValueRangeFilter(b"0.693", b"1"),
            CellsPerRow(1), // since we only need one cell to trigger the true filter
        ]
    )
    
    conditionalFilter = row_filters.ConditionalRowFilter(
        base_filter = condition,
        true_filter = PassAllFilter(), // this way you get the whole row and not just the single cell value
        false_filter = BlockAllFilter()
    )
    

    【讨论】:

      【解决方案2】:

      您无法使用列过滤器从 read_rows 中获取所有列。 请参阅 read_rows() 的文档:

      filter (RowFilter) – (可选)应用于指定行内容的过滤器。如果未设置,则读取每一行中的每一列。

      https://googleapis.dev/python/bigtable/latest/table.html

      【讨论】:

        【解决方案3】:

        最有效的方法可能是围绕这个问题为您的表建模。 BT 不是为数据模型的灵活性而设计的——它不是 SQL。为了快速、高效地读取,不要依赖行内的条件。您应该考虑在行键中添加该值或生成一个所有数据都满足您的条件的专用表(从而消除条件)。使用过滤器会损害性能)。

        引用谷歌的话: 特别是条件过滤器会增加延迟,因为条件比其他过滤器慢得多。如果您的读取请求对性能极为敏感,请不要在请求中使用条件。 (见https://cloud.google.com/bigtable/docs/filters

        【讨论】:

          猜你喜欢
          • 2023-03-26
          • 1970-01-01
          • 2020-05-02
          • 2013-01-24
          • 2021-09-14
          • 1970-01-01
          • 2018-09-12
          • 1970-01-01
          • 2014-04-15
          相关资源
          最近更新 更多