View a markdown version of this page

使用 PutRecords 用 Amazon SDK 或 CLI - Amazon Kinesis Data Streams
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

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

使用 PutRecords 用 Amazon SDK 或 CLI

以下代码示例显示了如何使用 PutRecords。

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

CLI
Amazon CLI

将多条记录写入一个数据流中

以下 put-records 示例在一次调用中使用指定的分区键写入一条数据记录,并使用不同的分区键写入另一条数据记录。这个 --cli-binary-format raw-in-base64-out 选项将数据作为原始文本传递,而不是 base64。

aws kinesis put-records \ --stream-name samplestream \ --cli-binary-format raw-in-base64-out \ --records Data=blob1,PartitionKey=partitionkey1 Data=blob2,PartitionKey=partitionkey2

输出:

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

欲了解更多信息,请参阅 使用 Amazon Kinesis 数据流 API 开发生产者 Amazon Java 软件开发工具包 在 亚马逊 Kinesis 数据流开发者指南。

  • 有关 API 的详细信息,请参阅 PutRecords 在 Amazon CLI 命令参考。

JavaScript
适用于 SDK JavaScript (v3)
注意

还有更多关于 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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 "node:url"; import { parseArgs } from "node: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 的详细信息,请参阅 PutRecords 在 适用于 JavaScript 的 Amazon SDK API 参考。

Python
适用于 Python 的 SDK(Boto3)
注意

还有更多关于 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

def put_records(self, stream_name, records): """ Writes multiple data records into a Kinesis data stream in a single call. :param stream_name: The name of the stream to write records to. :param records: A list of dicts, each with 'Data' (bytes) and 'PartitionKey' (str). :return: The response from the PutRecords call. """ try: response = self.kinesis_client.put_records( StreamName=stream_name, Records=records ) failed_count = response.get("FailedRecordCount", 0) logger.info( "Put %s record(s) to stream '%s' with %s failure(s).", len(records), stream_name, failed_count, ) return response except ClientError as err: if err.response["Error"]["Code"] == "InvalidArgumentException": logger.error( "Invalid argument for PutRecords. Check record sizes and parameters." ) raise
  • 有关 API 的详细信息,请参阅 PutRecords 在 Amazon 适用于 Python 的 SDK (Boto3) API 参考。

如需查看完整清单 Amazon SDK 开发者指南和代码示例,请参阅 将此服务与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。