使用 Kinesis 資料串流 API 和第 3 AWS SDK for PHP 版建立資料串流 - AWS SDK for PHP

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

使用 Kinesis 資料串流 API 和第 3 AWS SDK for PHP 版建立資料串流

Amazon Kinesis Data Streams 可讓您傳送即時資料。使用 Kinesis Data Streams 建立資料生產者,每次新增資料時都會將資料傳送到設定的目的地。

如需詳細資訊,請參閱 Amazon Kinesis 開發人員指南中的建立和管理串流

下列範例示範如何:

所有的範例程式碼都可以AWS SDK for PHP在這裡取得 GitHub。

登入資料

在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用

如需使用 Amazon Kinesis 開發人員指南的詳細資訊,請參閱 Amazon Kinesis Data Streams 開發人員指南。

使用 Kinesis 資料串流建立資料串流

建立 Kinesis 資料串流,您可以在其中使用下列程式碼範例傳送 Kinesis 要處理的資訊。請參閱 Amazon Kinesis 開發人員指南,進一步了解如何建立和更新資料串流

若要建立 Kinesis 資料串流,請使用此作CreateStream業。

匯入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

範例程式碼

$kinesisClient = new Aws\Kinesis\KinesisClient([ 'profile' => 'default', 'version' => '2013-12-02', 'region' => 'us-east-2' ]); $shardCount = 2; $name = "my_stream_name"; try { $result = $kinesisClient->createStream([ 'ShardCount' => $shardCount, 'StreamName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

擷取資料串流

使用以下程式碼範例,取得現有資料串流的詳細資訊。依預設,這會傳回連線至指定 Kinesis 資料串流的前 10 個碎片的相關資訊。請記得在將資料寫入 Kinesis 資料串流之前,先檢StreamStatus查回應。

若要擷取有關指定 Kinesis 資料串流的詳細資訊,請使用此DescribeStream作業。

匯入

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->describeStream([ 'StreamName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出連線到 Kinesis 的現有資料串流

列出您在所選AWS區域AWS 帳戶中的前 10 個資料串流。使用傳回的 `HasMoreStreams 判斷您的帳戶是否還有更多相關聯的串流。

若要列出您的 Kinesis 資料串流,請使用此ListStreams作業。

匯入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

範例程式碼

$kinesisClient = new Aws\Kinesis\KinesisClient([ 'profile' => 'default', 'version' => '2013-12-02', 'region' => 'us-east-2' ]); try { $result = $kinesisClient->listStreams(); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

將資料傳送至現有的資料串流

建立資料串流之後,使用以下範例傳送資料。傳送資料之前,使用 DescribeStream 檢查該資料的 StreamStatus 是否為作用中。

若要將單一資料記錄寫入 Kinesis 資料串流,請使用此PutRecord作業。若要將最多 500 筆記錄寫入 Kinesis 資料串流,請使用此PutRecords作業。

匯入

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

範例程式碼

$kinesisClient = new Aws\Kinesis\KinesisClient([ 'profile' => 'default', 'version' => '2013-12-02', 'region' => 'us-east-1' ]); $name = "my_stream_name"; $content = '{"ticker_symbol":"QXZ", "sector":"HEALTHCARE", "change":-0.05, "price":84.51}'; $groupID = "input to a hash function that maps the partition key (and associated data) to a specific shard"; try { $result = $kinesisClient->PutRecord([ 'Data' => $content, 'StreamName' => $name, 'PartitionKey' => $groupID ]); print("<p>ShardID = " . $result["ShardId"] . "</p>"); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

刪除資料串流

本範例示範如何刪除資料串流。刪除資料串流也會一併刪除您已傳送至該資料串流的任何資料。作用中 Kinesis 資料串流會切換到刪除狀態,直到串流刪除完成為止。若處於 DELETING 狀態,串流仍會繼續處理資料。

若要刪除 Kinesis 資料串流,請使用此作DeleteStream業。

匯入

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->deleteStream([ 'StreamName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }