Esempi di DynamoDB con Tools for PowerShell - AWS Esempi di codice SDK

Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub

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 con Tools for PowerShell

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS Strumenti per PowerShell con DynamoDB.

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati.

Ogni esempio include un collegamento al codice sorgente completo, dove è possibile trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Operazioni

Il seguente esempio di codice mostra come utilizzareAdd-DDBIndexSchema.

Strumenti per 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-DDBTableSchema

Output:

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- DDBIndex Schema in AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Add-DDBKeySchema

Strumenti per 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; utilizzate il KeyType parametro - con il 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 chiavi e attributi all' TableSchema oggetto fornito prima di scrivere l' TableSchema oggetto nella pipeline. Il tipo di chiave è dichiarato «HASH» per impostazione predefinita; utilizzate il KeyType parametro - con il valore «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- DDBKey Schema in AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. ConvertFrom-DDBItem

Strumenti per PowerShell

Esempio 1: ConvertFrom - DDBItem viene utilizzato per convertire il risultato di Get-DDBItem da una tabella hash di AttributeValues DynamoDB 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-DDBItem

Output:

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 Cmdlet Reference.AWS Strumenti per PowerShell

Il seguente esempio di codice mostra come utilizzare. ConvertTo-DDBItem

Strumenti per 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.AttributeValue

Esempio 2: Un esempio di conversione di una tabella hash in un dizionario dei valori degli attributi di 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

Output:

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 Cmdlet Reference.AWS Strumenti per PowerShell

Il seguente esempio di codice mostra come utilizzare. Get-DDBBatchItem

Strumenti per 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-DDBItem

Output:

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 BatchGetItemin Cmdlet Reference.AWS Strumenti per PowerShell

Il seguente esempio di codice mostra come utilizzare. Get-DDBItem

Strumenti per PowerShell

Esempio 1: restituisce l'elemento DynamoDB con la chiave di partizione e la SongTitle chiave di ordinamento Artist.

$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Get-DDBItem -TableName 'Music' -Key $key | ConvertFrom-DDBItem

Output:

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 GetItemin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Get-DDBTable

Strumenti per PowerShell

Esempio 1: restituisce i dettagli della tabella specificata.

Get-DDBTable -TableName "myTable"
  • Per i dettagli sull'API, vedere DescribeTablein AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Get-DDBTableList

Strumenti per PowerShell

Esempio 1: restituisce i dettagli di tutte le tabelle, iterando automaticamente fino a quando il servizio non indica che non esistono altre tabelle.

Get-DDBTableList
  • Per i dettagli sull'API, vedere ListTablesin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Invoke-DDBQuery

Strumenti per PowerShell

Esempio 1: richiama una query che restituisce elementi DynamoDB con l'elemento Artist specificato. 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

Output:

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 Query in Cmdlet Reference.AWS Strumenti per PowerShell

Il seguente esempio di codice mostra come utilizzare. Invoke-DDBScan

Strumenti per PowerShell

Esempio 1: restituisce tutti gli elementi della tabella Music.

Invoke-DDBScan -TableName 'Music' | ConvertFrom-DDBItem

Output:

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

Esempio 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-DDBItem

Output:

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 Scan in AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. New-DDBTable

Strumenti per PowerShell

Esempio 1: Questo esempio crea una tabella denominata Thread con una chiave primaria composta da 'ForumName' (hash del tipo di chiave) e 'Subject' (intervallo dei tipi di chiave). Lo schema utilizzato per costruire la tabella può essere inserito in ogni cmdlet come mostrato o specificato utilizzando 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 5

Output:

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 '' (hash del tipo di chiave) e ForumName 'Subject' (intervallo dei 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 utilizzando 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 5

Output:

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 '' (hash del tipo di chiave) e ForumName 'Subject' (intervallo dei tipi di chiave) e un indice secondario locale. Add- DDBKey Schema e Add- DDBIndex Schema creano automaticamente un nuovo TableSchema oggetto se uno non viene fornito 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 5

Output:

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 CreateTablein AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. New-DDBTableSchema

Strumenti per 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 cmdlet Add- DDBKey Schema, Add- DDBIndex Schema e New- o passato a essi utilizzando il parametro -Schema su ogni DDBTable cmdlet.

New-DDBTableSchema

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}
  • Per i dettagli sull'API, vedere New - Schema in Cmdlet Reference. DDBTable AWS Strumenti per PowerShell

Il seguente esempio di codice mostra come utilizzare. Remove-DDBItem

Strumenti per PowerShell

Esempio 1: rimuove l'elemento DynamoDB che corrisponde alla chiave fornita.

$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 DeleteItemin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Remove-DDBTable

Strumenti per 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. Non viene richiesta la conferma prima che l'operazione prosegua.

Remove-DDBTable -TableName "myTable" -Force
  • Per i dettagli sull'API, vedere DeleteTablein AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Set-DDBBatchItem

Strumenti per PowerShell

Esempio 1: crea un nuovo elemento o sostituisce un elemento esistente con un nuovo elemento nelle tabelle DynamoDB Music and 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

Output:

$requestItem = @{ 'Music' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) 'Songs' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) } Set-DDBBatchItem -RequestItem $requestItem
  • Per i dettagli sull'API, vedere BatchWriteItemin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Set-DDBItem

Strumenti per 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 PutItemin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Update-DDBItem

Strumenti per PowerShell

Esempio 1: imposta l'attributo genre su 'Rap' sull'elemento DynamoDB con la chiave di partizione e la SongTitle 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 @updateDdbItem

Output:

Name Value ---- ----- Genre Rap
  • Per i dettagli sull'API, vedere UpdateItemin AWS Strumenti per PowerShell Cmdlet Reference.

Il seguente esempio di codice mostra come utilizzare. Update-DDBTable

Strumenti per PowerShell

Esempio 1: aggiorna il throughput assegnato per la tabella specificata.

Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5
  • Per i dettagli sull'API, vedere UpdateTablein AWS Strumenti per PowerShell Cmdlet Reference.