Contoh DynamoDB menggunakan Alat untuk V4 PowerShell - Alat AWS untuk PowerShell (versi 4)

Alat AWS untuk PowerShell Versi 5 (V5) dalam pratinjau. Untuk melihat konten V5, yang dapat berubah, dan mencoba versi baru, lihat panduan pengguna versi 5 (pratinjau). Untuk informasi spesifik tentang melanggar perubahan dan migrasi ke V5, lihat topik migrasi dalam panduan tersebut.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh DynamoDB menggunakan Alat untuk V4 PowerShell

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan Alat AWS untuk PowerShell V4 dengan DynamoDB.

Tindakan merupakan kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanAdd-DDBIndexSchema.

Alat untuk PowerShell V4

Contoh 1: Membuat TableSchema objek kosong dan menambahkan definisi indeks sekunder lokal baru sebelum menulis TableSchema objek ke pipeline.

$schema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only" $schema = New-DDBTableSchema

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}

Contoh 2: Menambahkan definisi indeks sekunder lokal baru ke TableSchema objek yang disediakan sebelum menulis TableSchema objek kembali ke pipeline. TableSchema Objek juga dapat diberikan menggunakan parameter -Schema.

New-DDBTableSchema | Add-DDBIndexSchema -IndexName "LastPostIndex" -RangeKeyName "LastPostDateTime" -RangeKeyDataType "S" -ProjectionType "keys_only"

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {LastPostDateTime} {} {LastPostIndex}

Contoh kode berikut menunjukkan cara menggunakanAdd-DDBKeySchema.

Alat untuk PowerShell V4

Contoh 1: Membuat TableSchema objek kosong dan menambahkan entri definisi kunci dan atribut ke dalamnya menggunakan data kunci yang ditentukan sebelum menulis TableSchema objek ke pipeline. Tipe kunci dinyatakan sebagai 'HASH' secara default; gunakan - KeyType paameter dengan nilai 'RANGE' untuk mendeklarasikan kunci rentang.

$schema = New-DDBTableSchema $schema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}

Contoh 2: Menambahkan entri definisi kunci dan atribut baru ke TableSchema objek yang disediakan sebelum menulis TableSchema objek ke pipeline. Tipe kunci dinyatakan sebagai 'HASH' secara default; gunakan - KeyType paameter dengan nilai 'RANGE' untuk mendeklarasikan kunci rentang. TableSchema Objek juga dapat diberikan menggunakan parameter -Schema.

New-DDBTableSchema | Add-DDBKeySchema -KeyName "ForumName" -KeyDataType "S"

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {ForumName} {ForumName} {}

Contoh kode berikut menunjukkan cara menggunakanConvertFrom-DDBItem.

Alat untuk PowerShell V4

Contoh 1: ConvertFrom - DDBItem digunakan untuk mengonversi hasil Get-DDBItem dari hashtable AttributeValues DynamoDB ke hashtable tipe umum seperti string dan 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

Contoh kode berikut menunjukkan cara menggunakanConvertTo-DDBItem.

Alat untuk PowerShell V4

Contoh 1: Contoh untuk mengubah hashtable menjadi kamus nilai atribut 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

Contoh 2: Contoh untuk mengubah hashtable menjadi kamus nilai atribut 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
  • Untuk detail API, lihat ConvertTo- DDBItem di Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanGet-DDBBatchItem.

Alat untuk PowerShell V4

Contoh 1: Mendapatkan item dengan SongTitle “Somewhere Down The Road” dari tabel DynamoDB 'Music' dan 'Songs'.

$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
  • Untuk detail API, lihat BatchGetItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanGet-DDBItem.

Alat untuk PowerShell V4

Contoh 1: Mengembalikan item DynamoDB dengan SongTitle kunci partisi dan kunci sort 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
  • Untuk detail API, lihat GetItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanGet-DDBTable.

Alat untuk PowerShell V4

Contoh 1: Mengembalikan rincian tabel yang ditentukan.

Get-DDBTable -TableName "myTable"
  • Untuk detail API, lihat DescribeTabledi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanGet-DDBTableList.

Alat untuk PowerShell V4

Contoh 1: Mengembalikan rincian semua tabel, secara otomatis iterasi sampai layanan menunjukkan tidak ada tabel lebih lanjut.

Get-DDBTableList
  • Untuk detail API, lihat ListTablesdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanInvoke-DDBQuery.

Alat untuk PowerShell V4

Contoh 1: Memanggil query yang mengembalikan item DynamoDB dengan yang ditentukan dan 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-DDBItem

Output:

Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous
  • Untuk detail API, lihat Kueri di Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanInvoke-DDBScan.

