在 DynamoDB 中批量读取和写入项目 - Amazon SDK for JavaScript
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon SDK for JavaScript V3 API 参考指南详细描述了 Amazon SDK for JavaScript 版本 3 (V3) 的所有 API 操作。

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

在 DynamoDB 中批量读取和写入项目

JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 如何在 DynamoDB 表中批量读取和写入项目。

情景

在本示例中,您使用一系列 Node.js 模块在 DynamoDB 表中批量放置项目以及批量读取项目。该代码使用 SDK for JavaScript,通过 DynamoDB 客户端类的下列方法来执行批量读取和写入操作:

完成先决条件任务

要设置和运行此示例,请先完成以下任务:

  • 设置项目环境以运行这些 Node TypeScript 示例,并安装所需的 Amazon SDK for JavaScript 和第三方模块。请按照 GitHub 上的说明进行操作。

  • 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅《Amazon SDK 和工具参考指南》中的共享配置和凭证文件

  • 创建一个可访问其项目的 DynamoDB 表。有关创建 DynamoDB 表的更多信息,请参阅在 DynamoDB 中创建和使用表

重要

这些示例使用 ECMAScript6 (ES6)。这需要使用 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅 Node.js 下载

但是,如果您更喜欢使用 CommonJS 语法,请参阅 JavaScript ES6/CommonJS 语法

注意

有关这些示例中使用的数据类型的信息,请参阅 Amazon DynamoDB 中支持的数据类型和命名规则

批量读取项目

创建文件名为 batch-get-item.js 的 Node.js 模块。请务必如前所示配置 SDK,包括下载所需的客户端和软件包。要访问 DynamoDB,请创建一个 DynamoDB 客户端服务对象。创建一个 JSON 对象,其中包含批量获取项目所需的参数,在此示例中包括要读取的一个或多个表的名称,在各个表中要读取的键的值,以及指定要返回的属性的投影表达式。调用 DynamoDB 服务对象的 BatchGetItemCommand 方法。

import { BatchGetItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchGetItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a PageAnalytics table. PageAnalytics: { // Each entry in Keys is an object that specifies a primary key. Keys: [ { // "PageName" is the partition key (simple primary key). // "S" specifies a string as the data type for the value "Home". // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors PageName: { S: "Home" }, }, { PageName: { S: "About" }, }, ], // Only return the "PageName" and "PageViews" attributes. ProjectionExpression: "PageName, PageViews", }, }, }); const response = await client.send(command); console.log(response.Responses["PageAnalytics"]); return response; };

要运行示例,请在命令提示符中键入以下内容。

node batch-get-item.js

此示例代码可在 GitHub 上的此处找到。

批量写入项目

创建文件名为 batch-write-item.js 的 Node.js 模块。请务必如前所示配置 SDK,包括下载所需的客户端和软件包。要访问 DynamoDB,请创建一个 DynamoDB 客户端服务对象。创建一个 JSON 对象,其中包含批量获取项目所需的参数,在本示例中包括要写入项目的表,要写入的各个项目的键,以及属性及值。调用 DynamoDB 服务对象的 BatchWriteItemCommand 方法。

import { BatchWriteItemCommand, DynamoDBClient, } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchWriteItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a Coffees table. Coffees: [ // Each entry in Coffees is an object that defines either a PutRequest or DeleteRequest. { // Each PutRequest object defines one item to be inserted into the table. PutRequest: { // The keys of Item are attribute names. Each attribute value is an object with a data type and value. // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes Item: { Name: { S: "Donkey Kick" }, Process: { S: "Wet-Hulled" }, Flavors: { SS: ["Earth", "Syrup", "Spice"] }, }, }, }, { PutRequest: { Item: { Name: { S: "Flora Ethiopia" }, Process: { S: "Washed" }, Flavors: { SS: ["Stone Fruit", "Toasted Almond", "Delicate"] }, }, }, }, ], }, }); const response = await client.send(command); console.log(response); return response; };

要运行示例,请在命令提示符中键入以下内容。

node batch-write-item.js

此示例代码可在 GitHub 上的此处找到。