|
| 1 | +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | +# use this file except in compliance with the License. You may obtain a copy of |
| 5 | +# the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations under |
| 13 | +# the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Stub Test""" |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import time |
| 20 | +import shutil |
| 21 | +import datetime |
| 22 | +import tempfile |
| 23 | +import numpy as np |
| 24 | +import pytest |
| 25 | + |
| 26 | +import tensorflow as tf |
| 27 | +import tensorflow_io as tfio |
| 28 | + |
| 29 | + |
| 30 | +def bigtable_func(project_id, instance_id, table_id): |
| 31 | + from google.cloud import bigtable |
| 32 | + from google.cloud.bigtable import column_family |
| 33 | + from google.cloud.bigtable import row_filters |
| 34 | + from google.auth.credentials import AnonymousCredentials |
| 35 | + |
| 36 | + os.environ["BIGTABLE_EMULATOR_HOST"] = "localhost:8086" |
| 37 | + |
| 38 | + # [START bigtable_hw_connect] |
| 39 | + # The client must be created with admin=True because it will create a |
| 40 | + # table. |
| 41 | + client = bigtable.Client( |
| 42 | + project=project_id, admin=True, credentials=AnonymousCredentials() |
| 43 | + ) |
| 44 | + instance = client.instance(instance_id) |
| 45 | + # [END bigtable_hw_connect] |
| 46 | + |
| 47 | + # [START bigtable_hw_create_table] |
| 48 | + print("Creating the {} table.".format(table_id)) |
| 49 | + table = instance.table(table_id) |
| 50 | + |
| 51 | + print("Creating column family cf1 with Max Version GC rule...") |
| 52 | + # Create a column family with GC policy : most recent N versions |
| 53 | + # Define the GC policy to retain only the most recent 2 versions |
| 54 | + max_versions_rule = column_family.MaxVersionsGCRule(2) |
| 55 | + column_family_id = "cf1" |
| 56 | + column_families = {column_family_id: max_versions_rule} |
| 57 | + if not table.exists(): |
| 58 | + table.create(column_families=column_families) |
| 59 | + else: |
| 60 | + print("Table {} already exists.".format(table_id)) |
| 61 | + # [END bigtable_hw_create_table] |
| 62 | + |
| 63 | + # [START bigtable_hw_write_rows] |
| 64 | + print("Writing some greetings to the table.") |
| 65 | + greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"] |
| 66 | + rows = [] |
| 67 | + column = b"greeting" |
| 68 | + for i, value in enumerate(greetings): |
| 69 | + # Note: This example uses sequential numeric IDs for simplicity, |
| 70 | + # but this can result in poor performance in a production |
| 71 | + # application. Since rows are stored in sorted order by key, |
| 72 | + # sequential keys can result in poor distribution of operations |
| 73 | + # across nodes. |
| 74 | + # |
| 75 | + # For more information about how to design a Bigtable schema for |
| 76 | + # the best performance, see the documentation: |
| 77 | + # |
| 78 | + # https://cloud.google.com/bigtable/docs/schema-design |
| 79 | + row_key = "greeting{}".format(i).encode() |
| 80 | + row = table.direct_row(row_key) |
| 81 | + row.set_cell( |
| 82 | + column_family_id, column, value, timestamp=datetime.datetime.utcnow() |
| 83 | + ) |
| 84 | + rows.append(row) |
| 85 | + table.mutate_rows(rows) |
| 86 | + # [END bigtable_hw_write_rows] |
| 87 | + |
| 88 | + # [START bigtable_hw_create_filter] |
| 89 | + # Create a filter to only retrieve the most recent version of the cell |
| 90 | + # for each column accross entire row. |
| 91 | + row_filter = row_filters.CellsColumnLimitFilter(1) |
| 92 | + # [END bigtable_hw_create_filter] |
| 93 | + |
| 94 | + # [START bigtable_hw_get_with_filter] |
| 95 | + print("Getting a single greeting by row key.") |
| 96 | + key = b"greeting0" |
| 97 | + |
| 98 | + row = table.read_row(key, row_filter) |
| 99 | + cell = row.cells[column_family_id][column][0] |
| 100 | + print(cell.value.decode("utf-8")) |
| 101 | + # [END bigtable_hw_get_with_filter] |
| 102 | + |
| 103 | + # [START bigtable_hw_scan_with_filter] |
| 104 | + print("Scanning for all greetings:") |
| 105 | + partial_rows = table.read_rows(filter_=row_filter) |
| 106 | + |
| 107 | + for row in partial_rows: |
| 108 | + cell = row.cells[column_family_id][column][0] |
| 109 | + print(cell.value.decode("utf-8")) |
| 110 | + # [END bigtable_hw_scan_with_filter] |
| 111 | + |
| 112 | + # [START bigtable_hw_delete_table] |
| 113 | + print("Deleting the {} table.".format(table_id)) |
| 114 | + table.delete() |
| 115 | + # [END bigtable_hw_delete_table] |
| 116 | + |
| 117 | + |
| 118 | +def test_bigtable(): |
| 119 | + bigtable_func("bigtable_project", "bigtable_instance", "bigtable_table") |
0 commit comments