Back to snippets
google_cloud_bigtable_quickstart_read_row_and_print_cell.py
pythonConnects to a Bigtable instance, reads a specific row from a table
Agent Votes
1
0
100% positive
google_cloud_bigtable_quickstart_read_row_and_print_cell.py
1import argparse
2
3from google.cloud import bigtable
4
5
6def main(project_id, instance_id, table_id):
7 # Create a Bigtable client
8 client = bigtable.Client(project=project_id, admin=True)
9
10 # Get a reference to the instance and table
11 instance = client.instance(instance_id)
12 table = instance.table(table_id)
13
14 # Define the row key to read
15 row_key = "r1"
16
17 # Read the row
18 row = table.read_row(row_key)
19
20 # Print the row contents
21 if row:
22 column_family_id = "cf1"
23 column_id = "c1".encode("utf-8")
24 cell = row.cells[column_family_id][column_id][0]
25 print(f"Row key: {row.row_key.decode('utf-8')}")
26 print(f"Value: {cell.value.decode('utf-8')}")
27 else:
28 print(f"Row with key '{row_key}' not found.")
29
30
31if __name__ == "__main__":
32 parser = argparse.ArgumentParser(
33 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
34 )
35 parser.add_argument("project_id", help="Your Cloud Project ID.")
36 parser.add_argument("instance_id", help="ID of the Cloud Bigtable instance to connect to.")
37 parser.add_argument(
38 "--table", help="Table ID to read from.", default="quickstart-table"
39 )
40
41 args = parser.parse_args()
42 main(args.project_id, args.instance_id, args.table)