使用 Kinesis Data Firehose API 和AmazonSDK for PHP 版本 3 - Amazon适用于 PHP 的开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Kinesis Data Firehose API 和AmazonSDK for PHP 版本 3

Amazon Kinesis Data Firehose 使您能够将实时数据发送到其他Amazon服务,包括 Amazon Kinesis Data Streams、亚马逊 S3Amazon OpenSearch Service(OpenSearch Service) 和 Amazon Redshift,或者到斯普伦克。使用传输流创建一个数据创建器,以在您每次添加数据时将数据传送到配置的目的地。

以下示例演示如何:

所有示例AmazonSDK for PHP 版本 3(位于 GitHub 上).

Credentials

运行示例代码之前,请配置Amazon凭证,如的凭证AmazonSDK for PHP 版本 3. 然后导入AmazonSDK for PHP,如的基本使用模式AmazonSDK for PHP 版本 3.

有关使用 Amazon Kinesis Data Firehose 的更多信息,请参阅Amazon Kinesis Data Firehose 开发人员指南.

使用 Kinesis 数据流创建传输流

要建立一个将数据放入现有 Kinesis 数据流的传输流,请使用CreateDeliveryStreamoperation.

这使开发人员能够将现有 Kinesis 服务迁移到 Kinesis Data Firehose。

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_stream_name"; $stream_type = "KinesisStreamAsSource"; $kinesis_stream = "arn:aws:kinesis:us-east-2:0123456789:stream/my_stream_name"; $role = "arn:aws:iam::0123456789:policy/Role"; try { $result = $firehoseClient->createDeliveryStream([ 'DeliveryStreamName' => $name, 'DeliveryStreamType' => $stream_type, 'KinesisStreamSourceConfiguration' => [ 'KinesisStreamARN' => $kinesis_stream, 'RoleARN' => $role, ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

使用 Amazon S3 存储桶创建传输流

要建立一个将数据放入现有 Amazon S3 存储桶的传输流,请使用CreateDeliveryStreamoperation.

提供目标参数,如目标参数中所述。然后,确保您向 Kinesis Data Firehose 授予对 Amazon S3 存储桶的访问权限,如向 Kinesis Data Firehose 授予对 Amazon S3 目标的访问权限.

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_S3_stream_name"; $stream_type = "DirectPut"; $s3bucket = 'arn:aws:s3:::bucket_name'; $s3Role = 'arn:aws:iam::0123456789:policy/Role'; try { $result = $firehoseClient->createDeliveryStream([ 'DeliveryStreamName' => $name, 'DeliveryStreamType' => $stream_type, 'S3DestinationConfiguration' => [ 'BucketARN' => $s3bucket, 'CloudWatchLoggingOptions' => [ 'Enabled' => false, ], 'RoleARN' => $s3Role ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

使用 OpenSearch Service 创建传输流

要建立 Kinesis Data Firehose 传输流,该流将数据放入OpenSearch Service群集,请使用CreateDeliveryStreamoperation.

提供目标参数,如目标参数中所述。确保您授予 Kinesis Data Firehose 访问您的OpenSearch Service群集,如向 Kinesis Data Firehose 授予对 Amazon ES 目标的访问权限.

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_ES_stream_name"; $stream_type = "DirectPut"; $esDomainARN = 'arn:aws:es:us-east-2:0123456789:domain/Name'; $esRole = 'arn:aws:iam::0123456789:policy/Role'; $esIndex = 'root'; $esType = 'PHP_SDK'; $s3bucket = 'arn:aws:s3:::bucket_name'; $s3Role = 'arn:aws:iam::0123456789:policy/Role'; try { $result = $firehoseClient->createDeliveryStream([ 'DeliveryStreamName' => $name, 'DeliveryStreamType' => $stream_type, 'ElasticsearchDestinationConfiguration' => [ 'DomainARN' => $esDomainARN, 'IndexName' => $esIndex, 'RoleARN' => $esRole, 'S3Configuration' => [ 'BucketARN' => $s3bucket, 'CloudWatchLoggingOptions' => [ 'Enabled' => false, ], 'RoleARN' => $s3Role, ], 'TypeName' => $esType, ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

检索传输流

要获取有关现有 Kinesis Data Firehose 传输流的详细信息,请使用DescribeDeliveryStreamoperation.

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_stream_name"; try { $result = $firehoseClient->describeDeliveryStream([ 'DeliveryStreamName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出已连接到 Kinesis 数据流的现有传输流

要列出所有将数据发送到 Kinesis Data Firehose 的现有传输流,请使用ListDeliveryStreamsoperation.

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); try { $result = $firehoseClient->listDeliveryStreams([ 'DeliveryStreamType' => 'KinesisStreamAsSource', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出将数据发送到其他 Amazon 服务的现有传输流

要列出所有将数据发送到 Amazon S的现有 Kinesis Data Firehose 传输流,OpenSearch Service或者 Amazon Redshift,或者对于 Splunk,请使用ListDeliveryStreamsoperation.

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); try { $result = $firehoseClient->listDeliveryStreams([ 'DeliveryStreamType' => 'DirectPut', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

将数据发送到现有 Kinesis Data Firehose 传输流

要将数据通过 Kinesis Data Firehose 传输流发送到指定的目的地,请使用PutRecord操作后,您可以创建 Kinesis Data Firehose 传输流。

将数据发送到 Kinesis Data Firehose 传输流之前,请使用DescribeDeliveryStream来了解该传输流是否处于活动状态。

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_stream_name"; $content = '{"ticker_symbol":"QXZ", "sector":"HEALTHCARE", "change":-0.05, "price":84.51}'; try { $result = $firehoseClient->putRecord([ 'DeliveryStreamName' => $name, 'Record' => [ 'Data' => $content, ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

删除 Kinesis Data Firehose 传输流

要删除 Kinesis Data Firehose 传输流,请使用DeleteDeliveryStreamsoperation. 这也将删除您已发送到该传输流的所有数据。

导入

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

示例代码

$firehoseClient = new Aws\Firehose\FirehoseClient([ 'profile' => 'default', 'version' => '2015-08-04', 'region' => 'us-east-2' ]); $name = "my_stream_name"; try { $result = $firehoseClient->deleteDeliveryStream([ 'DeliveryStreamName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }