

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

# 使用 Kinesis Data Streams API 和 适用于 PHP 的 AWS SDK 版本 3 来创建数据流
<a name="kinesis-example-data-stream"></a>

Amazon Kinesis Data Streams 允许发送实时数据。使用 Kinesis Data Streams 来创建数据创建器，该创建器在每次添加数据时将数据发送到配置的目的地。

有关更多信息，请参阅 Amazon Kinesis 开发人员指南中的[创建和管理流](https://docs.aws.amazon.com/kinesis/latest/dev/working-with-streams.html.html)。

以下示例演示如何：
+ 使用 [CreateAlias](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#createstream) 创建数据流。
+ 使用 [DescribeStream](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#describestream) 获取有关单个数据流的详细信息。
+ 使用 [ListStreams](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#liststreams) 列出现有数据流。
+ 使用 [PutRecord](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#putrecord) 将数据发送到现有数据流。
+ 使用 [DeleteStream](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kinesis-1913-12-02.html#deletestream) 删除数据流。

适用于 PHP 的 AWS SDKGitHub[ 上提供了](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)的所有示例代码。

## 凭证
<a name="examplecredentials"></a>

运行示例代码之前，请配置您的 AWS 凭证，如 [AWS 使用 适用于 PHP 的 AWS SDK 版本 3 进行身份验证](credentials.md) 中所述。然后导入 适用于 PHP 的 AWS SDK，如 [安装 适用于 PHP 的 AWS SDK 版本 3](getting-started_installation.md) 中所述。

有关使用 Amazon Kinesis 开发人员指南的更多信息，请参阅 [Amazon Kinesis Data Streams 开发人员指南](https://docs.aws.amazon.com/kinesis/latest/dev/)。

## 使用 Kinesis 数据流来创建数据流
<a name="create-a-data-stream-using-a-ak-data-stream"></a>

建立 Kinesis 数据流，您可以使用以下代码示例来在其中发送要由 Kinesis 处理的信息。要了解更多信息，请参阅 Amazon Kinesis 开发人员指南中的[创建和更新数据流](https://docs.aws.amazon.com/kinesis/latest/dev/amazon-kinesis-streams.html)。

要创建 Kinesis 数据流，请使用 [CreateStream](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html) 操作。

 **导入**。

```
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";
}
```

## 检索数据流
<a name="retrieve-a-data-stream"></a>

获取有关使用以下代码示例的现有数据流的详细信息。默认情况下，这将返回有关连接到指定 Kinesis 数据流的前 10 个分片的信息。在将数据写入 Kinesis 数据流之前，请记住检查响应中的 `StreamStatus`。

要检索有关指定 Kinesis 数据流的详细信息，请使用 [DescribeStream](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_DescribeStream.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->describeStream([
        'StreamName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出已连接到 Kinesis 的现有数据流
<a name="list-existing-data-streams-that-are-connected-to-ak"></a>

列出所选 AWS 区域中您 AWS 账户 的前 10 个数据流。使用返回的 ``HasMoreStreams` 来确定是否有更多与您的账户关联的流。

要列出 Kinesis 数据流，请使用 [ListStreams](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_ListStreams.html) 操作。

 **导入**。

```
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";
}
```

## 将数据发送到现有数据流
<a name="send-data-to-an-existing-data-stream"></a>

在创建数据流后，请使用以下示例来发送数据。在向其发送数据之前，请使用 `DescribeStream` 检查数据 `StreamStatus` 是否处于活动状态。

要将单条数据记录写入 Kinesis 数据流，请使用 [PutRecord](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html) 操作。要将最多 500 条记录写入 Kinesis 数据流，请使用 [PutRecords](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecords.html) 操作。

 **导入**。

```
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";
}
```

## 删除数据流
<a name="delete-a-data-stream"></a>

此示例演示如何删除数据流。删除数据流也会删除您发送到数据流的所有数据。活动 Kinesis 数据流切换到 DELETING 状态，直到完成流删除操作。在处于 DELETING 状态时，流将继续处理数据。

要删除 Kinesis 数据流，请使用 [DeleteStream](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_DeleteStream.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->deleteStream([
        'StreamName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```