Alat untuk PowerShell V4

Contoh 1: Mengembalikan semua item dalam tabel Musik.

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

Contoh 2: Mengembalikan item dalam tabel Musik dengan CriticRating lebih besar dari atau sama dengan sembilan.

$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
  • Untuk detail API, lihat Memindai di Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanNew-DDBTable.

Alat untuk PowerShell V4

Contoh 1: Contoh ini membuat tabel bernama Thread yang memiliki kunci utama yang terdiri dari 'ForumName' (hash tipe kunci) dan 'Subject' (rentang tipe kunci). Skema yang digunakan untuk membangun tabel dapat disalurkan ke setiap cmdlet seperti yang ditunjukkan atau ditentukan menggunakan parameter -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 : {}

Contoh 2: Contoh ini membuat tabel bernama Thread yang memiliki kunci utama yang terdiri dari 'ForumName' (hash tipe kunci) dan 'Subject' (rentang tipe kunci). Indeks sekunder lokal juga didefinisikan. Kunci indeks sekunder lokal akan diatur secara otomatis dari kunci hash utama pada tabel (ForumName). Skema yang digunakan untuk membangun tabel dapat disalurkan ke setiap cmdlet seperti yang ditunjukkan atau ditentukan menggunakan parameter -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}

Contoh 3: Contoh ini menunjukkan cara menggunakan pipeline tunggal untuk membuat tabel bernama Thread yang memiliki kunci utama yang terdiri dari 'ForumName' (hash tipe kunci) dan 'Subjek' (rentang tipe kunci) dan indeks sekunder lokal. DDBKeyAdd-Schema dan Add- DDBIndex Schema membuat TableSchema objek baru untuk Anda jika tidak disediakan dari pipeline atau parameter -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}
  • Untuk detail API, lihat CreateTabledi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanNew-DDBTableSchema.

Alat untuk PowerShell V4

Contoh 1: Membuat TableSchema objek kosong yang siap menerima definisi kunci dan indeks untuk digunakan dalam membuat tabel Amazon DynamoDB baru. Objek yang dikembalikan dapat disalurkan ke dalam Add- DDBKey Schema, Add- DDBIndex Schema dan New- DDBTable cmdlet atau diteruskan ke mereka menggunakan parameter -Schema pada setiap cmdlet.

New-DDBTableSchema

Output:

AttributeSchema KeySchema LocalSecondaryIndexSchema --------------- --------- ------------------------- {} {} {}

Contoh kode berikut menunjukkan cara menggunakanRemove-DDBItem.

Alat untuk PowerShell V4

Contoh 1: Menghapus item DynamoDB yang cocok dengan kunci yang disediakan.

$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem Remove-DDBItem -TableName 'Music' -Key $key -Confirm:$false
  • Untuk detail API, lihat DeleteItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanRemove-DDBTable.

Alat untuk PowerShell V4

Contoh 1: Menghapus tabel yang ditentukan. Anda diminta untuk konfirmasi sebelum operasi berlangsung.

Remove-DDBTable -TableName "myTable"

Contoh 2: Menghapus tabel yang ditentukan. Anda tidak diminta untuk konfirmasi sebelum operasi berlangsung.

Remove-DDBTable -TableName "myTable" -Force
  • Untuk detail API, lihat DeleteTabledi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanSet-DDBBatchItem.

Alat untuk PowerShell V4

Contoh 1: Membuat item baru, atau mengganti item yang ada dengan item baru di tabel DynamoDB Musik dan Lagu.

$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
  • Untuk detail API, lihat BatchWriteItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanSet-DDBItem.

Alat untuk PowerShell V4

Contoh 1: Membuat item baru, atau mengganti item yang sudah ada dengan item baru.

$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
  • Untuk detail API, lihat PutItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanUpdate-DDBItem.

Alat untuk PowerShell V4

Contoh 1: Menetapkan atribut genre ke 'Rap' pada item DynamoDB dengan SongTitle kunci partisi dan Artis kunci sortir.

$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
  • Untuk detail API, lihat UpdateItemdi Referensi Alat AWS untuk PowerShell Cmdlet (V4).

Contoh kode berikut menunjukkan cara menggunakanUpdate-DDBTable.

Alat untuk PowerShell V4

Contoh 1: Memperbarui throughput yang disediakan untuk tabel yang diberikan.

Update-DDBTable -TableName "myTable" -ReadCapacity 10 -WriteCapacity 5
  • Untuk detail API, lihat UpdateTabledi Referensi Alat AWS untuk PowerShell Cmdlet (V4).