如何检查DynamoDB表是否存在? [英] How to check if DynamoDB table exists?

查看:103
本文介绍了如何检查DynamoDB表是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a new user in boto3 and i'm using DynamoDB.

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

I went through over the DynamoDB api and I couldn't find any method which tell me if a table is already exists.

解决此问题的最佳方法是什么?

What is the best approach dealing this issue?

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

Should I try to create a new table and wrap it using try catch ?

推荐答案

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

From reading the documentation, I can see that there are three methods by which you can check if a table exists.


  1. CreateTable API 引发错误 ResourceInUseException (如果该表已存在)。尝试将create_table方法包装起来,除了捕获

  2. 您可以使用 ListTables API 获取与当前帐户和端点关联的表名列表。检查在响应中获得的表名列表中是否存在表名。

  3. DescribeTable API 将引发错误 ResourceNotFoundException 。 / li>
  1. The CreateTable API throws an error ResourceInUseException if the table already exists. Wrap the create_table method with try except to catch this
  2. You can use the ListTables API to get the list of table names associated with the current account and endpoint. Check if the table name is present in the list of table names you get in the response.
  3. The DescribeTable API will throw an error ResourceNotFoundException if the table name you request doesn't exist.

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

To me, the first option sounds better if you just want to create a table.

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

I see that some people are finding it difficult to catch the exceptions. I will put some code below for you to know how to handle exceptions in 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

这篇关于如何检查DynamoDB表是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