AWS Tools for PowerShell 版本 5(V5)已经发布!
要开始使用新版本的工具,请参阅《AWS Tools for PowerShell 用户指南(V5)》,特别是关于迁移到 V5 的主题。
使用 Tools for PowerShell V4 的 DynamoDB 示例
以下代码示例演示了如何将 AWS Tools for PowerShell V4 与 DynamoDB 结合使用,来执行操作和实现常见场景。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示了如何使用 Add-DDBIndexSchema。
- Tools for PowerShell V4
-
示例 1:创建一个空的 TableSchema 对象并向其添加一个新的本地二级索引定义,然后将该 TableSchema 对象写入管道。
$schema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only" $schema = New-DDBTableSchema输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}示例 2:向提供的 TableSchema 对象添加新的本地二级索引定义,然后将 TableSchema 对象写回管道。也可以使用 -Schema 参数提供 TableSchema 对象。
New-DDBTableSchema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考(V4)》中的 Add-DDBIndexSchema。
-
以下代码示例演示了如何使用 Add-DDBKeySchema。
- Tools for PowerShell V4
-
示例 1:创建一个空的 TableSchema 对象,并使用指定的键数据向其添加键和属性定义条目,然后将 TableSchema 对象写入管道。默认情况下,键类型声明为“HASH”;使用值为“RANGE”的 -KeyType 参数来声明范围键。
$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}示例 2:向提供的 TableSchema 对象添加新的键和属性定义条目,然后将 TableSchema 对象写入管道。默认情况下,键类型声明为“HASH”;使用值为“RANGE”的 -KeyType 参数来声明范围键。也可以使用 -Schema 参数提供 TableSchema 对象。
New-DDBTableSchema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考(V4)》中的 Add-DDBKeySchema。
-
以下代码示例演示了如何使用 ConvertFrom-DDBItem。
- Tools for PowerShell V4
-
示例 1:ConvertFrom-DDBItem 用于将 Get-DDBItem 的结果从 DynamoDB AttributeValues 的哈希表转换为诸如字符串和双精度等常见类型的哈希表。
@{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Get-DDBItem -TableName 'Music' -Key $key | ConvertFrom-DDBItem输出:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考(V4)》中的 ConvertFrom-DDBItem。
-
以下代码示例演示了如何使用 ConvertTo-DDBItem。
- Tools for PowerShell V4
-
示例 1:将哈希表转换为 DynamoDB 属性值字典的示例。
@{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Key Value --- ----- SongTitle Amazon.DynamoDBv2.Model.AttributeValue Artist Amazon.DynamoDBv2.Model.AttributeValue示例 2:将哈希表转换为 DynamoDB 属性值字典的示例。
@{ MyMap = @{ MyString = 'my string' } MyStringSet = [System.Collections.Generic.HashSet[String]]@('my', 'string') MyNumericSet = [System.Collections.Generic.HashSet[Int]]@(1, 2, 3) MyBinarySet = [System.Collections.Generic.HashSet[System.IO.MemoryStream]]@( ([IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes('my'))), ([IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes('string'))) ) MyList1 = @('my', 'string') MyList2 = [System.Collections.Generic.List[Int]]@(1, 2) MyList3 = [System.Collections.ArrayList]@('one', 2, $true) } | ConvertTo-DDBItem输出:
Key Value --- ----- MyStringSet Amazon.DynamoDBv2.Model.AttributeValue MyList1 Amazon.DynamoDBv2.Model.AttributeValue MyNumericSet Amazon.DynamoDBv2.Model.AttributeValue MyList2 Amazon.DynamoDBv2.Model.AttributeValue MyBinarySet Amazon.DynamoDBv2.Model.AttributeValue MyMap Amazon.DynamoDBv2.Model.AttributeValue MyList3 Amazon.DynamoDBv2.Model.AttributeValue-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考(V4)》中的 ConvertTo-DDBItem。
-
以下代码示例演示了如何使用 Get-DDBBatchItem。
- Tools for PowerShell V4
-
示例 1:从 DynamoDB 表“Music”和“Songs”中获取 SongTitle 为“Somewhere Down The Road”的项目。
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem $keysAndAttributes = New-Object Amazon.DynamoDBv2.Model.KeysAndAttributes $list = New-Object 'System.Collections.Generic.List[System.Collections.Generic.Dictionary[String, Amazon.DynamoDBv2.Model.AttributeValue]]' $list.Add($key) $keysAndAttributes.Keys = $list $requestItem = @{ 'Music' = [Amazon.DynamoDBv2.Model.KeysAndAttributes]$keysAndAttributes 'Songs' = [Amazon.DynamoDBv2.Model.KeysAndAttributes]$keysAndAttributes } $batchItems = Get-DDBBatchItem -RequestItem $requestItem $batchItems.GetEnumerator() | ForEach-Object {$PSItem.Value} | ConvertFrom-DDBItem输出:
Name Value ---- ----- Artist No One You Know SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous CriticRating 10 Genre Country Price 1.94 Artist No One You Know SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous CriticRating 10 Genre Country Price 1.94-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 BatchGetItem。
-
以下代码示例演示了如何使用 Get-DDBItem。
- Tools for PowerShell V4
-
示例 1:返回带有分区键 SongTitle 和排序键 Artist 的 DynamoDB 项目。
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Get-DDBItem -TableName 'Music' -Key $key | ConvertFrom-DDBItem输出:
Name Value ---- ----- Genre Country SongTitle Somewhere Down The Road Price 1.94 Artist No One You Know CriticRating 9 AlbumTitle Somewhat Famous-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 GetItem。
-
以下代码示例演示了如何使用 Get-DDBTable。
- Tools for PowerShell V4
-
示例 1:返回指定表的详细信息。
Get-DDBTable -TableName "myTable"-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 DescribeTable。
-
以下代码示例演示了如何使用 Get-DDBTableList。
- Tools for PowerShell V4
-
示例 1:返回所有表的详细信息,自动迭代,直到服务指示不存在其它表。
Get-DDBTableList-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 ListTables。
-
以下代码示例演示了如何使用 Invoke-DDBQuery。
- Tools for PowerShell V4
-
示例 1:调用一个查询,该查询返回具有指定 SongTitle 和 Artist 的 DynamoDB 项目。
$invokeDDBQuery = @{ TableName = 'Music' KeyConditionExpression = ' SongTitle = :SongTitle and Artist = :Artist' ExpressionAttributeValues = @{ ':SongTitle' = 'Somewhere Down The Road' ':Artist' = 'No One You Know' } | ConvertTo-DDBItem } Invoke-DDBQuery @invokeDDBQuery | ConvertFrom-DDBItem输出:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 Query。
-
以下代码示例演示了如何使用 Invoke-DDBScan。
- Tools for PowerShell V4
-
示例 1:返回 Music 表中的所有项目。
Invoke-DDBScan -TableName 'Music' | ConvertFrom-DDBItem输出:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous Genre Country Artist No One You Know Price 1.98 CriticRating 8.4 SongTitle My Dog Spot AlbumTitle Hey Now示例 2:返回 Music 表中 CriticRating 大于或等于 9 的项目。
$scanFilter = @{ CriticRating = [Amazon.DynamoDBv2.Model.Condition]@{ AttributeValueList = @(@{N = '9'}) ComparisonOperator = 'GE' } } Invoke-DDBScan -TableName 'Music' -ScanFilter $scanFilter | ConvertFrom-DDBItem输出:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 Scan。
-
以下代码示例演示了如何使用 New-DDBTable。
- Tools for PowerShell V4
-
示例 1:此示例创建一个名为 Thread 的表,该表的主键由“ForumName”(键类型哈希)和“Subject”(键类型范围)组成。用于构造表的架构可以通过管道传输到所示的每个 cmdlet 中,也可以使用 -Schema 参数指定。
$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S" $schema | Add-DDBKeySchema -KeyName "Subject" -KeyType RANGE -KeyDataType "S" $schema | New-DDBTable -TableName "Thread" -ReadCapacity 10 -WriteCapacity 5输出:
AttributeDefinitions : {ForumName, Subject} TableName : Thread KeySchema : {ForumName, Subject} TableStatus : CREATING CreationDateTime : 10/28/2013 4:39:49 PM ProvisionedThroughput : Amazon.DynamoDBv2.Model.ProvisionedThroughputDescription TableSizeBytes : 0 ItemCount : 0 LocalSecondaryIndexes : {}示例 2:此示例创建一个名为 Thread 的表,该表的主键由“ForumName”(键类型哈希)和“Subject”(键类型范围)组成。还定义了本地二级索引。本地二级索引的键将根据表上的主哈希键(ForumName)自动设置。用于构造表的架构可以通过管道传输到所示的每个 cmdlet 中,也可以使用 -Schema 参数指定。
$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S" $schema | Add-DDBKeySchema -KeyName "Subject" -KeyDataType "S" $schema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only" $schema | New-DDBTable -TableName "Thread" -ReadCapacity 10 -WriteCapacity 5输出:
AttributeDefinitions : {ForumName, LastPostDateTime, Subject} TableName : Thread KeySchema : {ForumName, Subject} TableStatus : CREATING CreationDateTime : 10/28/2013 4:39:49 PM ProvisionedThroughput : Amazon.DynamoDBv2.Model.ProvisionedThroughputDescription TableSizeBytes : 0 ItemCount : 0 LocalSecondaryIndexes : {LastPostIndex}示例 3:此示例展示了如何使用单个管道创建一个名为 Thread 的表,该表的主键由“ForumName”(键类型哈希)和“Subject”(键类型范围)以及本地二级索引组成。如果管道或 -Schema 参数中未提供 TableSchema 对象,则 Add-DDBKeySchema 和 Add-DDBIndexSchema 会为您创建一个新的 TableSchema 对象。
New-DDBTableSchema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S" | Add-DDBKeySchema -KeyName "Subject" -KeyDataType "S" | Add-DDBIndexSchema -IndexName "LastPostIndex" ` -RangeKeyName "LastPostDateTime" ` -RangeKeyDataType "S" ` -ProjectionType "keys_only" | New-DDBTable -TableName "Thread" -ReadCapacity 10 -WriteCapacity 5输出:
AttributeDefinitions : {ForumName, LastPostDateTime, Subject} TableName : Thread KeySchema : {ForumName, Subject} TableStatus : CREATING CreationDateTime : 10/28/2013 4:39:49 PM ProvisionedThroughput : Amazon.DynamoDBv2.Model.ProvisionedThroughputDescription TableSizeBytes : 0 ItemCount : 0 LocalSecondaryIndexes : {LastPostIndex}-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 CreateTable。
-
以下代码示例演示了如何使用 New-DDBTableSchema。
- Tools for PowerShell V4
-
示例 1:创建一个空的 TableSchema 对象,该对象可以接受键和索引定义,来用于创建新的 Amazon DynamoDB 表。返回的对象可以通过管道传送到 Add-DDBKeySchema、Add-DDBIndexSchema 和 New-DDBTable cmdlet 中,也可以在每个 cmdlet 上使用 -Schema 参数来传递给它们。
New-DDBTableSchema输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考(V4)》中的 New-DDBTableSchema。
-
以下代码示例演示了如何使用 Remove-DDBItem。
- Tools for PowerShell V4
-
示例 1:移除与提供的键匹配的 DynamoDB 项目。
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Remove-DDBItem -TableName 'Music' -Key $key -Confirm:$false-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 DeleteItem。
-
以下代码示例演示了如何使用 Remove-DDBTable。
- Tools for PowerShell V4
-
示例 1:删除指定的表。在操作继续之前,系统会提示您进行确认。
Remove-DDBTable -TableName "myTable"示例 2:删除指定的表。在操作继续之前,系统不会提示您进行确认。
Remove-DDBTable -TableName "myTable" -Force-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 DeleteTable。
-
以下代码示例演示了如何使用 Set-DDBBatchItem。
- Tools for PowerShell V4
-
示例 1:创建一个新项目,或将现有项目替换为 DynamoDB 表 Music 和 Songs 中的新项目。
$item = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' AlbumTitle = 'Somewhat Famous' Price = 1.94 Genre = 'Country' CriticRating = 10.0 } | ConvertTo-DDBItem $writeRequest = New-Object Amazon.DynamoDBv2.Model.WriteRequest $writeRequest.PutRequest = [Amazon.DynamoDBv2.Model.PutRequest]$item输出:
$requestItem = @{ 'Music' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) 'Songs' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) } Set-DDBBatchItem -RequestItem $requestItem-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 BatchWriteItem。
-
以下代码示例演示了如何使用 Set-DDBItem。
- Tools for PowerShell V4
-
示例 1:创建一个新项目,或将现有项目替换为新项目。
$item = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' AlbumTitle = 'Somewhat Famous' Price = 1.94 Genre = 'Country' CriticRating = 9.0 } | ConvertTo-DDBItem Set-DDBItem -TableName 'Music' -Item $item-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 PutItem。
-
以下代码示例演示了如何使用 Update-DDBItem。
- Tools for PowerShell V4
-
示例 1:在带有分区键 SongTitle 和排序键 Artist 的 DynamoDB 项目上将流派属性设置为“Rap”。
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem $updateDdbItem = @{ TableName = 'Music' Key = $key UpdateExpression = 'set Genre = :val1' ExpressionAttributeValue = (@{ ':val1' = ([Amazon.DynamoDBv2.Model.AttributeValue]'Rap') }) } Update-DDBItem @updateDdbItem输出:
Name Value ---- ----- Genre Rap-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 UpdateItem。
-
以下代码示例演示了如何使用 Update-DDBTable。
- Tools for PowerShell V4
-
示例 1:更新给定表的预置吞吐量。
Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5-
有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 UpdateTable。
-