https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.htmlhtml
Creating a New Tablepython
In order to create a new table, use the DynamoDB.ServiceResource.create_table() method:api
建立新的表的方法:ide
import boto3ui
#獲取dynamoDB 服務資源this
dynamodb = boto3.resource('dynamodb')spa
# 建立DynamDB 表htm
使用create_table()方法建立表,對象
而且設置:ci
表名稱:TableName
主鍵屬性: keySchema
table = dynamodb.create_table(
TableName=‘users’,
KeySchema=[
{
'AttrbuteName':'username',
'KeyType':'HASH' #哈希
},
{
'AttrbuteName':'last_name',
'KeyType':'RANGE' #貌似列表
}
],
AttributeDefinitions=[
{
'AttributeName': 'username', 'AttributeType': 'S' }, { 'AttributeName': 'last_name', 'AttributeType': 'S' },
],
ProvisionedThroughput={
'ReadCapacityUnits':5,
'WriteCapacityUnits':5
}
)
# wait until the talble exists.
table.meta.client.get_waiter('table_exists').wait(TableName="users")
#print out some data about the table.
#打印表格數據
print(table.item_count)
這將建立一個名爲users的表,該表分別具備散列和範圍主鍵username和last_name。這個方法將返回一個DynamoDB。表資源來調用已建立表上的其餘方法。
This creates a table named users that respectively has the hash and range primary keys username and last_name. This method will return a DynamoDB.Table resource to call additional methods on the created table.
Using an Existing Table 使用存在的表
It is also possible to create a DynamoDB.Table resource from an existing table:
也能夠建立DynamoDB。來自現有表的表資源:
import boto3
#get the service resource.
dynamodb = boto3.resource('dynamodb')
# Instantiate a table resource object without actually
實例化表資源對象 # creating a DynamoDB table. Note that the attributes of this table
are lazy-loaded: a request is not made nor are the attribute
values populated until the attributes on the table resource are accessed or its load() method is called.
#建立DynamoDB表。注意,此表的屬性是延遲加載的:在訪問表資源上的屬性或調用其load()方法以前,
不會發出請求,也不會填充屬性值。
table = dynamodb.Table('user')
# Print out some data about the table.打印一些關於表格的數據。 # This will cause a request to be made to DynamoDB and its attribute values will be set based on the response.
譯文:這將致使向DynamoDB發出請求,並根據響應設置其屬性值。
print(table.creation_date_time)
Expected Output (Please note that the actual times will probably not match up):
2015-06-26 12:42:45.149000-07:00
譯文:指望輸出(請注意實際時間可能不匹配):
Creating a New Item
Once you have a DynamoDB.Table resource you can add new items to the table using DynamoDB.Table.put_item():
table.put_item(
Item={
'username':'janedoe',
'first_name':'Jane',
'age':25,
'account_type':'standard_user',
}
)
For all of the valid types that can be used for an item, refer to Valid DynamoDB Types.
對於可用於項的全部有效類型,請參考有效的DynamoDB類型。
Valid DynamoDB Types 有效的數據類型:
Python Type
Python Type | DynamoDB Type |
---|---|
string | String (S) |
integer | Number (N) |
decimal.Decimal | Number (N) |
boto3.dynamodb.types.Binary | Binary (B) |
boolean | Boolean (BOOL) |
None | Null (NULL) |
string set | String Set (SS) |
integer set | Number Set (NS) |
decimal.Decimal set | Number Set (NS) |
boto3.dynamodb.types.Binary set | Binary Set (BS) |
list | List (L) |
dict | Map (M) |
未完 待續