使用 DynamoDB API,您可以使用 PutItem
操作向表添加项目。
{
TableName: "Music",
Item: {
"Artist":"No One You Know",
"SongTitle":"Call Me Today",
"AlbumTitle":"Somewhat Famous",
"Year": 2015,
"Price": 2.14,
"Genre": "Country",
"Tags": {
"Composers": [
"Smith",
"Jones",
"Davis"
],
"LengthInSeconds": 214
}
}
}
此表的主键包含 Artist 和 SongTitle。您必须为这些属性指定值。
以下是要了解的有关此 PutItem
示例的几个关键事项:
-
DynamoDB 使用 JSON 提供对文档的原生支持。这使得 DynamoDB 非常适合存储半结构化数据,例如标记。您也可以从 JSON 文档中检索和操作数据。
-
除了主键(Artist 和 SongTitle)外,Music 表没有任何预定义属性。
-
大多数 SQL 数据库是面向事务的。当您发出 INSERT
语句时,数据修改不是永久性的,直至您发出 COMMIT
语句。利用 Amazon DynamoDB,当 DynamoDB 回复 HTTP 200 状态代码(OK
)时,PutItem
操作的效果是永久性的。
以下是其他几个 PutItem
示例。
{
TableName: "Music",
Item: {
"Artist": "No One You Know",
"SongTitle": "My Dog Spot",
"AlbumTitle":"Hey Now",
"Price": 1.98,
"Genre": "Country",
"CriticRating": 8.4
}
}
{
TableName: "Music",
Item: {
"Artist": "No One You Know",
"SongTitle": "Somewhere Down The Road",
"AlbumTitle":"Somewhat Famous",
"Genre": "Country",
"CriticRating": 8.4,
"Year": 1984
}
}
{
TableName: "Music",
Item: {
"Artist": "The Acme Band",
"SongTitle": "Still In Love",
"AlbumTitle":"The Buck Starts Here",
"Price": 2.47,
"Genre": "Rock",
"PromotionInfo": {
"RadioStationsPlaying":[
"KHCR", "KBQX", "WTNR", "WJJH"
],
"TourDates": {
"Seattle": "20150625",
"Cleveland": "20150630"
},
"Rotation": "Heavy"
}
}
}
{
TableName: "Music",
Item: {
"Artist": "The Acme Band",
"SongTitle": "Look Out, World",
"AlbumTitle":"The Buck Starts Here",
"Price": 0.99,
"Genre": "Rock"
}
}
除了 PutItem
之外,DynamoDB 还支持使用 BatchWriteItem
操作同时写入多个项目。