DynamoDB – 查询
DynamoDB – 查询
查询通过主键定位项目或二级索引。执行查询需要分区键和特定值,或排序键和值;可以选择通过比较进行过滤。查询的默认行为包括返回与提供的主键关联的项目的每个属性。但是,您可以使用ProjectionExpression参数指定所需的属性。
查询使用KeyConditionExpression参数来选择项目,这需要以相等条件的形式提供分区键名称和值。您还可以选择为存在的任何排序键提供附加条件。
排序键条件的一些示例是 –
| Sr.No | 条件和描述 |
|---|---|
| 1 |
x = y 如果属性 x 等于 y,则计算结果为真。 |
| 2 |
x < y 如果 x 小于 y,则计算结果为真。 |
| 3 |
x <= y 如果 x 小于或等于 y,则计算结果为真。 |
| 4 |
x > y 如果 x 大于 y,则计算结果为真。 |
| 5 |
x >= y 如果 x 大于或等于 y,则计算结果为真。 |
| 6 |
x BETWEEN y AND z 如果 x 既 >= y,又 <= z,则计算结果为真。 |
DynamoDB 还支持以下函数:begins_with (x, substr)
如果属性 x 以指定的字符串开头,则计算结果为真。
以下条件必须符合某些要求 –
-
属性名称必须以 az 或 AZ 集中的字符开头。
-
属性名称的第二个字符必须属于 az、AZ 或 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 工具包。
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("\ngetThreadReplies results:");
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
}