私はboto3の新規ユーザーです。私はDynamoDB
を使用しています。DynamoDBテーブルが存在するかどうかを確認する方法は?
私はDynamoDB APIを経由しましたが、テーブルがすでに存在するかどうかを知る方法は見つかりませんでした。
この問題を解決する最善の方法は何ですか?
新しいテーブルを作成し、try catchを使用してラップする必要がありますか?
私はboto3の新規ユーザーです。私はDynamoDB
を使用しています。DynamoDBテーブルが存在するかどうかを確認する方法は?
私はDynamoDB APIを経由しましたが、テーブルがすでに存在するかどうかを知る方法は見つかりませんでした。
この問題を解決する最善の方法は何ですか?
新しいテーブルを作成し、try catchを使用してラップする必要がありますか?
ドキュメントを読んだことで、テーブルが存在するかどうかを確認できる3つの方法があることがわかりました。
ResourceInUseException
をスローします。試しにcreate_tableメソッドをラップしてこれをキャッチする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 = 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
テーブルの説明テーブルを使用して、テーブルが存在するかどうかを判断できます。
サンプルコード:
from __future__ import print_function # Python 2/3 compatibility
import os
os.environ["TZ"] = "UTC"
import boto3
client = boto3.client('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")
response = client.describe_table(
TableName='Movies'
)
print(response)
表が存在する場合: - : - を
を取得します
あなたはResourceNotFoundException
botocore.errorfactory.ResourceNotFoundException取得します:DescribeTable操作を呼び出すときにエラーが(ResourceNotF oundException)が発生しました: 存在しないテーブル
別の方法: -
Waits until this Table is exists. This method calls DynamoDB.Waiter.table_exists.wait() which polls. DynamoDB.Client.describe_table() every 20 seconds until a successful state is reached. An error is returned after 25 failed checks.
table.wait_until_exists()
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)
ありがとうございます!私は自分の髪の毛が残っていたものを失っていた。 – mmr
任意のboto3テーブルインスタンスオブジェクトの.table_status attrを使用できます。存在する(CREATING、UPDATING、DELETING、ACTIVE)か、例外botocore.exceptions.ClientError: Requested resource not found: Table: <YOUR_TABLE_NAME> not found
をスローします。これらの条件をtry/exceptにラップして、現在のテーブル状態に関する完全な情報を得ることができます。
import boto3
from botocore.exceptions import ClientError
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table('your_table_name_str')
try:
is_table_existing = table.table_status in ("CREATING", "UPDATING",
"DELETING", "ACTIVE")
except ClientError:
is_table_existing = False
print "Table %s doesn't exist." % table.name
私はあなたのコードは好きですが、 'botocore.errorfactory.ResourceNotFoundException'をインポートする方法を理解できません。 'AttributeError: 'module'オブジェクトに「ResourceNotFoundException」という属性がありません。私は 'boto3'と' botocore'を輸入しています。 – anon58192932
@ anon58192932あなたはその例外をインポートする方法を考え出しましたか?私は同じ問題に直面しています。 – Phito
@Phito遅れて申し訳ありません、私は仕事に戻りました。例外を確認する方法については、私が投稿してくれる私の答えを見てください。私が理解する限り直接輸入することはできません。 – anon58192932