|
| 1 | +# Copyright 2024, Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import datetime |
| 15 | +from google.cloud.bigtable.data import Row |
| 16 | +from google.cloud._helpers import _datetime_from_microseconds |
| 17 | + |
| 18 | + |
| 19 | +# [START bigtable_filters_limit_row_sample_asyncio] |
| 20 | +async def filter_limit_row_sample(project_id, instance_id, table_id): |
| 21 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 22 | + from google.cloud.bigtable.data import row_filters |
| 23 | + |
| 24 | + query = ReadRowsQuery(row_filter=row_filters.RowSampleFilter(0.75)) |
| 25 | + |
| 26 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 27 | + async with client.get_table(instance_id, table_id) as table: |
| 28 | + for row in await table.read_rows(query): |
| 29 | + print_row(row) |
| 30 | + |
| 31 | + |
| 32 | +# [END bigtable_filters_limit_row_sample_asyncio] |
| 33 | +# [START bigtable_filters_limit_row_regex_asyncio] |
| 34 | +async def filter_limit_row_regex(project_id, instance_id, table_id): |
| 35 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 36 | + from google.cloud.bigtable.data import row_filters |
| 37 | + |
| 38 | + query = ReadRowsQuery( |
| 39 | + row_filter=row_filters.RowKeyRegexFilter(".*#20190501$".encode("utf-8")) |
| 40 | + ) |
| 41 | + |
| 42 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 43 | + async with client.get_table(instance_id, table_id) as table: |
| 44 | + for row in await table.read_rows(query): |
| 45 | + print_row(row) |
| 46 | + |
| 47 | + |
| 48 | +# [END bigtable_filters_limit_row_regex_asyncio] |
| 49 | +# [START bigtable_filters_limit_cells_per_col_asyncio] |
| 50 | +async def filter_limit_cells_per_col(project_id, instance_id, table_id): |
| 51 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 52 | + from google.cloud.bigtable.data import row_filters |
| 53 | + |
| 54 | + query = ReadRowsQuery(row_filter=row_filters.CellsColumnLimitFilter(2)) |
| 55 | + |
| 56 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 57 | + async with client.get_table(instance_id, table_id) as table: |
| 58 | + for row in await table.read_rows(query): |
| 59 | + print_row(row) |
| 60 | + |
| 61 | + |
| 62 | +# [END bigtable_filters_limit_cells_per_col_asyncio] |
| 63 | +# [START bigtable_filters_limit_cells_per_row_asyncio] |
| 64 | +async def filter_limit_cells_per_row(project_id, instance_id, table_id): |
| 65 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 66 | + from google.cloud.bigtable.data import row_filters |
| 67 | + |
| 68 | + query = ReadRowsQuery(row_filter=row_filters.CellsRowLimitFilter(2)) |
| 69 | + |
| 70 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 71 | + async with client.get_table(instance_id, table_id) as table: |
| 72 | + for row in await table.read_rows(query): |
| 73 | + print_row(row) |
| 74 | + |
| 75 | + |
| 76 | +# [END bigtable_filters_limit_cells_per_row_asyncio] |
| 77 | +# [START bigtable_filters_limit_cells_per_row_offset_asyncio] |
| 78 | +async def filter_limit_cells_per_row_offset(project_id, instance_id, table_id): |
| 79 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 80 | + from google.cloud.bigtable.data import row_filters |
| 81 | + |
| 82 | + query = ReadRowsQuery(row_filter=row_filters.CellsRowOffsetFilter(2)) |
| 83 | + |
| 84 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 85 | + async with client.get_table(instance_id, table_id) as table: |
| 86 | + for row in await table.read_rows(query): |
| 87 | + print_row(row) |
| 88 | + |
| 89 | + |
| 90 | +# [END bigtable_filters_limit_cells_per_row_offset_asyncio] |
| 91 | +# [START bigtable_filters_limit_col_family_regex_asyncio] |
| 92 | +async def filter_limit_col_family_regex(project_id, instance_id, table_id): |
| 93 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 94 | + from google.cloud.bigtable.data import row_filters |
| 95 | + |
| 96 | + query = ReadRowsQuery( |
| 97 | + row_filter=row_filters.FamilyNameRegexFilter("stats_.*$".encode("utf-8")) |
| 98 | + ) |
| 99 | + |
| 100 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 101 | + async with client.get_table(instance_id, table_id) as table: |
| 102 | + for row in await table.read_rows(query): |
| 103 | + print_row(row) |
| 104 | + |
| 105 | + |
| 106 | +# [END bigtable_filters_limit_col_family_regex_asyncio] |
| 107 | +# [START bigtable_filters_limit_col_qualifier_regex_asyncio] |
| 108 | +async def filter_limit_col_qualifier_regex(project_id, instance_id, table_id): |
| 109 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 110 | + from google.cloud.bigtable.data import row_filters |
| 111 | + |
| 112 | + query = ReadRowsQuery( |
| 113 | + row_filter=row_filters.ColumnQualifierRegexFilter( |
| 114 | + "connected_.*$".encode("utf-8") |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 119 | + async with client.get_table(instance_id, table_id) as table: |
| 120 | + for row in await table.read_rows(query): |
| 121 | + print_row(row) |
| 122 | + |
| 123 | + |
| 124 | +# [END bigtable_filters_limit_col_qualifier_regex_asyncio] |
| 125 | +# [START bigtable_filters_limit_col_range_asyncio] |
| 126 | +async def filter_limit_col_range(project_id, instance_id, table_id): |
| 127 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 128 | + from google.cloud.bigtable.data import row_filters |
| 129 | + |
| 130 | + query = ReadRowsQuery( |
| 131 | + row_filter=row_filters.ColumnRangeFilter( |
| 132 | + "cell_plan", b"data_plan_01gb", b"data_plan_10gb", inclusive_end=False |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 137 | + async with client.get_table(instance_id, table_id) as table: |
| 138 | + for row in await table.read_rows(query): |
| 139 | + print_row(row) |
| 140 | + |
| 141 | + |
| 142 | +# [END bigtable_filters_limit_col_range_asyncio] |
| 143 | +# [START bigtable_filters_limit_value_range_asyncio] |
| 144 | +async def filter_limit_value_range(project_id, instance_id, table_id): |
| 145 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 146 | + from google.cloud.bigtable.data import row_filters |
| 147 | + |
| 148 | + query = ReadRowsQuery( |
| 149 | + row_filter=row_filters.ValueRangeFilter(b"PQ2A.190405", b"PQ2A.190406") |
| 150 | + ) |
| 151 | + |
| 152 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 153 | + async with client.get_table(instance_id, table_id) as table: |
| 154 | + for row in await table.read_rows(query): |
| 155 | + print_row(row) |
| 156 | + |
| 157 | + |
| 158 | +# [END bigtable_filters_limit_value_range_asyncio] |
| 159 | +# [START bigtable_filters_limit_value_regex_asyncio] |
| 160 | + |
| 161 | + |
| 162 | +async def filter_limit_value_regex(project_id, instance_id, table_id): |
| 163 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 164 | + from google.cloud.bigtable.data import row_filters |
| 165 | + |
| 166 | + query = ReadRowsQuery( |
| 167 | + row_filter=row_filters.ValueRegexFilter("PQ2A.*$".encode("utf-8")) |
| 168 | + ) |
| 169 | + |
| 170 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 171 | + async with client.get_table(instance_id, table_id) as table: |
| 172 | + for row in await table.read_rows(query): |
| 173 | + print_row(row) |
| 174 | + |
| 175 | + |
| 176 | +# [END bigtable_filters_limit_value_regex_asyncio] |
| 177 | +# [START bigtable_filters_limit_timestamp_range_asyncio] |
| 178 | +async def filter_limit_timestamp_range(project_id, instance_id, table_id): |
| 179 | + import datetime |
| 180 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 181 | + from google.cloud.bigtable.data import row_filters |
| 182 | + |
| 183 | + end = datetime.datetime(2019, 5, 1) |
| 184 | + |
| 185 | + query = ReadRowsQuery(row_filter=row_filters.TimestampRangeFilter(end=end)) |
| 186 | + |
| 187 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 188 | + async with client.get_table(instance_id, table_id) as table: |
| 189 | + for row in await table.read_rows(query): |
| 190 | + print_row(row) |
| 191 | + |
| 192 | + |
| 193 | +# [END bigtable_filters_limit_timestamp_range_asyncio] |
| 194 | +# [START bigtable_filters_limit_block_all_asyncio] |
| 195 | +async def filter_limit_block_all(project_id, instance_id, table_id): |
| 196 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 197 | + from google.cloud.bigtable.data import row_filters |
| 198 | + |
| 199 | + query = ReadRowsQuery(row_filter=row_filters.BlockAllFilter(True)) |
| 200 | + |
| 201 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 202 | + async with client.get_table(instance_id, table_id) as table: |
| 203 | + for row in await table.read_rows(query): |
| 204 | + print_row(row) |
| 205 | + |
| 206 | + |
| 207 | +# [END bigtable_filters_limit_block_all_asyncio] |
| 208 | +# [START bigtable_filters_limit_pass_all_asyncio] |
| 209 | +async def filter_limit_pass_all(project_id, instance_id, table_id): |
| 210 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 211 | + from google.cloud.bigtable.data import row_filters |
| 212 | + |
| 213 | + query = ReadRowsQuery(row_filter=row_filters.PassAllFilter(True)) |
| 214 | + |
| 215 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 216 | + async with client.get_table(instance_id, table_id) as table: |
| 217 | + for row in await table.read_rows(query): |
| 218 | + print_row(row) |
| 219 | + |
| 220 | + |
| 221 | +# [END bigtable_filters_limit_pass_all_asyncio] |
| 222 | +# [START bigtable_filters_modify_strip_value_asyncio] |
| 223 | +async def filter_modify_strip_value(project_id, instance_id, table_id): |
| 224 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 225 | + from google.cloud.bigtable.data import row_filters |
| 226 | + |
| 227 | + query = ReadRowsQuery(row_filter=row_filters.StripValueTransformerFilter(True)) |
| 228 | + |
| 229 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 230 | + async with client.get_table(instance_id, table_id) as table: |
| 231 | + for row in await table.read_rows(query): |
| 232 | + print_row(row) |
| 233 | + |
| 234 | + |
| 235 | +# [END bigtable_filters_modify_strip_value_asyncio] |
| 236 | +# [START bigtable_filters_modify_apply_label_asyncio] |
| 237 | +async def filter_modify_apply_label(project_id, instance_id, table_id): |
| 238 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 239 | + from google.cloud.bigtable.data import row_filters |
| 240 | + |
| 241 | + query = ReadRowsQuery(row_filter=row_filters.ApplyLabelFilter(label="labelled")) |
| 242 | + |
| 243 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 244 | + async with client.get_table(instance_id, table_id) as table: |
| 245 | + for row in await table.read_rows(query): |
| 246 | + print_row(row) |
| 247 | + |
| 248 | + |
| 249 | +# [END bigtable_filters_modify_apply_label_asyncio] |
| 250 | +# [START bigtable_filters_composing_chain_asyncio] |
| 251 | +async def filter_composing_chain(project_id, instance_id, table_id): |
| 252 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 253 | + from google.cloud.bigtable.data import row_filters |
| 254 | + |
| 255 | + query = ReadRowsQuery( |
| 256 | + row_filter=row_filters.RowFilterChain( |
| 257 | + filters=[ |
| 258 | + row_filters.CellsColumnLimitFilter(1), |
| 259 | + row_filters.FamilyNameRegexFilter("cell_plan"), |
| 260 | + ] |
| 261 | + ) |
| 262 | + ) |
| 263 | + |
| 264 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 265 | + async with client.get_table(instance_id, table_id) as table: |
| 266 | + for row in await table.read_rows(query): |
| 267 | + print_row(row) |
| 268 | + |
| 269 | + |
| 270 | +# [END bigtable_filters_composing_chain_asyncio] |
| 271 | +# [START bigtable_filters_composing_interleave_asyncio] |
| 272 | +async def filter_composing_interleave(project_id, instance_id, table_id): |
| 273 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 274 | + from google.cloud.bigtable.data import row_filters |
| 275 | + |
| 276 | + query = ReadRowsQuery( |
| 277 | + row_filter=row_filters.RowFilterUnion( |
| 278 | + filters=[ |
| 279 | + row_filters.ValueRegexFilter("true"), |
| 280 | + row_filters.ColumnQualifierRegexFilter("os_build"), |
| 281 | + ] |
| 282 | + ) |
| 283 | + ) |
| 284 | + |
| 285 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 286 | + async with client.get_table(instance_id, table_id) as table: |
| 287 | + for row in await table.read_rows(query): |
| 288 | + print_row(row) |
| 289 | + |
| 290 | + |
| 291 | +# [END bigtable_filters_composing_interleave_asyncio] |
| 292 | +# [START bigtable_filters_composing_condition_asyncio] |
| 293 | +async def filter_composing_condition(project_id, instance_id, table_id): |
| 294 | + from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery |
| 295 | + from google.cloud.bigtable.data import row_filters |
| 296 | + |
| 297 | + query = ReadRowsQuery( |
| 298 | + row_filter=row_filters.ConditionalRowFilter( |
| 299 | + predicate_filter=row_filters.RowFilterChain( |
| 300 | + filters=[ |
| 301 | + row_filters.ColumnQualifierRegexFilter("data_plan_10gb"), |
| 302 | + row_filters.ValueRegexFilter("true"), |
| 303 | + ] |
| 304 | + ), |
| 305 | + true_filter=row_filters.ApplyLabelFilter(label="passed-filter"), |
| 306 | + false_filter=row_filters.ApplyLabelFilter(label="filtered-out"), |
| 307 | + ) |
| 308 | + ) |
| 309 | + |
| 310 | + async with BigtableDataClientAsync(project=project_id) as client: |
| 311 | + async with client.get_table(instance_id, table_id) as table: |
| 312 | + for row in await table.read_rows(query): |
| 313 | + print_row(row) |
| 314 | + |
| 315 | + |
| 316 | +# [END bigtable_filters_composing_condition_asyncio] |
| 317 | +# [END_EXCLUDE] |
| 318 | + |
| 319 | + |
| 320 | +def print_row(row: Row): |
| 321 | + print("Reading data for {}:".format(row.row_key.decode("utf-8"))) |
| 322 | + last_family = None |
| 323 | + for cell in row.cells: |
| 324 | + if last_family != cell.family: |
| 325 | + print("Column Family {}".format(cell.family)) |
| 326 | + last_family = cell.family |
| 327 | + |
| 328 | + labels = " [{}]".format(",".join(cell.labels)) if len(cell.labels) else "" |
| 329 | + print( |
| 330 | + "\t{}: {} @{}{}".format( |
| 331 | + cell.qualifier.decode("utf-8"), |
| 332 | + cell.value.decode("utf-8"), |
| 333 | + _datetime_from_microseconds(cell.timestamp_micros), |
| 334 | + labels, |
| 335 | + ) |
| 336 | + ) |
| 337 | + print("") |
0 commit comments