开发手册 欢迎您!
软件开发者资料库

DynamoDB - 查询

DynamoDB查询 - 从简单和简单的步骤学习DynamoDB,从基本到高级概念,包括概述,基本概念,环境,操作工具,数据类型,创建,加载,查询,删除表,API接口,创建,获取,更新,删除项目,批量写入,批量检索,查询,扫描,索引,全局,本地二级索引,聚合,访问控制,权限API,条件,Web身份联合,数据管道,数据备份,监控,CloudTrail,MapReduce,表活动,错误处理,最佳实践。

查询通过主键查找项目或辅助索引.执行查询需要分区键和特定值,或排序键和值;可以选择过滤比较.查询的默认行为包括返回与提供的主键关联的项的每个属性.但是,您可以使用 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还支持以下函数: starts_with(x,substr)

如果属性x以指定的字符串开头,则计算结果为true.

以下条件必须符合某些要求 :

  • 属性名称必须以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 items = table.query(spec);  Iterator 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 items = table.query(spec);  Iterator 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 items = table.query(spec);       System.out.println("\ngetThreadReplies results:");       Iterator iterator = items.iterator();             while (iterator.hasNext()) {          System.out.println(iterator.next().toJSONPretty());       }    } }
dy>