# The client must be created with admin=True because it will create a# table.client=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)connection=happybase.Connection(instance=instance)
print("Creating the {} table.".format(table_name))column_family_name="cf1"connection.create_table(table_name,{column_family_name:dict()}# Use default options.)
print("Writing some greetings to the table.")table=connection.table(table_name)column_name="{fam}:greeting".format(fam=column_family_name)greetings=["Hello World!","Hello Cloud Bigtable!","Hello HappyBase!",]fori,valueinenumerate(greetings):# Note: This example uses sequential numeric IDs for simplicity,# but this can result in poor performance in a production# application. Since rows are stored in sorted order by key,# sequential keys can result in poor distribution of operations# across nodes.## For more information about how to design a Bigtable schema for# the best performance, see the documentation:## https://cloud.google.com/bigtable/docs/schema-designrow_key="greeting{}".format(i)table.put(row_key,{column_name.encode("utf-8"):value.encode("utf-8")})
print("Getting a single greeting by row key.")key="greeting0".encode("utf-8")row=table.row(key)print("\t{}: {}".format(key,row[column_name.encode("utf-8")]))
print("Deleting the {} table.".format(table_name))connection.delete_table(table_name)
综合应用
以下为不包含注释的完整示例。
"""Demonstrates how to connect to Cloud Bigtable and run some basic operations.Prerequisites:- Create a Cloud Bigtable cluster. https://cloud.google.com/bigtable/docs/creating-cluster- Set your Google Application Default Credentials. https://developers.google.com/identity/protocols/application-default-credentials"""importargparsefrom..utilsimportwait_for_tablefromgoogle.cloudimportbigtablefromgoogle.cloudimporthappybasedefmain(project_id,instance_id,table_name):client=bigtable.Client(project=project_id,admin=True)instance=client.instance(instance_id)connection=happybase.Connection(instance=instance)try:print("Creating the {} table.".format(table_name))column_family_name="cf1"connection.create_table(table_name,{column_family_name:dict()}# Use default options.)wait_for_table(instance.table(table_name))print("Writing some greetings to the table.")table=connection.table(table_name)column_name="{fam}:greeting".format(fam=column_family_name)greetings=["Hello World!","Hello Cloud Bigtable!","Hello HappyBase!",]fori,valueinenumerate(greetings):row_key="greeting{}".format(i)table.put(row_key,{column_name.encode("utf-8"):value.encode("utf-8")})print("Getting a single greeting by row key.")key="greeting0".encode("utf-8")row=table.row(key)print("\t{}: {}".format(key,row[column_name.encode("utf-8")]))print("Scanning for all greetings:")forkey,rowintable.scan():print("\t{}: {}".format(key,row[column_name.encode("utf-8")]))finally:print("Deleting the {} table.".format(table_name))connection.delete_table(table_name)connection.close()if__name__=="__main__":parser=argparse.ArgumentParser(description=__doc__,formatter_class=argparse.ArgumentDefaultsHelpFormatter)parser.add_argument("project_id",help="Your Cloud Platform project ID.")parser.add_argument("instance_id",help="ID of the Cloud Bigtable instance to connect to.")parser.add_argument("--table",help="Table to create and destroy.",default="Hello-Bigtable")args=parser.parse_args()main(args.project_id,args.instance_id,args.table)
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThis guide demonstrates a simple "hello world" application using the HappyBase API to interact with Bigtable, showcasing how to set up authentication, connect to an instance, and perform basic table operations.\u003c/p\u003e\n"],["\u003cp\u003eThe HappyBase package, part of the Google Cloud Client Library for Python, is used for communicating with Bigtable, especially beneficial when moving existing HBase workloads.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for a local environment requires installing and initializing the gcloud CLI and setting up Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe application illustrates core functionalities like creating a table with column families, writing data using \u003ccode\u003eTable.put()\u003c/code\u003e, reading data with \u003ccode\u003eTable.row()\u003c/code\u003e and \u003ccode\u003eTable.scan()\u003c/code\u003e, and finally deleting the table.\u003c/p\u003e\n"],["\u003cp\u003eThe sample uses \u003ccode\u003eConnection.create_table()\u003c/code\u003e to create a table and \u003ccode\u003eConnection.delete_table()\u003c/code\u003e to delete a table, and it also shows how to define column families for organizing data.\u003c/p\u003e\n"]]],[],null,[]]