Versione 5 (V5) del AWS Strumenti per PowerShell è stata rilasciata!
Per informazioni sulle modifiche sostanziali e sulla migrazione delle applicazioni, consulta la argomento sulla migrazione.
Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Esempi di DynamoDB che utilizzano Tools for PowerShell V5
I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS Strumenti per PowerShell V5 con DynamoDB.
Le azioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le azioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati.
Ogni esempio include un link al codice sorgente completo, in cui vengono fornite le istruzioni su come configurare ed eseguire il codice nel contesto.
Argomenti
Operazioni
L’esempio di codice seguente mostra come utilizzare Add-DDBIndexSchema.
- Strumenti per PowerShell V5
-
Esempio 1: crea un vuoto TableSchema oggetto e vi aggiunge una nuova definizione di indice secondario locale prima di scrivere il TableSchema oggetto alla pipeline.
$schema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only" $schema = New-DDBTableSchemaOutput:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}Esempio 2: aggiunge una nuova definizione di indice secondario locale a TableSchema quella fornitaoggetto prima di scrivere ilritorno dell' TableSchema oggetto alla pipeline. Il TableSchema l'oggetto può essere fornito anche utilizzando il parametro -Schema.
New-DDBTableSchema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only"Output:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}-
Per i dettagli sull'API, vedere Add-DDBIndexSchema nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Add-DDBKeySchema.
- Utensili per PowerShell V5
-
Esempio 1: crea un TableSchema vuotooggetto e vi aggiunge le voci di definizione di chiavi e attributi utilizzando i dati chiave specificati prima di scrivere TableSchema iloggetto alla pipeline. Il tipo di chiave è dichiarato 'HASH' per impostazione predefinita; usa il -KeyType parametro con valore 'RANGE' per dichiarare una chiave di intervallo.
$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"Output:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}Esempio 2: aggiunge nuove voci di definizione di chiave e attributo a TableSchema quelle forniteoggetto prima di scrivere TableSchema iloggetto alla pipeline. Il tipo di chiave è dichiarato 'HASH' per impostazione predefinita; usa il -KeyType parametro con valore 'RANGE' per dichiarare una chiave di intervallo. Il TableSchema l'oggetto può essere fornito anche utilizzando il parametro -Schema.
New-DDBTableSchema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"Output:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}-
Per i dettagli sull'API, vedere Add-DDBKeySchema nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare ConvertFrom-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: ConvertFrom-DDBItem viene utilizzato per convertire il risultato di Get-DDBItem da una tabella hash di DynamoDB AttributeValues a una tabella hash di tipi comuni come string e double.
@{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Get-DDBItem -TableName 'Music' -Key $key | ConvertFrom-DDBItemOutput:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
Per i dettagli sull'API, vedi ConvertFrom-DDBItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare ConvertTo-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: un esempio di conversione di una tabella hash in un dizionario di valori di attributi DynamoDB.
@{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Key Value --- ----- SongTitle Amazon.DynamoDBv2.Model.AttributeValue Artist Amazon.DynamoDBv2.Model.AttributeValueEsempio 2: un esempio di conversione di una tabella hash in un dizionario di valori di attributi 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-DDBItemOutput:
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-
Per i dettagli sull'API, vedi ConvertTo-DDBItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBBatchItem.
- Utensili per PowerShell V5
-
Esempio 1: ottiene l'articolo con SongTitle «Somewhere Down The Road» dalle tabelle «Music» e «Songs» di 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-DDBItemOutput:
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-
Per i dettagli sulle API, consulta BatchGetItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: restituisce l'elemento DynamoDB con la chiave di partizione SongTitle e la chiave di ordinamento Artist.
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Get-DDBItem -TableName 'Music' -Key $key | ConvertFrom-DDBItemOutput:
Name Value ---- ----- Genre Country SongTitle Somewhere Down The Road Price 1.94 Artist No One You Know CriticRating 9 AlbumTitle Somewhat Famous-
Per i dettagli sull'API, vedi GetItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBTable.
- Utensili per PowerShell V5
-
Esempio 1: restituisce i dettagli della tabella specificata.
Get-DDBTable -TableName "myTable"-
Per i dettagli sull'API, vedi DescribeTable nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBTableList.
- Utensili per PowerShell V5
-
Esempio 1: restituisce i dettagli di tutte le tabelle, eseguendo un’iterazione automatica finché il servizio non indica che non esistono altre tabelle.
Get-DDBTableList-
Per i dettagli sull'API, vedi ListTables nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Invoke-DDBQuery.
- Utensili per PowerShell V5
-
Esempio 1: richiama una query che restituisce elementi DynamoDB con il valore specificato SongTitle e Artista.
$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-DDBItemOutput:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
Per informazioni dettagliate sull’API, consulta Query nella documentazione di riferimento dei cmdlet di AWS Strumenti per PowerShell (V5).
-
L’esempio di codice seguente mostra come utilizzare Invoke-DDBScan.
- Strumenti per PowerShell V5
-
Esempio 1: restituisce tutti gli elementi nella tabella Music.
Invoke-DDBScan -TableName 'Music' | ConvertFrom-DDBItemOutput:
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 NowEsempio 2: restituisce gli elementi nella tabella Music con un CriticRating maggiore o uguale a nove.
$scanFilter = @{ CriticRating = [Amazon.DynamoDBv2.Model.Condition]@{ AttributeValueList = @(@{N = '9'}) ComparisonOperator = 'GE' } } Invoke-DDBScan -TableName 'Music' -ScanFilter $scanFilter | ConvertFrom-DDBItemOutput:
Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous-
Per informazioni dettagliate sull’API, consulta Scan nella documentazione di riferimento dei cmdlet di AWS Strumenti per PowerShell (V5).
-
L’esempio di codice seguente mostra come utilizzare New-DDBTable.
- Strumenti per PowerShell V5
-
Esempio 1: questo esempio crea una tabella denominata Thread con una chiave primaria composta da ''ForumName(hash del tipo di chiave) e' Subject '(intervallo di tipi di chiave). Lo schema utilizzato per costruire la tabella può essere inserito in ogni cmdlet come mostrato o specificato mediante il parametro -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 5Output:
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 : {}Esempio 2: questo esempio crea una tabella denominata Thread con una chiave primaria composta da ''ForumName(hash del tipo di chiave) e' Subject '(intervallo di tipi di chiave). Viene inoltre definito un indice secondario locale. La chiave dell'indice secondario locale verrà impostata automaticamente dalla chiave hash primaria sulla tabella (ForumName). Lo schema utilizzato per costruire la tabella può essere inserito in ogni cmdlet come mostrato o specificato mediante il parametro -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 5Output:
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}Esempio 3: questo esempio mostra come utilizzare una singola pipeline per creare una tabella denominata Thread con una chiave primaria composta da ''ForumName(hash del tipo di chiave) e' Subject '(intervallo di tipi di chiave) e un indice secondario locale. Il Add-DDBKeySchema e Add-DDBIndexSchema creare un nuovo TableSchema oggetto per voi se non ne viene fornito uno dalla pipeline o dal parametro -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 5Output:
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}-
Per i dettagli sull'API, vedi CreateTable nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare New-DDBTableSchema.
- Utensili per PowerShell V5
-
Esempio 1: crea un TableSchema vuotooggetto pronto ad accettare definizioni di chiavi e indici da utilizzare nella creazione di una nuova tabella Amazon DynamoDB. L'oggetto restituito può essere reindirizzato a Add-DDBKeySchema, Add-DDBIndexSchema e New-DDBTable cmdlet o passati loro utilizzando il parametro -Schema in ogni cmdlet.
New-DDBTableSchemaOutput:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}-
Per informazioni dettagliate sull'API, vedere New-DDBTableSchema nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Remove-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: rimuove l’elemento DynamoDB corrispondente alla chiave specificata.
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Remove-DDBItem -TableName 'Music' -Key $key -Confirm:$false-
Per i dettagli sull'API, vedi DeleteItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Remove-DDBTable.
- Utensili per PowerShell V5
-
Esempio 1: elimina la tabella specificata. Prima di procedere con l’operazione, viene richiesta una conferma.
Remove-DDBTable -TableName "myTable"Esempio 2: elimina la tabella specificata. Prima di procedere con l’operazione, non viene richiesta una conferma.
Remove-DDBTable -TableName "myTable" -Force-
Per i dettagli sull'API, vedi DeleteTable nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Set-DDBBatchItem.
- Utensili per PowerShell V5
-
Esempio 1: crea un nuovo elemento o sostituisce un elemento esistente con un nuovo elemento nelle tabelle DynamoDB Music e 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-
Per i dettagli sull'API, vedi BatchWriteItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Set-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: crea un nuovo elemento o sostituisce un elemento esistente con uno nuovo.
$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-
Per i dettagli sull'API, vedi PutItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Update-DDBItem.
- Utensili per PowerShell V5
-
Esempio 1: imposta l'attributo genre su 'Rap' sull'elemento DynamoDB con la chiave di partizione SongTitle e la chiave di ordinamento Artist.
$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 @updateDdbItemOutput:
Name Value ---- ----- Genre Rap-
Per i dettagli sull'API, vedi UpdateItem nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-
L’esempio di codice seguente mostra come utilizzare Update-DDBTable.
- Utensili per PowerShell V5
-
Esempio 1: aggiorna il throughput con provisioning per la tabella specificata.
Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5-
Per i dettagli sull'API, vedi UpdateTable nel AWS Strumenti per PowerShell Riferimento al cmdlet (V5).
-