首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检查DynamoDB表是否存在?

如何检查DynamoDB表是否存在?
EN

Stack Overflow用户
提问于 2017-02-27 12:16:04
回答 8查看 78.4K关注 0票数 74

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

我查看了DynamoDB api,找不到任何方法来告诉我表是否已经存在。

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

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

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2017-02-27 13:04:12

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

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

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

编辑:我看到有些人发现很难捕捉到异常。我将在下面放置一些代码,让您了解如何在boto3中处理异常。

示例1

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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
票数 96
EN

Stack Overflow用户

发布于 2017-06-05 15:42:35

代码语言:javascript
运行
复制
import boto3

from botocore.exceptions import ClientError

TABLE_NAME = "myTableName"
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")

table = dynamodb.Table(TABLE_NAME)

try:
    response = client.describe_table(TableName=TABLE_NAME)

except ClientError as ce:
if ce.response['Error']['Code'] == 'ResourceNotFoundException':
    print "Table " + TABLE_NAME + " does not exist. Create the table first and try again."
else:
    print "Unknown exception occurred while querying for the " + TABLE_NAME + " table. Printing full error:"
    pprint.pprint(ce.response)
票数 18
EN

Stack Overflow用户

发布于 2018-11-17 11:10:03

如果您不想使用boto3.client,而只使用boto3.resource,则可以选择其他方法。

代码语言:javascript
运行
复制
import boto3

database = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")    

table_name  = 'MyTable'
table_names = [table.name for table in database.tables.all()]

if table_name in table_names:
    print('table', table_name, 'exists')
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42485616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档