使用适用于 Java 的 SDK 1.x 查询全局二级索引
可以使用 Amazon DynamoDB Accelerator (DAX) 查询使用 DynamoDB 编程接口的全局二级索引。
下面的示例演示了如何使用 DAX 查询在示例:使用 CreateDateIndex
文档 API 的全局二级索引中创建的 AWS SDK for Java 全局二级索引。
DAXClient
类用于实例化与 DynamoDB 编程接口进行交互所需的客户端对象。
import com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.util.EC2MetadataUtils; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; public class DaxClient { private static final String region = EC2MetadataUtils.getEC2InstanceRegion(); DynamoDB getDaxDocClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client = daxClientBuilder.build(); return new DynamoDB(client); } DynamoDBMapper getDaxMapperClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client = daxClientBuilder.build(); return new DynamoDBMapper(client); } }
可以通过以下方式查询全局二级索引:
-
使用下面示例中定义的
QueryIndexDax
类的queryIndex
方法。QueryIndexDax
接受由DaxClient
类的getDaxDocClient
方法返回的客户端对象作为参数。 -
如果使用对象持久化接口,请使用下面示例定义的
QueryIndexDax
类的queryIndexMapper
方法。queryIndexMapper
接受由DaxClient
类定义的getDaxMapperClient
方法返回的客户端对象作为参数。
import java.util.Iterator; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; import java.util.List; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import java.util.HashMap; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; import com.amazonaws.services.dynamodbv2.document.QueryOutcome; import com.amazonaws.services.dynamodbv2.document.ItemCollection; import com.amazonaws.services.dynamodbv2.document.Index; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.DynamoDB; public class QueryIndexDax { //This is used to query Index using the low-level interface. public static void queryIndex(DynamoDB client, String tableName, String indexName) { Table table = client.getTable(tableName); System.out.println("\n***********************************************************\n"); System.out.print("Querying index " + indexName + "..."); Index index = table.getIndex(indexName); ItemCollection<QueryOutcome> items = null; QuerySpec querySpec = new QuerySpec(); if (indexName == "CreateDateIndex") { System.out.println("Issues filed on 2013-11-01"); querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)") .withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-")); items = index.query(querySpec); } else { System.out.println("\nNo valid index name provided"); return; } Iterator<Item> iterator = items.iterator(); System.out.println("Query: printing results..."); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } } //This is used to query Index using the high-level mapper interface. public static void queryIndexMapper(DynamoDBMapper mapper, String tableName, String indexName) { HashMap<String, AttributeValue> eav = new HashMap<String, AttributeValue>(); eav.put(":v_date", new AttributeValue().withS("2013-11-01")); eav.put(":v_issue", new AttributeValue().withS("A-")); DynamoDBQueryExpression<CreateDate> queryExpression = new DynamoDBQueryExpression<CreateDate>() .withIndexName("CreateDateIndex").withConsistentRead(false) .withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)") .withExpressionAttributeValues(eav); List<CreateDate> items = mapper.query(CreateDate.class, queryExpression); Iterator<CreateDate> iterator = items.iterator(); System.out.println("Query: printing results..."); while (iterator.hasNext()) { CreateDate iterObj = iterator.next(); System.out.println(iterObj.getCreateDate()); System.out.println(iterObj.getIssueId()); } } }
下面的类定义表示问题表,用于 queryIndexMapper
方法。
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; @DynamoDBTable(tableName = "Issues") public class CreateDate { private String createDate; @DynamoDBHashKey(attributeName = "IssueId") private String issueId; @DynamoDBIndexHashKey(globalSecondaryIndexName = "CreateDateIndex", attributeName = "CreateDate") public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } @DynamoDBIndexRangeKey(globalSecondaryIndexName = "CreateDateIndex", attributeName = "IssueId") public String getIssueId() { return issueId; } public void setIssueId(String issueId) { this.issueId = issueId; } }