python - 如何检查 DynamoDB 表是否存在?

标签 python amazon-dynamodb boto3

我是 boto3 的新用户,我正在使用 DynamoDB

我浏览了 DynamoDB api,但找不到任何方法可以告诉我表是否已经存在。

处理此问题的最佳方法是什么?

我应该尝试创建一个新表并使用 try catch 包装它吗?

最佳答案

通过阅读文档,我可以看到有三种方法可以检查表是否存在。

  1. CreateTable API如果表已经存在,则抛出错误 ResourceInUseException。用 try 包裹 create_table 方法来捕捉它
  2. 您可以使用 ListTables API获取与当前帐户和端点关联的表名列表。检查您在响应中获得的表名列表中是否存在该表名。
  3. DescribeTable API如果您请求的表名不存在,将抛出错误 ResourceNotFoundException

对我来说,如果您只想创建一个表,第一个选项听起来更好。

编辑: 我看到有些人发现很难捕捉到异常。我会在下面放一些代码让你知道如何处理boto3中的异常。

示例 1

import boto3

dynamodb_client = boto3.client('dynamodb')

try:
    response = dynamodb_client.create_table(
        AttributeDefinitions=[
            {
                'AttributeName': 'Artist',
                'AttributeType': 'S',
            },
            {
                'AttributeName': 'SongTitle',
                'AttributeType': 'S',
            },
        ],
        KeySchema=[
            {
                'AttributeName': 'Artist',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'SongTitle',
                'KeyType': 'RANGE',
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5,
        },
        TableName='test',
    )
except dynamodb_client.exceptions.ResourceInUseException:
    # do something here as you require
    pass

示例 2

import boto3

dynamodb_client = boto3.client('dynamodb')


table_name = 'test'
existing_tables = dynamodb_client.list_tables()['TableNames']

if table_name not in existing_tables:
    response = dynamodb_client.create_table(
        AttributeDefinitions=[
            {
                'AttributeName': 'Artist',
                'AttributeType': 'S',
            },
            {
                'AttributeName': 'SongTitle',
                'AttributeType': 'S',
            },
        ],
        KeySchema=[
            {
                'AttributeName': 'Artist',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'SongTitle',
                'KeyType': 'RANGE',
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5,
        },
        TableName=table_name,
    )

示例 3

import boto3

dynamodb_client = boto3.client('dynamodb')

try:
    response = dynamodb_client.describe_table(TableName='test')
except dynamodb_client.exceptions.ResourceNotFoundException:
    # do something here as you require
    pass

关于python - 如何检查 DynamoDB 表是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42485616/

相关文章:

python - 如何检测碰撞? pygame

python - keras 是基于 python 中的闭包吗?

amazon-web-services - AWS 云形成 : Is it possible to Update Stack to recreate DB resources without data loss

python - 博托 + Python + AWS S3 : How to get last_modified attribute of specific file?

python - 如何检查我上传的文件是否为 "multipart"?

django - 确认设备 : Invalid device key given 上的 AWS Cognito Boto3 错误

python - 获取给定图形的标题

python - 为行组填充空值

javascript - 使用 Javascript 将 DynamodB 日期格式字符串转换为日期格式

java - 无法在 Spark Executor 中创建 DynamoDB 客户端