

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

# 使用 Kinesis Data Streams API 和第 3 適用於 PHP 的 AWS SDK 版管理資料碎片
<a name="kinesis-example-shard"></a>

Amazon Kinesis Data Streams 可讓您將即時資料傳送至端點。資料流程速率取決於串流中的碎片數目。

每秒寫入單個碎片的記錄可多達 1,000 筆。各碎片另有每秒 1 MiB 的上傳限制。用量將按碎片數目計算和收費，所以使用這些範例可控管您的串流資料容量及成本。

下列範例示範如何：
+ 使用 [ListShards](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#listshards) 列出串流中的碎片。
+ 使用 [UpdateShardCount](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#updateshardcount) 增加或減少串流中的碎片數目。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon Kinesis Data Streams 的詳細資訊，請參閱 [Amazon Kinesis Data Streams 開發人員指南](https://docs.aws.amazon.com/streams/latest/dev/)。

## 列出資料串流碎片
<a name="list-data-stream-shards"></a>

列出特定串流中多達 100 個碎片的詳細資訊。

若要列出 Kinesis 資料串流中的碎片，請使用 [ListShards](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_ListShards.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$kinesisClient = new Aws\Kinesis\KinesisClient([
    'profile' => 'default',
    'version' => '2013-12-02',
    'region' => 'us-east-2'
]);

$name = "my_stream_name";

try {
    $result = $kinesisClient->ListShards([
        'StreamName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 新增更多資料串流碎片
<a name="add-more-data-stream-shards"></a>

如果您需要更多資料串流碎片，則可增加您目前的碎片數目。建議您將碎片數目增加為兩倍。如此會為您目前可用的每個碎片各建立一份複本以增加容量。您在 24 小時期間內可將碎片數目加倍的上限為兩次。

請記住，Kinesis Data Streams 用量的計費是以每個碎片計算，因此當需求減少時，建議您將碎片計數減少一半。移除碎片時，您可以將碎片總數僅縮減為目前碎片數目的一半。

若要更新 Kinesis 資料串流的碎片計數，請使用 [UpdateShardCount](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_UpdateShardCount.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$kinesisClient = new Aws\Kinesis\KinesisClient([
    'profile' => 'default',
    'version' => '2013-12-02',
    'region' => 'us-east-2'
]);

$name = "my_stream_name";
$totalshards = 4;

try {
    $result = $kinesisClient->UpdateShardCount([
        'ScalingType' => 'UNIFORM_SCALING',
        'StreamName' => $name,
        'TargetShardCount' => $totalshards
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```