的版本 5 (V5) AWS Tools for PowerShell 已经发布!
有关重大更改和迁移应用程序的信息,请参阅 迁移话题。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用工具的 DynamoDB 示例 PowerShell V5
以下代码示例向您展示如何使用执行操作和实现常见场景 AWS Tools for PowerShell 带有 DynamoDB 的 V5。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示了如何使用 Add-DDBIndexSchema。
- 用途的工具 PowerShell V5
-
示例 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 之前的对象物体返回管道。这个 TableSchema 也可以使用-Schema 参数提供对象。
New-DDBTableSchema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}-
有关 API 的详细信息,请参阅 Add-DDBIndexSchema 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Add-DDBKeySchema。
- 用途的工具 PowerShell V5
-
示例 1:创建一个空 TableSchema 的对象,并在写入之前使用指定的密钥数据向其添加密钥和属性定义条目管道的 TableSchema 对象。默认情况下,密钥类型声明为 “HASH”;使用-KeyType 值为 “RANGE” 的参数来声明范围键。
$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}示例 2:向提供 TableSchema 的内容添加新的键和属性定义条目写入之前的对象 TableSchema 管道的对象。默认情况下,密钥类型声明为 “HASH”;使用-KeyType 值为 “RANGE” 的参数来声明范围键。这个 TableSchema 也可以使用-Schema 参数提供对象。
New-DDBTableSchema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}-
有关 API 的详细信息,请参阅 Add-DDBKeySchema 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 ConvertFrom-DDBItem。
- 用途的工具 PowerShell V5
-
示例 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 的详细信息,请参阅 ConvertFrom-DDBItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 ConvertTo-DDBItem。
- 用途的工具 PowerShell V5
-
示例 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 的详细信息,请参阅 ConvertTo-DDBItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Get-DDBBatchItem。
- 用途的工具 PowerShell V5
-
示例 1:获取带有 SongTitle DynamoDB 表中的 “音乐” 和 “歌曲” 表中的 “某处”。
$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 的详细信息,请参阅 BatchGetItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Get-DDBItem。
- 用途的工具 PowerShell V5
-
示例 1:返回带有分区键的 DynamoDB 项目 SongTitle 还有排序键 “艺术家”。
$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 的详细信息,请参阅 GetItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Get-DDBTable。
- 用途的工具 PowerShell V5
-
示例 1:返回指定表的详细信息。
Get-DDBTable -TableName "myTable"-
有关 API 的详细信息,请参阅 DescribeTable 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Get-DDBTableList。
- 用途的工具 PowerShell V5
-
示例 1:返回所有表的详细信息,自动迭代,直到服务指示不存在其它表。
Get-DDBTableList-
有关 API 的详细信息,请参阅 ListTables 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Invoke-DDBQuery。
- 用途的工具 PowerShell V5
-
示例 1:调用一个查询,该查询返回具有指定的 DynamoDB 项目 SongTitle 和艺术家。
$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 (V5)》中的 Query。
-
以下代码示例演示了如何使用 Invoke-DDBScan。
- 用途的工具 PowerShell V5
-
示例 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:返回音乐表中带有 CriticRating 大于或等于九。
$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 (V5)》中的 Scan。
-
以下代码示例演示了如何使用 New-DDBTable。
- 用途的工具 PowerShell V5
-
示例 1:此示例创建了一个名为 Thread 的表,其主键由 'ForumName组成'(密钥类型哈希)和 “主题”(密钥类型范围)。用于构造表的架构可以通过管道传输到所示的每个 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组成'(密钥类型哈希)和 “主题”(密钥类型范围)。还定义了本地二级索引。本地二级索引的键将根据表上的主哈希键自动设置 (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'(密钥类型哈希)和 “主题”(密钥类型范围)以及本地二级索引。这个 Add-DDBKeySchema 和 Add-DDBIndexSchema 创建一个新的 TableSchema 如果管道或-Schema 参数未提供对象,则为你提供对象。
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 的详细信息,请参阅 CreateTable 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 New-DDBTableSchema。
- 用途的工具 PowerShell V5
-
示例 1:创建一个空 TableSchema 的对象已准备好接受密钥和索引定义,用于创建新的 Amazon DynamoDB 表。返回的对象可以通过管道传送到 Add-DDBKeySchema, Add-DDBIndexSchema 和 New-DDBTable cmdlet 或使用每个 cmdlet 上的-Schema 参数传递给他们。
New-DDBTableSchema输出:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}-
有关 API 的详细信息,请参阅 New-DDBTableSchema 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Remove-DDBItem。
- 用途的工具 PowerShell V5
-
示例 1:移除与提供的键匹配的 DynamoDB 项目。
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Remove-DDBItem -TableName 'Music' -Key $key -Confirm:$false-
有关 API 的详细信息,请参阅 DeleteItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Remove-DDBTable。
- 用途的工具 PowerShell V5
-
示例 1:删除指定的表。在操作继续之前,系统会提示您进行确认。
Remove-DDBTable -TableName "myTable"示例 2:删除指定的表。在操作继续之前,系统不会提示您进行确认。
Remove-DDBTable -TableName "myTable" -Force-
有关 API 的详细信息,请参阅 DeleteTable 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Set-DDBBatchItem。
- 用途的工具 PowerShell V5
-
示例 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 的详细信息,请参阅 BatchWriteItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Set-DDBItem。
- 用途的工具 PowerShell V5
-
示例 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 的详细信息,请参阅 PutItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Update-DDBItem。
- 用途的工具 PowerShell V5
-
示例 1:使用分区键将 DynamoDB 项目上的流派属性设置为 “Rap” SongTitle 还有排序键 “艺术家”。
$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 的详细信息,请参阅 UpdateItem 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-
以下代码示例演示了如何使用 Update-DDBTable。
- 用途的工具 PowerShell V5
-
示例 1:更新给定表的预置吞吐量。
Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5-
有关 API 的详细信息,请参阅 UpdateTable 在 AWS Tools for PowerShell Cmdlet 参考手册 (V5)。
-