示例:使用 Java 文档 API 的本地二级索引 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

示例:使用 Java 文档 API 的本地二级索引

以下 Java 代码示例显示如何在Amazon DynamoDB 中处理本地二级索引。示例创建名为 CustomerOrders 的表,其分区键为 CustomerId,排序键为 OrderId。此表上有两个本地二级索引:

  • OrderCreationDateIndex — 排序键是 OrderCreationDate,并将以下属性投影到索引:

    • ProductCategory

    • ProductName

    • OrderStatus

    • ShipmentTrackingId

  • IsOpenIndex — 排序键是 IsOpen,并将表的所有属性投影到索引。

创建 CustomerOrders 表后,程序为该表加载表示客户订单的数据。然后使用本地二级索引查询这些数据。最后,程序会删除 CustomerOrders 表。

有关测试以下示例的 step-by-step 说明,请参阅Java 代码示例

package com.amazonaws.codesamples.document; import java.util.ArrayList; import java.util.Iterator; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Index; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.ItemCollection; import com.amazonaws.services.dynamodbv2.document.PutItemOutcome; 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; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex; import com.amazonaws.services.dynamodbv2.model.Projection; import com.amazonaws.services.dynamodbv2.model.ProjectionType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity; import com.amazonaws.services.dynamodbv2.model.Select; public class DocumentAPILocalSecondaryIndexExample { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); static DynamoDB dynamoDB = new DynamoDB(client); public static String tableName = "CustomerOrders"; public static void main(String[] args) throws Exception { createTable(); loadData(); query(null); query("IsOpenIndex"); query("OrderCreationDateIndex"); deleteTable(tableName); } public static void createTable() { CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits((long) 1) .withWriteCapacityUnits((long) 1)); // Attribute definitions for table partition and sort keys ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions .add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N")); // Attribute definition for index primary key attributes attributeDefinitions .add(new AttributeDefinition().withAttributeName("OrderCreationDate") .withAttributeType("N")); attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N")); createTableRequest.setAttributeDefinitions(attributeDefinitions); // Key schema for table ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition // key tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE)); // Sort // key createTableRequest.setKeySchema(tableKeySchema); ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>(); // OrderCreationDateIndex LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex() .withIndexName("OrderCreationDateIndex"); // Key schema for OrderCreationDateIndex ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>(); indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition // key indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate") .withKeyType(KeyType.RANGE)); // Sort // key orderCreationDateIndex.setKeySchema(indexKeySchema); // Projection (with list of projected attributes) for // OrderCreationDateIndex Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE); ArrayList<String> nonKeyAttributes = new ArrayList<String>(); nonKeyAttributes.add("ProductCategory"); nonKeyAttributes.add("ProductName"); projection.setNonKeyAttributes(nonKeyAttributes); orderCreationDateIndex.setProjection(projection); localSecondaryIndexes.add(orderCreationDateIndex); // IsOpenIndex LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex"); // Key schema for IsOpenIndex indexKeySchema = new ArrayList<KeySchemaElement>(); indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition // key indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE)); // Sort // key // Projection (all attributes) for IsOpenIndex projection = new Projection().withProjectionType(ProjectionType.ALL); isOpenIndex.setKeySchema(indexKeySchema); isOpenIndex.setProjection(projection); localSecondaryIndexes.add(isOpenIndex); // Add index definitions to CreateTable request createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes); System.out.println("Creating table " + tableName + "..."); System.out.println(dynamoDB.createTable(createTableRequest)); // Wait for table to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); try { Table table = dynamoDB.getTable(tableName); table.waitForActive(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void query(String indexName) { Table table = dynamoDB.getTable(tableName); System.out.println("\n***********************************************************\n"); System.out.println("Querying table " + tableName + "..."); QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); if (indexName == "IsOpenIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open."); System.out.println("Only a user-specified list of attributes are returned\n"); Index index = table.getIndex(indexName); querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen") .withValueMap(new ValueMap().withString(":v_custid", "bob@example.com") .withNumber(":v_isopen", 1)); querySpec.withProjectionExpression( "OrderCreationDate, ProductCategory, ProductName, OrderStatus"); ItemCollection<QueryOutcome> items = index.query(querySpec); Iterator<Item> iterator = items.iterator(); System.out.println("Query: printing results..."); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } } else if (indexName == "OrderCreationDateIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2015."); System.out.println("Only the projected attributes are returned\n"); Index index = table.getIndex(indexName); querySpec.withKeyConditionExpression( "CustomerId = :v_custid and OrderCreationDate >= :v_orddate") .withValueMap( new ValueMap().withString(":v_custid", "bob@example.com") .withNumber(":v_orddate", 20150131)); querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES); ItemCollection<QueryOutcome> items = index.query(querySpec); Iterator<Item> iterator = items.iterator(); System.out.println("Query: printing results..."); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } } else { System.out.println("\nNo index: All of Bob's orders, by OrderId:\n"); querySpec.withKeyConditionExpression("CustomerId = :v_custid") .withValueMap(new ValueMap().withString(":v_custid", "bob@example.com")); ItemCollection<QueryOutcome> items = table.query(querySpec); Iterator<Item> iterator = items.iterator(); System.out.println("Query: printing results..."); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } } } public static void deleteTable(String tableName) { Table table = dynamoDB.getTable(tableName); System.out.println("Deleting table " + tableName + "..."); table.delete(); // Wait for table to be deleted System.out.println("Waiting for " + tableName + " to be deleted..."); try { table.waitForDelete(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void loadData() { Table table = dynamoDB.getTable(tableName); System.out.println("Loading data into table " + tableName + "..."); Item item = new Item().withPrimaryKey("CustomerId", "alice@example.com").withNumber("OrderId", 1) .withNumber("IsOpen", 1).withNumber("OrderCreationDate", 20150101) .withString("ProductCategory", "Book") .withString("ProductName", "The Great Outdoors") .withString("OrderStatus", "PACKING ITEMS"); // no ShipmentTrackingId attribute PutItemOutcome putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "alice@example.com").withNumber("OrderId", 2) .withNumber("IsOpen", 1).withNumber("OrderCreationDate", 20150221) .withString("ProductCategory", "Bike") .withString("ProductName", "Super Mountain") .withString("OrderStatus", "ORDER RECEIVED"); // no ShipmentTrackingId attribute putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "alice@example.com").withNumber("OrderId", 3) // no IsOpen attribute .withNumber("OrderCreationDate", 20150304).withString("ProductCategory", "Music") .withString("ProductName", "A Quiet Interlude").withString("OrderStatus", "IN TRANSIT") .withString("ShipmentTrackingId", "176493"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 1) // no IsOpen attribute .withNumber("OrderCreationDate", 20150111).withString("ProductCategory", "Movie") .withString("ProductName", "Calm Before The Storm") .withString("OrderStatus", "SHIPPING DELAY") .withString("ShipmentTrackingId", "859323"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 2) // no IsOpen attribute .withNumber("OrderCreationDate", 20150124).withString("ProductCategory", "Music") .withString("ProductName", "E-Z Listening").withString("OrderStatus", "DELIVERED") .withString("ShipmentTrackingId", "756943"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 3) // no IsOpen attribute .withNumber("OrderCreationDate", 20150221).withString("ProductCategory", "Music") .withString("ProductName", "Symphony 9").withString("OrderStatus", "DELIVERED") .withString("ShipmentTrackingId", "645193"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 4) .withNumber("IsOpen", 1).withNumber("OrderCreationDate", 20150222) .withString("ProductCategory", "Hardware") .withString("ProductName", "Extra Heavy Hammer") .withString("OrderStatus", "PACKING ITEMS"); // no ShipmentTrackingId attribute putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 5) /* no IsOpen attribute */ .withNumber("OrderCreationDate", 20150309).withString("ProductCategory", "Book") .withString("ProductName", "How To Cook").withString("OrderStatus", "IN TRANSIT") .withString("ShipmentTrackingId", "440185"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 6) // no IsOpen attribute .withNumber("OrderCreationDate", 20150318).withString("ProductCategory", "Luggage") .withString("ProductName", "Really Big Suitcase").withString("OrderStatus", "DELIVERED") .withString("ShipmentTrackingId", "893927"); putItemOutcome = table.putItem(item); item = new Item().withPrimaryKey("CustomerId", "bob@example.com").withNumber("OrderId", 7) /* no IsOpen attribute */ .withNumber("OrderCreationDate", 20150324).withString("ProductCategory", "Golf") .withString("ProductName", "PGA Pro II").withString("OrderStatus", "OUT FOR DELIVERY") .withString("ShipmentTrackingId", "383283"); putItemOutcome = table.putItem(item); assert putItemOutcome != null; } }