使用 Python 在 DynamoDB 上執行操作

莊志炎 發佈 2022-05-28T19:47:24.328748+00:00

如何使用 Python 和 Boto3 庫在 AWS DynamoDB 中創建表、加載數據、執行操作和查詢/掃描表。Amazon DynamoDB (DDB) 是一個 NoSQL 無伺服器資料庫。

如何使用 python 和 Boto3 庫在 AWS dynamodb 中創建表、加載數據、執行操作和查詢/掃描表。

Amazon DynamoDB (DDB) 是一個 NoSQL 無伺服器資料庫。這意味著它是一個鍵值資料庫,可提供靈活的模式,並可輕鬆擴展大量數據和高用戶負載,並可用於按用量付費的定價規模。

在本文中,我們將了解如何使用 Python 和 Boto3 庫在 AWS DynamoDB 中創建表、加載數據、執行操作和查詢/掃描表。

為此,我將使用 Cloud9 作為我的 IDE,您可以選擇使用您喜歡的任何 IDE,我選擇使用 Cloud9。


創建表

我決定創建一個表,命名為:「生日」。該表將具有以下屬性:人名、出生年份、月份和月份中的某天。

在分配分區和排序鍵時,我最初選擇使用屬性名稱和年份。但是,後來在嘗試查詢我的表時發現,這兩個詞都在您不允許用作 DynamoDB 中的屬性的保留詞列表中。我將分區鍵更改為 Person 並將排序鍵更改為 Birthyear 並且能夠避免任何問題。

注意在建表的時候,我們只添加了分區和排序鍵屬性,剩下的屬性我們會在加載數據到表的時候添加。

這是我的代碼的要點:

import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.create_table(
    TableName='Birthdays',
    KeySchema=[
        {
            'AttributeName': 'Person',
            'KeyType': 'HASH'  #Partition key
        },
        {
            'AttributeName': 'Birthyear',
            'KeyType': 'RANGE'  #Sort key
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'Person',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'Birthyear',
            'AttributeType': 'N'
        },
    ],
     ProvisionedThroughput={
        'ReadCapacityUnits': 10,
        'WriteCapacityUnits': 10
    }
)

要運行我提供的任何程序,請使用 Cloud9 中的終端並運行以下命令,Python 後跟文件名。 確保您在當前工作目錄中並使用您的文件名。

python Week15_create_table.py

如果您已正確編寫所有內容,您應該會看到:


加載表中的數據

創建表後,您最可能想要做的下一件事就是加載數據。 這也是我們將添加您希望包含在表中的任何其他屬性的地方。 首先,您需要創建一個單獨的 .json 文件來包含您的數據。

您將重複上面的腳本來添加儘可能多的數據。 為方便起見,請將 .json 文件保存在與 Python 腳本相同的工作目錄中。

要加載數據,您可以運行以下腳本,請注意第 8 行它調用您創建的 .json 文件:

這是我的代碼的要點:

import boto3
import json

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('Birthdays')

with open("Birthday_data.json") as json_file:
    Birthdays = json.load(json_file)
    for Birthday in Birthdays:
        Person = Birthday['Person'] 
        Birthyear = int(Birthday['Birthyear'])
        info = Birthday['info']

        print("Adding person:", Person, Birthyear)

        table.put_item(
           Item={
               'Person': Person,
               'Birthyear': Birthyear,
               'info': info,
            }
        )

運行腳本時,它將列印出分區並對加載的每個項目的鍵進行排序。

從表中刪除項目

要從表中刪除項目,您需要知道要刪除它的時間的分區和排序鍵:

這是代碼的要點:

import boto3

client = boto3.client('dynamodb', region_name='us-east-1')

response = client.delete_item(
    TableName='Birthdays',
    Key={'Person': {'S': 'Dennis #########'}, 'Birthyear': {'N': '1970'}, 
    }
)


掃描表格

要掃描您的表,您只需要知道您的表的名稱:

這是代碼的要點:

import boto3

client = boto3.client('dynamodb', region_name='us-east-1')

response = client.scan(TableName='Birthdays')

print(response)

當您掃描表時,它將如下所示:

要查詢您的表:

為了查詢您的表格,在我的例子中,我正在尋找一個特定人的出生年份,我運行了以下代碼:

這是代碼的要點:

import boto3
from boto3.dynamodb.conditions import Key

client = boto3.client('dynamodb', region_name='us-east-1')

response = client.query(
    TableName='Birthdays',
    ExpressionAttributeValues={
        ':ident':{
            'S': 'Amy ######',
        },
    },
    KeyConditionExpression='Person = :ident',
    ProjectionExpression='Birthyear'
)

print(response['Items'])

程序的結果:

要刪除您的表:

現在要刪除您的表,您只需要表的名稱:

這是我的代碼的要點:

import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('Birthdays')

table.delete()

您可以調整上述代碼以滿足您選擇的表格。 重要的是要注意不能用作屬性的保留字列表,因為它可能會在您開始嘗試查詢數據時給您帶來問題。

關鍵字: