1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ // snippet-start:[dynamodb.java.codeexample.DocumentAPILocalSecondaryIndexExample]
5+
6+ package com .example .dynamodb ;
7+
8+ import software .amazon .awssdk .core .waiters .WaiterResponse ;
9+ import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
10+ import software .amazon .awssdk .services .dynamodb .model .*;
11+ import software .amazon .awssdk .services .dynamodb .waiters .DynamoDbWaiter ;
12+
13+ import java .util .HashMap ;
14+ import java .util .Map ;
15+
16+ public class DocumentAPILocalSecondaryIndexExample {
17+
18+ static DynamoDbClient client = DynamoDbClient .create ();
19+ public static String tableName = "CustomerOrders" ;
20+
21+ public static void main (String [] args ) {
22+ createTable ();
23+ loadData ();
24+ query (null );
25+ query ("IsOpenIndex" );
26+ query ("OrderCreationDateIndex" );
27+ deleteTable (tableName );
28+ }
29+
30+ public static void createTable () {
31+ CreateTableRequest request = CreateTableRequest .builder ()
32+ .tableName (tableName )
33+ .provisionedThroughput (ProvisionedThroughput .builder ()
34+ .readCapacityUnits (1L )
35+ .writeCapacityUnits (1L )
36+ .build ())
37+ .attributeDefinitions (
38+ AttributeDefinition .builder ().attributeName ("CustomerId" ).attributeType (ScalarAttributeType .S ).build (),
39+ AttributeDefinition .builder ().attributeName ("OrderId" ).attributeType (ScalarAttributeType .N ).build (),
40+ AttributeDefinition .builder ().attributeName ("OrderCreationDate" ).attributeType (ScalarAttributeType .N ).build (),
41+ AttributeDefinition .builder ().attributeName ("IsOpen" ).attributeType (ScalarAttributeType .N ).build ())
42+ .keySchema (
43+ KeySchemaElement .builder ().attributeName ("CustomerId" ).keyType (KeyType .HASH ).build (),
44+ KeySchemaElement .builder ().attributeName ("OrderId" ).keyType (KeyType .RANGE ).build ())
45+ .localSecondaryIndexes (
46+ LocalSecondaryIndex .builder ()
47+ .indexName ("OrderCreationDateIndex" )
48+ .keySchema (
49+ KeySchemaElement .builder ().attributeName ("CustomerId" ).keyType (KeyType .HASH ).build (),
50+ KeySchemaElement .builder ().attributeName ("OrderCreationDate" ).keyType (KeyType .RANGE ).build ())
51+ .projection (Projection .builder ()
52+ .projectionType (ProjectionType .INCLUDE )
53+ .nonKeyAttributes ("ProductCategory" , "ProductName" )
54+ .build ())
55+ .build (),
56+ LocalSecondaryIndex .builder ()
57+ .indexName ("IsOpenIndex" )
58+ .keySchema (
59+ KeySchemaElement .builder ().attributeName ("CustomerId" ).keyType (KeyType .HASH ).build (),
60+ KeySchemaElement .builder ().attributeName ("IsOpen" ).keyType (KeyType .RANGE ).build ())
61+ .projection (Projection .builder ()
62+ .projectionType (ProjectionType .ALL )
63+ .build ())
64+ .build ())
65+ .build ();
66+
67+ System .out .println ("Creating table " + tableName + "..." );
68+ client .createTable (request );
69+
70+ try (DynamoDbWaiter waiter = client .waiter ()) {
71+ WaiterResponse <DescribeTableResponse > response = waiter .waitUntilTableExists (r -> r .tableName (tableName ));
72+ response .matched ().response ().ifPresent (System .out ::println );
73+ }
74+ }
75+
76+ public static void query (String indexName ) {
77+ System .out .println ("\n ***********************************************************\n " );
78+ System .out .println ("Querying table " + tableName + "..." );
79+
80+ if ("IsOpenIndex" .equals (indexName )) {
81+ System .out .println ("\n Using index: '" + indexName + "': Bob's orders that are open." );
82+ System .out .println ("Only a user-specified list of attributes are returned\n " );
83+
84+ Map <String , AttributeValue > values = new HashMap <>();
85+ values .put (":v_custid" , AttributeValue .builder ().s ("bob@example.com" ).build ());
86+ values .put (":v_isopen" , AttributeValue .builder ().n ("1" ).build ());
87+
88+ QueryRequest request = QueryRequest .builder ()
89+ .tableName (tableName )
90+ .indexName (indexName )
91+ .keyConditionExpression ("CustomerId = :v_custid and IsOpen = :v_isopen" )
92+ .expressionAttributeValues (values )
93+ .projectionExpression ("OrderCreationDate, ProductCategory, ProductName, OrderStatus" )
94+ .build ();
95+
96+ System .out .println ("Query: printing results..." );
97+ client .query (request ).items ().forEach (System .out ::println );
98+
99+ } else if ("OrderCreationDateIndex" .equals (indexName )) {
100+ System .out .println ("\n Using index: '" + indexName + "': Bob's orders that were placed after 01/31/2015." );
101+ System .out .println ("Only the projected attributes are returned\n " );
102+
103+ Map <String , AttributeValue > values = new HashMap <>();
104+ values .put (":v_custid" , AttributeValue .builder ().s ("bob@example.com" ).build ());
105+ values .put (":v_orddate" , AttributeValue .builder ().n ("20150131" ).build ());
106+
107+ QueryRequest request = QueryRequest .builder ()
108+ .tableName (tableName )
109+ .indexName (indexName )
110+ .keyConditionExpression ("CustomerId = :v_custid and OrderCreationDate >= :v_orddate" )
111+ .expressionAttributeValues (values )
112+ .select (Select .ALL_PROJECTED_ATTRIBUTES )
113+ .build ();
114+
115+ System .out .println ("Query: printing results..." );
116+ client .query (request ).items ().forEach (System .out ::println );
117+
118+ } else {
119+ System .out .println ("\n No index: All of Bob's orders, by OrderId:\n " );
120+
121+ Map <String , AttributeValue > values = new HashMap <>();
122+ values .put (":v_custid" , AttributeValue .builder ().s ("bob@example.com" ).build ());
123+
124+ QueryRequest request = QueryRequest .builder ()
125+ .tableName (tableName )
126+ .keyConditionExpression ("CustomerId = :v_custid" )
127+ .expressionAttributeValues (values )
128+ .build ();
129+
130+ System .out .println ("Query: printing results..." );
131+ client .query (request ).items ().forEach (System .out ::println );
132+ }
133+ }
134+
135+ public static void deleteTable (String tableName ) {
136+ System .out .println ("Deleting table " + tableName + "..." );
137+ client .deleteTable (DeleteTableRequest .builder ().tableName (tableName ).build ());
138+
139+ try (DynamoDbWaiter waiter = client .waiter ()) {
140+ waiter .waitUntilTableNotExists (r -> r .tableName (tableName ));
141+ }
142+ }
143+
144+ public static void loadData () {
145+ System .out .println ("Loading data into table " + tableName + "..." );
146+
147+ putItem (Map .of (
148+ "CustomerId" , AttributeValue .builder ().s ("alice@example.com" ).build (),
149+ "OrderId" , AttributeValue .builder ().n ("1" ).build (),
150+ "IsOpen" , AttributeValue .builder ().n ("1" ).build (),
151+ "OrderCreationDate" , AttributeValue .builder ().n ("20150101" ).build (),
152+ "ProductCategory" , AttributeValue .builder ().s ("Book" ).build (),
153+ "ProductName" , AttributeValue .builder ().s ("The Great Outdoors" ).build (),
154+ "OrderStatus" , AttributeValue .builder ().s ("PACKING ITEMS" ).build ()));
155+
156+ putItem (Map .of (
157+ "CustomerId" , AttributeValue .builder ().s ("alice@example.com" ).build (),
158+ "OrderId" , AttributeValue .builder ().n ("2" ).build (),
159+ "IsOpen" , AttributeValue .builder ().n ("1" ).build (),
160+ "OrderCreationDate" , AttributeValue .builder ().n ("20150221" ).build (),
161+ "ProductCategory" , AttributeValue .builder ().s ("Bike" ).build (),
162+ "ProductName" , AttributeValue .builder ().s ("Super Mountain" ).build (),
163+ "OrderStatus" , AttributeValue .builder ().s ("ORDER RECEIVED" ).build ()));
164+
165+ putItem (Map .of (
166+ "CustomerId" , AttributeValue .builder ().s ("alice@example.com" ).build (),
167+ "OrderId" , AttributeValue .builder ().n ("3" ).build (),
168+ "OrderCreationDate" , AttributeValue .builder ().n ("20150304" ).build (),
169+ "ProductCategory" , AttributeValue .builder ().s ("Music" ).build (),
170+ "ProductName" , AttributeValue .builder ().s ("A Quiet Interlude" ).build (),
171+ "OrderStatus" , AttributeValue .builder ().s ("IN TRANSIT" ).build (),
172+ "ShipmentTrackingId" , AttributeValue .builder ().s ("176493" ).build ()));
173+
174+ putItem (Map .of (
175+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
176+ "OrderId" , AttributeValue .builder ().n ("1" ).build (),
177+ "OrderCreationDate" , AttributeValue .builder ().n ("20150111" ).build (),
178+ "ProductCategory" , AttributeValue .builder ().s ("Movie" ).build (),
179+ "ProductName" , AttributeValue .builder ().s ("Calm Before The Storm" ).build (),
180+ "OrderStatus" , AttributeValue .builder ().s ("SHIPPING DELAY" ).build (),
181+ "ShipmentTrackingId" , AttributeValue .builder ().s ("859323" ).build ()));
182+
183+ putItem (Map .of (
184+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
185+ "OrderId" , AttributeValue .builder ().n ("2" ).build (),
186+ "OrderCreationDate" , AttributeValue .builder ().n ("20150124" ).build (),
187+ "ProductCategory" , AttributeValue .builder ().s ("Music" ).build (),
188+ "ProductName" , AttributeValue .builder ().s ("E-Z Listening" ).build (),
189+ "OrderStatus" , AttributeValue .builder ().s ("DELIVERED" ).build (),
190+ "ShipmentTrackingId" , AttributeValue .builder ().s ("756943" ).build ()));
191+
192+ putItem (Map .of (
193+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
194+ "OrderId" , AttributeValue .builder ().n ("3" ).build (),
195+ "OrderCreationDate" , AttributeValue .builder ().n ("20150221" ).build (),
196+ "ProductCategory" , AttributeValue .builder ().s ("Music" ).build (),
197+ "ProductName" , AttributeValue .builder ().s ("Symphony 9" ).build (),
198+ "OrderStatus" , AttributeValue .builder ().s ("DELIVERED" ).build (),
199+ "ShipmentTrackingId" , AttributeValue .builder ().s ("645193" ).build ()));
200+
201+ putItem (Map .of (
202+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
203+ "OrderId" , AttributeValue .builder ().n ("4" ).build (),
204+ "IsOpen" , AttributeValue .builder ().n ("1" ).build (),
205+ "OrderCreationDate" , AttributeValue .builder ().n ("20150222" ).build (),
206+ "ProductCategory" , AttributeValue .builder ().s ("Hardware" ).build (),
207+ "ProductName" , AttributeValue .builder ().s ("Extra Heavy Hammer" ).build (),
208+ "OrderStatus" , AttributeValue .builder ().s ("PACKING ITEMS" ).build ()));
209+
210+ putItem (Map .of (
211+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
212+ "OrderId" , AttributeValue .builder ().n ("5" ).build (),
213+ "OrderCreationDate" , AttributeValue .builder ().n ("20150309" ).build (),
214+ "ProductCategory" , AttributeValue .builder ().s ("Book" ).build (),
215+ "ProductName" , AttributeValue .builder ().s ("How To Cook" ).build (),
216+ "OrderStatus" , AttributeValue .builder ().s ("IN TRANSIT" ).build (),
217+ "ShipmentTrackingId" , AttributeValue .builder ().s ("440185" ).build ()));
218+
219+ putItem (Map .of (
220+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
221+ "OrderId" , AttributeValue .builder ().n ("6" ).build (),
222+ "OrderCreationDate" , AttributeValue .builder ().n ("20150318" ).build (),
223+ "ProductCategory" , AttributeValue .builder ().s ("Luggage" ).build (),
224+ "ProductName" , AttributeValue .builder ().s ("Really Big Suitcase" ).build (),
225+ "OrderStatus" , AttributeValue .builder ().s ("DELIVERED" ).build (),
226+ "ShipmentTrackingId" , AttributeValue .builder ().s ("893927" ).build ()));
227+
228+ putItem (Map .of (
229+ "CustomerId" , AttributeValue .builder ().s ("bob@example.com" ).build (),
230+ "OrderId" , AttributeValue .builder ().n ("7" ).build (),
231+ "OrderCreationDate" , AttributeValue .builder ().n ("20150324" ).build (),
232+ "ProductCategory" , AttributeValue .builder ().s ("Golf" ).build (),
233+ "ProductName" , AttributeValue .builder ().s ("PGA Pro II" ).build (),
234+ "OrderStatus" , AttributeValue .builder ().s ("OUT FOR DELIVERY" ).build (),
235+ "ShipmentTrackingId" , AttributeValue .builder ().s ("383283" ).build ()));
236+ }
237+
238+ private static void putItem (Map <String , AttributeValue > item ) {
239+ client .putItem (PutItemRequest .builder ().tableName (tableName ).item (item ).build ());
240+ }
241+ }
242+
243+ // snippet-end:[dynamodb.java.codeexample.DocumentAPILocalSecondaryIndexExample]
0 commit comments