查询并扫描 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.js 示例,并安装所需的 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 中支持的数据类型和命名规则

查询表

创建文件名为 query.js 的 Node.js 模块。请务必如前所示配置 SDK,包括下载所需的客户端和软件包。要访问 DynamoDB,请创建一个 DynamoDB 客户端服务对象。创建一个 JSON 对象,其中包含查询表所需的参数,在本示例中包括表名,查询所需的 ExpressionAttributeValues,使用这些值定义查询要返回的项目的 KeyConditionExpression,以及各个项目要返回的属性值的名称。调用 DynamoDB 服务对象的 QueryCommand 方法。

import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new QueryCommand({ KeyConditionExpression: "Flavor = :flavor", // 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 ExpressionAttributeValues: { ":flavor": { S: "Key Lime" }, ":searchKey": { S: "no coloring" }, }, FilterExpression: "contains (Description, :searchKey)", ProjectionExpression: "Flavor, CrustType, Description", TableName: "Pies", }); const response = await client.send(command); response.Items.forEach(function (pie) { console.log(`${pie.Flavor.S} - ${pie.Description.S}\n`); }); return response; };

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

node query.js

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

扫描表

创建文件名为 scan.js 的 Node.js 模块。请务必如前所示配置 SDK,包括下载所需的客户端和软件包。要访问 DynamoDB,请创建一个 DynamoDB 客户端服务对象。创建一个 JSON 对象,其中包含扫描表中项目所需的参数,在本示例中包括表的名称,各个匹配项目要返回的属性值的列表,以及用于筛选结果集来查找包含指定短语的项目的表达式。调用 DynamoDB 服务对象的 ScanCommand 方法。

import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new ScanCommand({ FilterExpression: "CrustType = :crustType", // 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 ExpressionAttributeValues: { ":crustType": { S: "Graham Cracker" }, }, ProjectionExpression: "Flavor, CrustType, Description", TableName: "Pies", }); const response = await client.send(command); response.Items.forEach(function (pie) { console.log(`${pie.Flavor.S} - ${pie.Description.S}\n`); }); return response; };

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

node scan.js

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