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

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

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

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

以下示例演示如何:

Amazon SDK for PHPGitHub 上提供了的所有示例代码。

凭证

运行示例代码之前,请配置您的Amazon凭证,如所述设置 凭证. 然后导入Amazon SDK for PHP,如所述基本用法.

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

使用 Kinesis Data 流创建传输流

要建立一个将数据放入现有 Kinesis Data 流的传输流,请使用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.

提供目标参数,如目标参数中所述。然后,确保您向授予对 Amazon S3 存储桶的 Kinesis Data Firehose 访问权限,如所述。授予对 Amazon S3 目标的 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_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 服务

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

提供目标参数,如目标参数中所述。确保您授予 Kinesis Data Firehose 访问 OpenSearch 服务群集,如中所述向 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 Data 流的现有传输流

要列出所有现有的 Kinesis Data Firehose 传输流将数据发送到 Kinesis Data Streams,请使用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 S3 的现有 Kinesis Data Firehose 传输流,请执行以下操作: OpenSearch 服务,或者 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"; }