

第 5 版 (V5) AWS Tools for PowerShell 已發行！

如需有關中斷變更和遷移應用程式的資訊，請參閱[遷移主題](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)。

 [https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Tools for PowerShell V5 的 Kinesis 範例
<a name="powershell_kinesis_code_examples"></a>

下列程式碼範例示範如何使用 AWS Tools for PowerShell V5 搭配 Kinesis 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `Get-KINRecord`
<a name="kinesis_GetRecords_powershell_topic"></a>

以下程式碼範例顯示如何使用 `Get-KINRecord`。

**Tools for PowerShell V5**  
**範例 1：此範例示範如何從一系列的一或多個記錄，傳回和擷取資料。提供給 Get-KINRecord 的迭代器，能判斷記錄的開始位置，以傳回在此範例中擷取到變數 \$1records 的位置。然後，您可以透過編製 \$1records 集合的索引來存取每個個別記錄。假設記錄中的資料為 UTF-8 編碼文字，最終命令會示範如何從物件中的 MemoryStream 擷取資料，並將其當作文字傳回至主控台。**  

```
$records
$records = Get-KINRecord -ShardIterator "AAAAAAAAAAGIc....9VnbiRNaP"
```
**輸出：**  

```
MillisBehindLatest NextShardIterator            Records
------------------ -----------------            -------
0                  AAAAAAAAAAERNIq...uDn11HuUs  {Key1, Key2}
```

```
$records.Records[0]
```
**輸出：**  

```
ApproximateArrivalTimestamp Data                   PartitionKey SequenceNumber
--------------------------- ----                   ------------ --------------
3/7/2016 5:14:33 PM         System.IO.MemoryStream Key1         4955986459776...931586
```

```
[Text.Encoding]::UTF8.GetString($records.Records[0].Data.ToArray())
```
**輸出：**  

```
test data from string
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetRecords](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-KINShardIterator`
<a name="kinesis_GetShardIterator_powershell_topic"></a>

以下程式碼範例顯示如何使用 `Get-KINShardIterator`。

**Tools for PowerShell V5**  
**範例 1：傳回指定碎片和開始位置的碎片迭代器。碎片識別碼和序號的詳細資訊，可從 Get-KINStream Cmdlet 的輸出取得，方法是參考傳回串流物件的碎片收集。傳回的迭代器可與 Get-KINRecord Cmdlet 搭配使用，以提取碎片中的資料記錄。**  

```
Get-KINShardIterator -StreamName "mystream" -ShardId "shardId-000000000000" -ShardIteratorType AT_SEQUENCE_NUMBER -StartingSequenceNumber "495598645..."
```
**輸出：**  

```
AAAAAAAAAAGIc....9VnbiRNaP
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [GetShardIterator](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Get-KINStream`
<a name="kinesis_DescribeStream_powershell_topic"></a>

以下程式碼範例顯示如何使用 `Get-KINStream`。

**Tools for PowerShell V5**  
**範例 1：傳回指定串流的詳細資訊。**  

```
Get-KINStream -StreamName "mystream"
```
**輸出：**  

```
HasMoreShards        : False
RetentionPeriodHours : 24
Shards               : {}
StreamARN            : arn:aws:kinesis:us-west-2:123456789012:stream/mystream
StreamName           : mystream
StreamStatus         : ACTIVE
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeStream](https://docs.aws.amazon.com/powershell/v5/reference)。

### `New-KINStream`
<a name="kinesis_CreateStream_powershell_topic"></a>

以下程式碼範例顯示如何使用 `New-KINStream`。

**Tools for PowerShell V5**  
**範例 1：建立新的串流。**  

```
New-KINStream -StreamName "mystream" -ShardCount 1
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateStream](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Remove-KINStream`
<a name="kinesis_DeleteStream_powershell_topic"></a>

以下程式碼範例顯示如何使用 `Remove-KINStream`。

**Tools for PowerShell V5**  
**範例 1：刪除指定的串流。在執行命令之前，系統會提示您確認。若要隱藏確認提示，請使用 -Force 切換變數。**  

```
Remove-KINStream -StreamName "mystream"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DeleteStream](https://docs.aws.amazon.com/powershell/v5/reference)。

### `Write-KINRecord`
<a name="kinesis_PutRecord_powershell_topic"></a>

以下程式碼範例顯示如何使用 `Write-KINRecord`。

**Tools for PowerShell V5**  
**範例 1：撰寫包含提供給 -Text 參數之字串的記錄。**  

```
Write-KINRecord -Text "test data from string" -StreamName "mystream" -PartitionKey "Key1"
```
**範例 2：寫入包含在指定檔案中的資料的記錄。該檔案會被視為位元組序列，因此如果其包含文字，則應在搭配此 Cmdlet 使用之前，使用任何必要的編碼進行編寫。**  

```
Write-KINRecord -FilePath "C:\TestData.txt" -StreamName "mystream" -PartitionKey "Key2"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [PutRecord](https://docs.aws.amazon.com/powershell/v5/reference)。