SDK for Java 1.x를 사용하여 글로벌 보조 인덱스 쿼리
Amazon DynamoDB Accelerator(DAX)를 사용하면 DynamoDB 프로그래밍 인터페이스를 사용하여 글로벌 보조 인덱스를 쿼리할 수 있습니다.
다음 예에서는 DAX를 사용하여 예: AWS SDK for Java 문서 API를 사용하는 글로벌 보조 인덱스에서 생성된 CreateDateIndex
글로벌 보조 인덱스를 쿼리하는 방법을 설명합니다.
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()); } } }
아래의 클래스 정의는 Issues 테이블을 나타내며 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; } }