与 AWS SDK或PutRecords一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或PutRecords一起使用 CLI

以下代码示例演示如何使用 PutRecords

CLI
AWS CLI

将多条记录写入数据流

以下put-records示例使用指定的分区键写入数据记录,在一次调用中使用不同的分区键写入另一条数据记录。

aws kinesis put-records \ --stream-name samplestream \ --records Data=blob1,PartitionKey=partitionkey1 Data=blob2,PartitionKey=partitionkey2

输出:

{ "FailedRecordCount": 0, "Records": [ { "SequenceNumber": "49600883331171471519674795588238531498465399900093808706", "ShardId": "shardId-000000000004" }, { "SequenceNumber": "49600902273357540915989931256902715169698037101720764562", "ShardId": "shardId-000000000009" } ], "EncryptionType": "KMS" }

有关更多信息,请参阅亚马逊 Kinesis Data Streams 开发者指南中的使用亚马逊 Kinesis Data API Streams 和 Java 版开发生产。 AWS SDK

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import { PutRecordsCommand, KinesisClient } from "@aws-sdk/client-kinesis"; /** * Put multiple records into a Kinesis stream. * @param {{ streamArn: string }} config */ export const main = async ({ streamArn }) => { const client = new KinesisClient({}); try { await client.send( new PutRecordsCommand({ StreamARN: streamArn, Records: [ { Data: new Uint8Array(), /** * Determines which shard in the stream the data record is assigned to. * Partition keys are Unicode strings with a maximum length limit of 256 * characters for each key. Amazon Kinesis Data Streams uses the partition * key as input to a hash function that maps the partition key and * associated data to a specific shard. */ PartitionKey: "TEST_KEY", }, { Data: new Uint8Array(), PartitionKey: "TEST_KEY", }, ], }), ); } catch (caught) { if (caught instanceof Error) { // } else { throw caught; } } }; // Call function if run directly. import { fileURLToPath } from "url"; import { parseArgs } from "util"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const options = { streamArn: { type: "string", description: "The ARN of the stream.", }, }; const { values } = parseArgs({ options }); main(values); }
  • 有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 PutRecords” 中的。