How to check if DynamoDB table exists

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

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?

Should I try to create a new table and wrap it using try catch ?
Mar 14, 2022 in Others by Edureka
• 13,690 points
2,118 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
import boto3
class tableCreate(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
# Query client and list_tables to see if table exists or not
def queryCreate(self):
# Instantiate your dynamo client object
client = boto3.client('dynamodb')
# Get an array of table names associated with the current account and endpoint.
response = client.list_tables()
if 'followers' in response['TableNames']:
table_found = True
else:
table_found = False
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
# Create the DynamoDB table called followers
table = dynamodb.create_table(
TableName ='followers',
KeySchema =
[
{
'AttributeName': 'follower_id',
'KeyType': 'HASH'
}
],
AttributeDefinitions =
[
{
'AttributeName': 'follower_id',
'AttributeType': 'N'
}
],
ProvisionedThroughput =
{
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='followers')
return table_found

According to the documentation, there are three techniques for determining whether or not a table exists.

If the table already exists, the CreateTable API produces a ResourceInUseException exception. To catch this, wrap the create table method in try except.
To receive a list of table names linked with the current account and endpoint, use the ListTables API. Check to see if the table name is in the response's list of table names.
If the table name you request does not exist, the DescribeTable API will return a ResourceNotFoundException.

answered Mar 25, 2022 by Edureka
• 13,690 points

edited Mar 5
0 votes

You could just do this

def checkTableExists(client, tableName): 
    try:
        response = client.describe_table(TableName=tableName)
        return True
    except client.exceptions.ResourceNotFoundException:
        return False

answered Apr 5, 2024 by Mark

edited Mar 5

Related Questions In Others

0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,250 points
6,283 views
0 votes
0 answers

SEO - How to programmatically check if a website has been Banned by Google

i wanted to link a website to ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
454 views
0 votes
0 answers
0 votes
1 answer

How to check if a cell is empty in a range variable?

Use WorksheetFunction.CountA() (https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.counta) function If WorksheetFunction.CountA(rng) = 0 Then ...READ MORE

answered Jan 15, 2023 in Others by narikkadan
• 63,600 points
978 views
0 votes
1 answer

How to check the connected device name in Flutter?

Hi@akhtar, Flutter has its command own command to ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
8,478 views
0 votes
1 answer

How to import data from a HTML table on a website to excel?

Hello  To import any HTML file in excel there ...READ MORE

answered Feb 10, 2022 in Others by gaurav
• 23,260 points
8,299 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,244 views
0 votes
0 answers

How is it possible for DynamoDB to support both Key-Value and Document database properties at the same time

As per DynamoDB's documentation, it supports both ...READ MORE

Apr 5, 2022 in Others by Kichu
• 19,040 points
836 views
0 votes
0 answers
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
15,105 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP