-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Update LSI code to Javav2 #7680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeeroyHannigan
wants to merge
2
commits into
awsdocs:main
Choose a base branch
from
LeeroyHannigan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
...de/dynamodb/src/main/java/com/example/dynamodb/DocumentAPILocalSecondaryIndexExample.java
LeeroyHannigan marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // snippet-start:[dynamodb.java.codeexample.DocumentAPILocalSecondaryIndexExample] | ||
|
|
||
| package com.example.dynamodb; | ||
|
|
||
| import software.amazon.awssdk.core.waiters.WaiterResponse; | ||
| import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
| import software.amazon.awssdk.services.dynamodb.model.*; | ||
| import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class DocumentAPILocalSecondaryIndexExample { | ||
|
|
||
| static DynamoDbClient client = DynamoDbClient.create(); | ||
| public static String tableName = "CustomerOrders"; | ||
|
|
||
| public static void main(String[] args) { | ||
| createTable(); | ||
| loadData(); | ||
| query(null); | ||
| query("IsOpenIndex"); | ||
| query("OrderCreationDateIndex"); | ||
| deleteTable(tableName); | ||
| } | ||
|
|
||
| public static void createTable() { | ||
| CreateTableRequest request = CreateTableRequest.builder() | ||
| .tableName(tableName) | ||
| .provisionedThroughput(ProvisionedThroughput.builder() | ||
| .readCapacityUnits(1L) | ||
| .writeCapacityUnits(1L) | ||
| .build()) | ||
| .attributeDefinitions( | ||
| AttributeDefinition.builder().attributeName("CustomerId").attributeType(ScalarAttributeType.S).build(), | ||
| AttributeDefinition.builder().attributeName("OrderId").attributeType(ScalarAttributeType.N).build(), | ||
| AttributeDefinition.builder().attributeName("OrderCreationDate").attributeType(ScalarAttributeType.N).build(), | ||
| AttributeDefinition.builder().attributeName("IsOpen").attributeType(ScalarAttributeType.N).build()) | ||
| .keySchema( | ||
| KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(), | ||
| KeySchemaElement.builder().attributeName("OrderId").keyType(KeyType.RANGE).build()) | ||
| .localSecondaryIndexes( | ||
| LocalSecondaryIndex.builder() | ||
| .indexName("OrderCreationDateIndex") | ||
| .keySchema( | ||
| KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(), | ||
| KeySchemaElement.builder().attributeName("OrderCreationDate").keyType(KeyType.RANGE).build()) | ||
| .projection(Projection.builder() | ||
| .projectionType(ProjectionType.INCLUDE) | ||
| .nonKeyAttributes("ProductCategory", "ProductName") | ||
| .build()) | ||
| .build(), | ||
| LocalSecondaryIndex.builder() | ||
| .indexName("IsOpenIndex") | ||
| .keySchema( | ||
| KeySchemaElement.builder().attributeName("CustomerId").keyType(KeyType.HASH).build(), | ||
| KeySchemaElement.builder().attributeName("IsOpen").keyType(KeyType.RANGE).build()) | ||
| .projection(Projection.builder() | ||
| .projectionType(ProjectionType.ALL) | ||
| .build()) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| System.out.println("Creating table " + tableName + "..."); | ||
| client.createTable(request); | ||
|
|
||
| try (DynamoDbWaiter waiter = client.waiter()) { | ||
| WaiterResponse<DescribeTableResponse> response = waiter.waitUntilTableExists(r -> r.tableName(tableName)); | ||
| response.matched().response().ifPresent(System.out::println); | ||
| } | ||
| } | ||
|
|
||
| public static void query(String indexName) { | ||
| System.out.println("\n***********************************************************\n"); | ||
| System.out.println("Querying table " + tableName + "..."); | ||
|
|
||
| if ("IsOpenIndex".equals(indexName)) { | ||
| 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"); | ||
|
|
||
| Map<String, AttributeValue> values = new HashMap<>(); | ||
| values.put(":v_custid", AttributeValue.builder().s("[email protected]").build()); | ||
| values.put(":v_isopen", AttributeValue.builder().n("1").build()); | ||
|
|
||
| QueryRequest request = QueryRequest.builder() | ||
| .tableName(tableName) | ||
| .indexName(indexName) | ||
| .keyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen") | ||
| .expressionAttributeValues(values) | ||
| .projectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus") | ||
| .build(); | ||
|
|
||
| System.out.println("Query: printing results..."); | ||
| client.query(request).items().forEach(System.out::println); | ||
|
|
||
| } else if ("OrderCreationDateIndex".equals(indexName)) { | ||
| 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"); | ||
|
|
||
| Map<String, AttributeValue> values = new HashMap<>(); | ||
| values.put(":v_custid", AttributeValue.builder().s("[email protected]").build()); | ||
| values.put(":v_orddate", AttributeValue.builder().n("20150131").build()); | ||
|
|
||
| QueryRequest request = QueryRequest.builder() | ||
| .tableName(tableName) | ||
| .indexName(indexName) | ||
| .keyConditionExpression("CustomerId = :v_custid and OrderCreationDate >= :v_orddate") | ||
| .expressionAttributeValues(values) | ||
| .select(Select.ALL_PROJECTED_ATTRIBUTES) | ||
| .build(); | ||
|
|
||
| System.out.println("Query: printing results..."); | ||
| client.query(request).items().forEach(System.out::println); | ||
|
|
||
| } else { | ||
| System.out.println("\nNo index: All of Bob's orders, by OrderId:\n"); | ||
|
|
||
| Map<String, AttributeValue> values = new HashMap<>(); | ||
| values.put(":v_custid", AttributeValue.builder().s("[email protected]").build()); | ||
|
|
||
| QueryRequest request = QueryRequest.builder() | ||
| .tableName(tableName) | ||
| .keyConditionExpression("CustomerId = :v_custid") | ||
| .expressionAttributeValues(values) | ||
| .build(); | ||
|
|
||
| System.out.println("Query: printing results..."); | ||
| client.query(request).items().forEach(System.out::println); | ||
| } | ||
| } | ||
|
|
||
| public static void deleteTable(String tableName) { | ||
| System.out.println("Deleting table " + tableName + "..."); | ||
| client.deleteTable(DeleteTableRequest.builder().tableName(tableName).build()); | ||
|
|
||
| try (DynamoDbWaiter waiter = client.waiter()) { | ||
| waiter.waitUntilTableNotExists(r -> r.tableName(tableName)); | ||
| } | ||
| } | ||
|
|
||
| public static void loadData() { | ||
| System.out.println("Loading data into table " + tableName + "..."); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("1").build(), | ||
| "IsOpen", AttributeValue.builder().n("1").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150101").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Book").build(), | ||
| "ProductName", AttributeValue.builder().s("The Great Outdoors").build(), | ||
| "OrderStatus", AttributeValue.builder().s("PACKING ITEMS").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("2").build(), | ||
| "IsOpen", AttributeValue.builder().n("1").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150221").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Bike").build(), | ||
| "ProductName", AttributeValue.builder().s("Super Mountain").build(), | ||
| "OrderStatus", AttributeValue.builder().s("ORDER RECEIVED").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("3").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150304").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Music").build(), | ||
| "ProductName", AttributeValue.builder().s("A Quiet Interlude").build(), | ||
| "OrderStatus", AttributeValue.builder().s("IN TRANSIT").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("176493").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("1").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150111").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Movie").build(), | ||
| "ProductName", AttributeValue.builder().s("Calm Before The Storm").build(), | ||
| "OrderStatus", AttributeValue.builder().s("SHIPPING DELAY").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("859323").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("2").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150124").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Music").build(), | ||
| "ProductName", AttributeValue.builder().s("E-Z Listening").build(), | ||
| "OrderStatus", AttributeValue.builder().s("DELIVERED").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("756943").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("3").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150221").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Music").build(), | ||
| "ProductName", AttributeValue.builder().s("Symphony 9").build(), | ||
| "OrderStatus", AttributeValue.builder().s("DELIVERED").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("645193").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("4").build(), | ||
| "IsOpen", AttributeValue.builder().n("1").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150222").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Hardware").build(), | ||
| "ProductName", AttributeValue.builder().s("Extra Heavy Hammer").build(), | ||
| "OrderStatus", AttributeValue.builder().s("PACKING ITEMS").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("5").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150309").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Book").build(), | ||
| "ProductName", AttributeValue.builder().s("How To Cook").build(), | ||
| "OrderStatus", AttributeValue.builder().s("IN TRANSIT").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("440185").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("6").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150318").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Luggage").build(), | ||
| "ProductName", AttributeValue.builder().s("Really Big Suitcase").build(), | ||
| "OrderStatus", AttributeValue.builder().s("DELIVERED").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("893927").build())); | ||
|
|
||
| putItem(Map.of( | ||
| "CustomerId", AttributeValue.builder().s("[email protected]").build(), | ||
| "OrderId", AttributeValue.builder().n("7").build(), | ||
| "OrderCreationDate", AttributeValue.builder().n("20150324").build(), | ||
| "ProductCategory", AttributeValue.builder().s("Golf").build(), | ||
| "ProductName", AttributeValue.builder().s("PGA Pro II").build(), | ||
| "OrderStatus", AttributeValue.builder().s("OUT FOR DELIVERY").build(), | ||
| "ShipmentTrackingId", AttributeValue.builder().s("383283").build())); | ||
| } | ||
|
|
||
| private static void putItem(Map<String, AttributeValue> item) { | ||
| client.putItem(PutItemRequest.builder().tableName(tableName).item(item).build()); | ||
| } | ||
| } | ||
|
|
||
| // snippet-end:[dynamodb.java.codeexample.DocumentAPILocalSecondaryIndexExample] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.