<i id='QXYDR'><tr id='QXYDR'><dt id='QXYDR'><q id='QXYDR'><span id='QXYDR'><b id='QXYDR'><form id='QXYDR'><ins id='QXYDR'></ins><ul id='QXYDR'></ul><sub id='QXYDR'></sub></form><legend id='QXYDR'></legend><bdo id='QXYDR'><pre id='QXYDR'><center id='QXYDR'></center></pre></bdo></b><th id='QXYDR'></th></span></q></dt></tr></i><div id='QXYDR'><tfoot id='QXYDR'></tfoot><dl id='QXYDR'><fieldset id='QXYDR'></fieldset></dl></div>
  • <small id='QXYDR'></small><noframes id='QXYDR'>

      <bdo id='QXYDR'></bdo><ul id='QXYDR'></ul>
      1. <tfoot id='QXYDR'></tfoot>
        <legend id='QXYDR'><style id='QXYDR'><dir id='QXYDR'><q id='QXYDR'></q></dir></style></legend>

        如何检查 DynamoDB 表是否存在?

        时间:2023-07-05
      2. <tfoot id='bg5NT'></tfoot>
          <i id='bg5NT'><tr id='bg5NT'><dt id='bg5NT'><q id='bg5NT'><span id='bg5NT'><b id='bg5NT'><form id='bg5NT'><ins id='bg5NT'></ins><ul id='bg5NT'></ul><sub id='bg5NT'></sub></form><legend id='bg5NT'></legend><bdo id='bg5NT'><pre id='bg5NT'><center id='bg5NT'></center></pre></bdo></b><th id='bg5NT'></th></span></q></dt></tr></i><div id='bg5NT'><tfoot id='bg5NT'></tfoot><dl id='bg5NT'><fieldset id='bg5NT'></fieldset></dl></div>
            <legend id='bg5NT'><style id='bg5NT'><dir id='bg5NT'><q id='bg5NT'></q></dir></style></legend>
                <bdo id='bg5NT'></bdo><ul id='bg5NT'></ul>

                    <tbody id='bg5NT'></tbody>

                1. <small id='bg5NT'></small><noframes id='bg5NT'>

                2. 本文介绍了如何检查 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 如果表已经存在.用 try 包裹 create_table 方法来捕捉它
                  2. 您可以使用 ListTables API 获取与当前帐户和端点关联的表名列表.检查您在响应中获得的表名列表中是否存在该表名.
                  3. DescribeTable API 会抛出错误ResourceNotFoundException 如果您请求的表名不存在.
                  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 表是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将 boto3 Dynamo DB 项转换为 Python 中的常规字典? 下一篇:如何使用 aws Lambda 和 python 将项目放入 aws DynamoDb

                  相关文章

                    <i id='AiGWw'><tr id='AiGWw'><dt id='AiGWw'><q id='AiGWw'><span id='AiGWw'><b id='AiGWw'><form id='AiGWw'><ins id='AiGWw'></ins><ul id='AiGWw'></ul><sub id='AiGWw'></sub></form><legend id='AiGWw'></legend><bdo id='AiGWw'><pre id='AiGWw'><center id='AiGWw'></center></pre></bdo></b><th id='AiGWw'></th></span></q></dt></tr></i><div id='AiGWw'><tfoot id='AiGWw'></tfoot><dl id='AiGWw'><fieldset id='AiGWw'></fieldset></dl></div>
                      <bdo id='AiGWw'></bdo><ul id='AiGWw'></ul>
                    <legend id='AiGWw'><style id='AiGWw'><dir id='AiGWw'><q id='AiGWw'></q></dir></style></legend>
                    <tfoot id='AiGWw'></tfoot>

                    <small id='AiGWw'></small><noframes id='AiGWw'>