Sono disponibili altri esempi di AWS SDK nel repository AWS Doc SDK Examples.
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 V5 PowerShell
I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando il 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
Azioni
L’esempio di codice seguente mostra come utilizzare Add-DDBIndexSchema.
- Strumenti per V5 PowerShell
-
Esempio 1: crea un TableSchema oggetto vuoto e vi aggiunge una nuova definizione di indice secondario locale prima di scrivere l' TableSchema oggetto nella 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 all' TableSchema oggetto fornito prima di riscrivere l' TableSchema oggetto nella pipeline. L' TableSchema 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 in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Add-DDBKeySchema.
- Strumenti per V5 PowerShell
-
Esempio 1: crea un TableSchema oggetto vuoto e vi aggiunge le voci di definizione di chiavi e attributi utilizzando i dati chiave specificati prima di scrivere l' TableSchema oggetto nella pipeline. Il tipo di chiave è dichiarato «HASH» per impostazione predefinita; usa il KeyType parametro - con un valore di «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 chiavi e attributi all' TableSchema oggetto fornito prima di scrivere l' TableSchema oggetto nella pipeline. Il tipo di chiave è dichiarato «HASH» per impostazione predefinita; usa il KeyType parametro - con un valore di «RANGE» per dichiarare una chiave di intervallo. L' TableSchema 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 in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare ConvertFrom-DDBItem.
- Strumenti per V5 PowerShell
-
Esempio 1: ConvertFrom-DDBItem viene utilizzato per convertire il risultato di Get-DDBItem una tabella hash di DynamoDB AttributeValues in 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, vedere ConvertFrom-DDBItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare ConvertTo-DDBItem.
- Strumenti per V5 PowerShell
-
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, vedere ConvertTo-DDBItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBBatchItem.
- Strumenti per V5 PowerShell
-
Esempio 1: ottiene l'elemento 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 sull'API, vedere BatchGetItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBItem.
- Strumenti per V5 PowerShell
-
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, vedere GetItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBTable.
- Strumenti per V5 PowerShell
-
Esempio 1: restituisce i dettagli della tabella specificata.
Get-DDBTable -TableName "myTable"-
Per i dettagli sull'API, vedere DescribeTable in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Get-DDBTableList.
- Strumenti per V5 PowerShell
-
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, vedere ListTables in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Invoke-DDBQuery.
- Strumenti per V5 PowerShell
-
Esempio 1: richiama una query che restituisce elementi DynamoDB con i valori specificati e Artist. 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-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.
- Utensili 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 valore 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.
- Utensili 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. Add-DDBIndexSchema Crea Add-DDBKeySchema e crea un nuovo TableSchema oggetto per te 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, vedere CreateTable in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare New-DDBTableSchema.
- Strumenti per V5 PowerShell
-
Esempio 1: crea un TableSchema oggetto vuoto pronto ad accettare definizioni di chiavi e indici da utilizzare nella creazione di una nuova tabella Amazon DynamoDB. L'oggetto restituito può essere reindirizzato ai New-DDBTable cmdlet Add-DDBIndexSchema e o passato ad essi utilizzando il parametro -Schema in ogni cmdlet. Add-DDBKeySchema
New-DDBTableSchemaOutput:
AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}-
Per i dettagli sull'API, vedere New-DDBTableSchema in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Remove-DDBItem.
- Strumenti per V5 PowerShell
-
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, vedere DeleteItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Remove-DDBTable.
- Strumenti per V5 PowerShell
-
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, vedere DeleteTable in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Set-DDBBatchItem.
- Strumenti per V5 PowerShell
-
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, vedere BatchWriteItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Set-DDBItem.
- Strumenti per V5 PowerShell
-
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, vedere PutItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Update-DDBItem.
- Strumenti per V5 PowerShell
-
Esempio 1: imposta l'attributo di genere 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, vedere UpdateItem in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-
L’esempio di codice seguente mostra come utilizzare Update-DDBTable.
- Strumenti per V5 PowerShell
-
Esempio 1: aggiorna il throughput con provisioning per la tabella specificata.
Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5-
Per i dettagli sull'API, vedere UpdateTable in AWS Strumenti per PowerShell Cmdlet Reference (V5).
-