当使用Amazon Keyspaces sync_table()同步表时,可能会遇到该表不可见的问题。解决方法是使用等待函数等待同步完成后再执行操作,示例代码如下:
import time
import boto3
keyspace_name = 'my_keyspace'
table_name = 'my_table'
client = boto3.client('dynamodb')
response = client.describe_table(
TableName=table_name
)
while response['Table']['TableStatus'] == 'PENDING':
time.sleep(5)
response = client.describe_table(
TableName=table_name
)
if response['Table']['TableStatus'] == 'ACTIVE':
client = boto3.client('dynamodb', region_name='us-west-2')
keyspace_description = client.describe_keyspace(
KeyspaceName=keyspace_name
)
table_list = keyspace_description.get('TableList', [])
for table in table_list:
if table.get('Name') == table_name:
print(f'Table {table_name} synced successfully')
以上代码中,使用describe_table()方法检查表状态,如果表状态为'PENDING”,则等待5秒后再次检查,直到表状态为'ACTIVE”为止。最后使用describe_keyspace()方法检索表列表并检查您要同步的表是否存在。如果存在,则同步成功。