使用 Python 与 Google Cloud Bigtable 进行交互

本文将指导您如何使用 Python 与 Google Cloud Bigtable 进行交互,包括设置身份验证、连接到 Bigtable 实例、创建表、写入数据、读取数据以及删除表。

步骤 1:设置身份验证

在开始使用 Bigtable 之前,您需要设置身份验证。以下是步骤:

  1. 安装 Google Cloud CLI

    bash 复制代码
    # 安装 Google Cloud CLI
    curl https://sdk.cloud.google.com | bash
  2. 初始化 Google Cloud CLI

    bash 复制代码
    # 初始化 Google Cloud CLI
    gcloud init
  3. 设置应用默认凭据

    bash 复制代码
    # 设置应用默认凭据
    gcloud auth application-default login

步骤 2:安装和导入客户端库

  1. 安装必要的 Python 包

    bash 复制代码
    # 安装必要的 Python 包
    pip install google-cloud-bigtable google-cloud-core
  2. 导入模块

    python 复制代码
    from google.cloud import bigtable
    from google.cloud.bigtable.data import row_filters

步骤 3:连接到 Bigtable

使用 bigtable.Client 连接到 Bigtable 实例:

python 复制代码
# 连接到 Bigtable 实例
client = bigtable.data.BigtableDataClientAsync(project="your_project_id")
table = client.get_table("your_instance_id", "your_table_id")

步骤 4:创建表

创建一个新表并设置列族:

python 复制代码
# 创建表
admin_client = bigtable.Client(project="your_project_id", admin=True)
admin_instance = admin_client.instance("your_instance_id")
admin_table = admin_instance.table("your_table_id")

# 创建列族
max_versions_rule = bigtable.column_family.MaxVersionsGCRule(2)
column_family_id = "cf1"
column_families = {column_family_id: max_versions_rule}

if not admin_table.exists():
    admin_table.create(column_families=column_families)
else:
    print("Table already exists.")

步骤 5:写入数据

将数据写入表中:

python 复制代码
# 写入数据
greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
mutations = []
column = "greeting"
for i, value in enumerate(greetings):
    row_key = "greeting{}".format(i).encode()
    row_mutation = bigtable.data.RowMutationEntry(
        row_key, bigtable.data.SetCell(column_family_id, column, value)
    )
    mutations.append(row_mutation)
await table.bulk_mutate_rows(mutations)

步骤 6:读取数据

使用过滤器读取数据:

python 复制代码
# 创建过滤器
row_filter = row_filters.CellsColumnLimitFilter(1)

# 读取单行数据
key = "greeting0".encode()
row = await table.read_row(key, row_filter=row_filter)
cell = row.cells[0]
print(cell.value.decode("utf-8"))

# 扫描所有行
query = bigtable.data.ReadRowsQuery(row_filter=row_filter)
async for row in await table.read_rows_stream(query):
    cell = row.cells[0]
    print(cell.value.decode("utf-8"))

步骤 7:删除表

删除表:

python 复制代码
# 删除表
admin_table.delete()

完整示例

以下是完整的 Python 脚本示例:

python 复制代码
import asyncio
from google.cloud import bigtable
from google.cloud.bigtable.data import row_filters

async def main(project_id, instance_id, table_id):
    client = bigtable.data.BigtableDataClientAsync(project=project_id)
    table = client.get_table(instance_id, table_id)

    admin_client = bigtable.Client(project=project_id, admin=True)
    admin_instance = admin_client.instance(instance_id)
    admin_table = admin_instance.table(table_id)

    # 创建表
    max_versions_rule = bigtable.column_family.MaxVersionsGCRule(2)
    column_family_id = "cf1"
    column_families = {column_family_id: max_versions_rule}
    if not admin_table.exists():
        admin_table.create(column_families=column_families)
    else:
        print("Table already exists.")

    try:
        # 写入数据
        greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
        mutations = []
        column = "greeting"
        for i, value in enumerate(greetings):
            row_key = "greeting{}".format(i).encode()
            row_mutation = bigtable.data.RowMutationEntry(
                row_key, bigtable.data.SetCell(column_family_id, column, value)
            )
            mutations.append(row_mutation)
        await table.bulk_mutate_rows(mutations)

        # 创建过滤器
        row_filter = row_filters.CellsColumnLimitFilter(1)

        # 读取单行数据
        key = "greeting0".encode()
        row = await table.read_row(key, row_filter=row_filter)
        cell = row.cells[0]
        print(cell.value.decode("utf-8"))

        # 扫描所有行
        query = bigtable.data.ReadRowsQuery(row_filter=row_filter)
        async for row in await table.read_rows_stream(query):
            cell = row.cells[0]
            print(cell.value.decode("utf-8"))
    finally:
        # 删除表
        admin_table.delete()

if __name__ == "__main__":
    project_id = "your_project_id"
    instance_id = "your_instance_id"
    table_id = "your_table_id"
    asyncio.run(main(project_id, instance_id, table_id))

总结

本文提供了使用 Python 与 Google Cloud Bigtable 进行交互的基本步骤,包括设置身份验证、连接到 Bigtable、创建表、写入数据、读取数据以及删除表。通过这些步骤,您可以轻松地使用 Bigtable 进行数据存储和管理。

相关推荐
草梅友仁1 小时前
草梅 Auth 1.1.0 发布与最新动态 | 2025 年第 30 周草梅周报
开源·github·ai编程
武子康1 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
PAK向日葵2 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
舒一笑2 小时前
我的开源项目-PandaCoder迎来史诗级大更新啦
后端·程序员·intellij idea
mortimer3 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github
@昵称不存在3 小时前
Flask input 和datalist结合
后端·python·flask
zhuyasen3 小时前
Go 分布式任务和定时任务太难?sasynq 让异步任务从未如此简单
后端·go
东林牧之4 小时前
Django+celery异步:拿来即用,可移植性高
后端·python·django
超浪的晨4 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
AntBlack5 小时前
从小不学好 ,影刀 + ddddocr 实现图片验证码认证自动化
后端·python·计算机视觉