Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

查询表

另一个常见访问模式是根据您的查询条件从表中读取多个项目。

SQL

SQL SELECT 语句可让您查询关键列、非关键列或任意组合。WHERE 子句确定返回的行,如以下示例所示:

/* Return a single song, by primary key */ SELECT * FROM Music WHERE Artist='No One You Know' AND SongTitle = 'Call Me Today';
/* Return all of the songs by an artist */ SELECT * FROM Music WHERE Artist='No One You Know';
/* Return all of the songs by an artist, matching first part of title */ SELECT * FROM Music WHERE Artist='No One You Know' AND SongTitle LIKE 'Call%';
/* Return all of the songs by an artist, with a particular word in the title... ...but only if the price is less than 1.00 */ SELECT * FROM Music WHERE Artist='No One You Know' AND SongTitle LIKE '%Today%' AND Price < 1.00;

请注意,此表的主键包含 ArtistSongTitle

DynamoDB

DynamoDB Query 操作可让您以类似方式检索数据。Query 操作提供对存储数据的物理位置的快速高效访问。 (有关更多信息,请参阅 分区和数据分配。)

可以将 Query 用于任何具有复合主键 (分区键和排序键) 的表。您必须指定分区键的相等条件,并且可以选择性为排序键提供另一个条件。

KeyConditionExpression 参数指定要查询的键值。可使用可选 FilterExpression 在结果中的某些项目返回给您之前删除这些项目。

在 DynamoDB 中,您必须使用 ExpressionAttributeValues 作为表达式参数 (例如,KeyConditionExpressionFilterExpression) 中的占位符。这类似于在关系数据库中使用绑定变量,其中,您在运行时将实际值代入 SELECT 语句。

请注意,此表的主键包含 ArtistSongTitle

以下是 DynamoDB 中的几个 Query 示例:

// Return a single song, by primary key { TableName: "Music", KeyConditionExpression: "Artist = :a and SongTitle = :t", ExpressionAttributeValues: { ":a": "No One You Know", ":t": "Call Me Today" } }
// Return all of the songs by an artist { TableName: "Music", KeyConditionExpression: "Artist = :a", ExpressionAttributeValues: { ":a": "No One You Know" } }
// Return all of the songs by an artist, matching first part of title { TableName: "Music", KeyConditionExpression: "Artist = :a and begins_with(SongTitle, :t)", ExpressionAttributeValues: { ":a": "No One You Know", ":t": "Call" } }
// Return all of the songs by an artist, with a particular word in the title... // ...but only if the price is less than 1.00 { TableName: "Music", KeyConditionExpression: "Artist = :a and contains(SongTitle, :t)", FilterExpression: "price < :p", ExpressionAttributeValues: { ":a": "No One You Know", ":t": "Today", ":p": 1.00 } }

注意

有关使用 Query 的代码示例,请参阅 DynamoDB 入门

本页内容: