DynamoDB - 查询
查询通过主键查找项目或二级索引。 执行查询需要分区键和具体值,或者排序键和值; 可以选择通过比较进行过滤。 查询的默认行为包括返回与所提供的主键关联的项目的每个属性。 但是,您可以使用 ProjectionExpression 参数指定所需的属性。
查询使用KeyConditionExpression参数来选择项目,这需要以相等条件的形式提供分区键名称和值。 您还可以选择为存在的任何排序键提供附加条件。
排序键条件的一些示例是 −
| Sr.No | 条件和描述 |
|---|---|
| 1 | x = y 如果属性 x 等于 y,则计算结果为 true。 |
| 2 | x < y 如果 x 小于 y,则计算结果为 true。 |
| 3 | x <= y 如果 x 小于或等于 y,则计算结果为 true。 |
| 4 | x > y 如果 x 大于 y,则计算结果为 true。 |
| 5 | x >= y 如果 x 大于或等于 y,则计算结果为 true。 |
| 6 | x BETWEEN y AND z 如果 x 都是 >= y, 且 <= z,则计算结果为 true。 |
DynamoDB 还支持以下函数:begins_with (x, substr)
如果属性 x 以指定字符串开头,则计算结果为 true。
以下条件必须符合一定要求 −
属性名称必须以 a-z 或 A-Z 集中的字符开头。
属性名称的第二个字符必须属于 a-z、A-Z 或 0-9 集合。
属性名称不能使用保留字。
不符合上述约束的属性名称可以定义占位符。
查询处理通过按排序键顺序执行检索并使用存在的任何条件和过滤表达式来进行。 查询总是返回一个结果集,如果没有匹配,则返回一个空结果集。
结果始终按排序键顺序和基于数据类型的顺序返回,并以可修改的默认值作为升序。
使用 Java 查询
Java 中的查询允许您查询表和二级索引。 它们需要指定分区键和相等条件,并可以选择指定排序键和条件。
Java 查询一般需要的步骤包括创建 DynamoDB 类实例、目标表的 Table 类实例,以及调用 Table 实例的查询方法来接收查询对象。
对查询的响应包含一个 ItemCollection 对象,提供所有返回的项目。
下面的例子演示了详细的查询 −
DynamoDB dynamoDB = new DynamoDB (
new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
Table table = dynamoDB.getTable("Response");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("ID = :nn")
.withValueMap(new ValueMap()
.withString(":nn", "Product Line 1#P1 Thread 1"));
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.toJSONPretty());
}
查询方法支持多种可选参数。 下面的例子演示了如何使用这些参数 −
Table table = dynamoDB.getTable("Response");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("ID = :nn and ResponseTM > :nn_responseTM")
.withFilterExpression("Author = :nn_author")
.withValueMap(new ValueMap()
.withString(":nn", "Product Line 1#P1 Thread 1")
.withString(":nn_responseTM", twoWeeksAgoStr)
.withString(":nn_author", "Member 123"))
.withConsistentRead(true);
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
您还可以查看以下更大的示例。
注意 − 以下程序可能假定先前创建的数据源。 在尝试执行之前,获取支持库并创建必要的数据源(具有所需特征的表或其他引用源)。
此示例还使用 Eclipse IDE、AWS 凭证文件以及 Eclipse AWS Java 项目中的 AWS Toolkit。
package com.amazonaws.codesamples.document;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.Page;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
public class QueryOpSample {
static DynamoDB dynamoDB = new DynamoDB(
new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
static String tableName = "Reply";
public static void main(String[] args) throws Exception {
String forumName = "PolyBlaster";
String threadSubject = "PolyBlaster Thread 1";
getThreadReplies(forumName, threadSubject);
}
private static void getThreadReplies(String forumName, String threadSubject) {
Table table = dynamoDB.getTable(tableName);
String replyId = forumName + "#" + threadSubject;
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("Id = :v_id")
.withValueMap(new ValueMap()
.withString(":v_id", replyId));
ItemCollection<QueryOutcome> items = table.query(spec);
System.out.println("
getThreadReplies results:");
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
}

