Dynamodb 简明教程
DynamoDB - Querying
查询会通过主键查找项目或辅助索引。执行查询要求使用分区键和特定值,或排序键和值;可以选择通过比较进行筛选。查询的默认执行过程是针对与提供的主键关联的项目返回每个属性。但你可以使用 ProjectionExpression 参数指定所需的属性。
Queries locate items or secondary indices through primary keys. Performing a query requires a partition key and specific value, or a sort key and value; with the option to filter with comparisons. The default behavior of a query consists of returning every attribute for items associated with the provided primary key. However, you can specify the desired attributes with the ProjectionExpression parameter.
查询使用 KeyConditionExpression 参数来选择项目,这要求以相等条件的形式提供分区键名称和值。你还可以选择为任何存在的排序键提供其他条件。
A query utilizes the KeyConditionExpression parameters to select items, which requires providing the partition key name and value in the form of an equality condition. You also have the option to provide an additional condition for any sort keys present.
下面列出了排序键条件的几个示例 −
A few examples of the sort key conditions are −
Sr.No |
Condition & Description |
1 |
x = y It evaluates to true if the attribute x equals y. |
2 |
x < y It evaluates to true if x is less than y. |
3 |
x ⇐ y It evaluates to true if x is less than or equal to y. |
4 |
x > y It evaluates to true if x is greater than y. |
5 |
x >= y It evaluates to true if x is greater than or equal to y. |
6 |
x BETWEEN y AND z It evaluates to true if x is both >= y, and ⇐ z. |
DynamoDB 还支持以下函数: begins_with (x, substr)
DynamoDB also supports the following functions: begins_with (x, substr)
如果属性 x 以指定字符串开头,则求值为真。
It evaluates to true if attribute x starts with the specified string.
以下条件必须符合某些要求 −
The following conditions must conform to certain requirements −
-
Attribute names must start with a character within the a-z or A-Z set.
-
The second character of an attribute name must fall in the a-z, A-Z, or 0-9 set.
-
Attribute names cannot use reserved words.
不符合以上约束的属性名可以定义占位符。
Attribute names out of compliance with the constraints above can define a placeholder.
查询通过按排序键顺序执行检索,并使用任何条件和筛选器表达式来处理。查询总是返回一个结果集,如果没有匹配项,则返回一个空集。
The query processes by performing retrievals in sort key order, and using any condition and filter expressions present. Queries always return a result set, and on no matches, it returns an empty one.
结果始终按排序键顺序返回,并且基于数据类型顺序,修改后的默认值为升序。
The results always return in sort key order, and data type based order with the modifiable default as the ascending order.
Querying with Java
Java 中的查询允许您查询表和二级索引。它们要求指定分区键和相等条件,并可以选择指定排序键和条件。
Queries in Java allow you to query tables and secondary indices. They require specification of partition keys and equality conditions, with the option to specify sort keys and conditions.
在 Java 中进行查询的常规必需步骤包括:创建 DynamoDB 类实例、目标表的 Table 类实例,并调用 Table 实例的 query 方法以接收查询对象。
The general required steps for a query in Java include creating a DynamoDB class instance, Table class instance for the target table, and calling the query method of the Table instance to receive the query object.
对查询的响应包含一个 ItemCollection 对象,该对象提供所有返回的项目。
The response to the query contains an ItemCollection object providing all the returned items.
以下示例演示了详细查询 −
The following example demonstrates detailed querying −
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());
}
查询方法支持广泛的可选参数。以下示例演示如何利用这些参数 −
The query method supports a wide variety of optional parameters. The following example demonstrates how to utilize these parameters −
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());
}
您还可以查看以下更大的示例。
You can also review the following larger example.
Note − 以下程序可能会假设有一个已创建的数据源。在尝试执行之前,获得支持库并创建必要的数据源(具备所需特征的表格或其他引用的源)。
Note − The following program may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources).
此示例还使用了 Eclipse IDE、AWS 凭据文件和 Eclipse AWS Java 项目中的 AWS Toolkit。
This example also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project.
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());
}
}
}