Uso de PutRecords con un SDK de AWS o la CLI - Ejemplos de código de AWS SDK

Hay más ejemplos de AWS SDK disponibles en el repositorio de GitHub de ejemplos de AWS SDK de documentos.

Uso de PutRecords con un SDK de AWS o la CLI

Los siguientes ejemplos de código muestran cómo utilizar PutRecords.

CLI
AWS CLI

Para escribir varios registros en un flujo de datos

En el siguiente ejemplo de put-records, se escribe un registro de datos con la clave de partición especificada y otro registro de datos con una clave de partición diferente en una sola llamada.

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

Salida:

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

Para obtener más información, consulte Desarrollo de productores mediante la API de Kinesis Data Streams con el SDK de AWS para Java en la Guía para desarrolladores de Amazon Kinesis Data Streams.

  • Para obtener información sobre la API, consulte PutRecords en la Referencia de comandos de la AWS CLI.

JavaScript
SDK para JavaScript (v3)
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 "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); }
  • Para obtener información sobre la API, consulte PutRecords en la Referencia de la API de AWS SDK para JavaScript.