From 7a92f662a06ea218a44d8dc6dd0b0427ba374b90 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 27 Feb 2025 15:13:02 -0500 Subject: [PATCH 1/5] updated DynamoDB to use billingmode --- .../com/example/dynamodb/CreateTable.java | 52 +++---- .../example/dynamodb/scenario/Scenario.java | 131 ++++++++---------- .../dynamodb/scenario/ScenarioPartiQ.java | 22 +-- .../scenario/ScenarioPartiQLBatch.java | 6 +- 4 files changed, 90 insertions(+), 121 deletions(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTable.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTable.java index 426002ccc28..5b73e4fbeb5 100755 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTable.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTable.java @@ -5,10 +5,12 @@ // snippet-start:[dynamodb.java2.create_table.main] // snippet-start:[dynamodb.java2.create_table.import] + import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; @@ -16,6 +18,7 @@ import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.KeyType; +import software.amazon.awssdk.services.dynamodb.model.OnDemandThroughput; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter; @@ -24,22 +27,22 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateTable { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - tableName - The Amazon DynamoDB table to create (for example, Music3). - key - The key for the Amazon DynamoDB table (for example, Artist). - """; + Where: + tableName - The Amazon DynamoDB table to create (for example, Music3). + key - The key for the Amazon DynamoDB table (for example, Artist). + """; if (args.length != 2) { System.out.println(usage); @@ -51,8 +54,8 @@ public static void main(String[] args) { System.out.println("Creating an Amazon DynamoDB table " + tableName + " with a simple primary key: " + key); Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build(); + .region(region) + .build(); String result = createTable(ddb, tableName, key); System.out.println("New table is " + result); @@ -62,27 +65,24 @@ public static void main(String[] args) { public static String createTable(DynamoDbClient ddb, String tableName, String key) { DynamoDbWaiter dbWaiter = ddb.waiter(); CreateTableRequest request = CreateTableRequest.builder() - .attributeDefinitions(AttributeDefinition.builder() - .attributeName(key) - .attributeType(ScalarAttributeType.S) - .build()) - .keySchema(KeySchemaElement.builder() - .attributeName(key) - .keyType(KeyType.HASH) - .build()) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(10L) - .writeCapacityUnits(10L) - .build()) - .tableName(tableName) - .build(); + .attributeDefinitions(AttributeDefinition.builder() + .attributeName(key) + .attributeType(ScalarAttributeType.S) + .build()) + .keySchema(KeySchemaElement.builder() + .attributeName(key) + .keyType(KeyType.HASH) + .build()) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. + .tableName(tableName) + .build(); String newTable; try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() - .tableName(tableName) - .build(); + .tableName(tableName) + .build(); // Wait until the Amazon DynamoDB table is created. WaiterResponse waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/Scenario.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/Scenario.java index 943b3b3da06..75a103e3b84 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/Scenario.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/Scenario.java @@ -4,6 +4,7 @@ package com.example.dynamodb.scenario; // snippet-start:[dynamodb.java2.scenario.import] + import com.example.dynamodb.Movies; import com.fasterxml.jackson.databind.JsonNode; import software.amazon.awssdk.core.waiters.WaiterResponse; @@ -14,6 +15,7 @@ import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; @@ -35,6 +37,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -45,16 +48,17 @@ // snippet-end:[dynamodb.java2.scenario.import] // snippet-start:[dynamodb.java2.scenario.main] + /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * + *

* This Java example performs these tasks: - * + *

* 1. Creates the Amazon DynamoDB Movie table with partition and sort key. * 2. Puts data into the Amazon DynamoDB table from a JSON document using the * Enhanced client. @@ -70,26 +74,12 @@ public class Scenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws IOException { - final String usage = """ - - Usage: - - - Where: - fileName - The path to the moviedata.json file that you can download from the Amazon DynamoDB Developer Guide. - """; - - if (args.length != 1) { - System.out.println(usage); - System.exit(1); - } - String tableName = "Movies"; - String fileName = args[0]; + String fileName = "../../../resources/sample_files/movies.json"; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build(); + .region(region) + .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon DynamoDB example scenario."); @@ -97,7 +87,7 @@ public static void main(String[] args) throws IOException { System.out.println(DASHES); System.out.println( - "1. Creating an Amazon DynamoDB table named Movies with a key named year and a sort key named title."); + "1. Creating an Amazon DynamoDB table named Movies with a key named year and a sort key named title."); createTable(ddb, tableName); System.out.println(DASHES); @@ -147,45 +137,42 @@ public static void createTable(DynamoDbClient ddb, String tableName) { // Define attributes. attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("year") - .attributeType("N") - .build()); + .attributeName("year") + .attributeType("N") + .build()); attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("title") - .attributeType("S") - .build()); + .attributeName("title") + .attributeType("S") + .build()); ArrayList tableKey = new ArrayList<>(); KeySchemaElement key = KeySchemaElement.builder() - .attributeName("year") - .keyType(KeyType.HASH) - .build(); + .attributeName("year") + .keyType(KeyType.HASH) + .build(); KeySchemaElement key2 = KeySchemaElement.builder() - .attributeName("title") - .keyType(KeyType.RANGE) - .build(); + .attributeName("title") + .keyType(KeyType.RANGE) + .build(); // Add KeySchemaElement objects to the list. tableKey.add(key); tableKey.add(key2); CreateTableRequest request = CreateTableRequest.builder() - .keySchema(tableKey) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(10L) - .writeCapacityUnits(10L) - .build()) - .attributeDefinitions(attributeDefinitions) - .tableName(tableName) - .build(); + .keySchema(tableKey) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. + .attributeDefinitions(attributeDefinitions) + .tableName(tableName) + .build(); try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() - .tableName(tableName) - .build(); + .tableName(tableName) + .build(); // Wait until the Amazon DynamoDB table is created. WaiterResponse waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); @@ -205,14 +192,14 @@ public static void createTable(DynamoDbClient ddb, String tableName) { public static void queryTable(DynamoDbClient ddb) { try { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() - .dynamoDbClient(ddb) - .build(); + .dynamoDbClient(ddb) + .build(); DynamoDbTable custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class)); QueryConditional queryConditional = QueryConditional - .keyEqualTo(Key.builder() - .partitionValue(2013) - .build()); + .keyEqualTo(Key.builder() + .partitionValue(2013) + .build()); // Get items in the table and write out the ID value. Iterator results = custTable.query(queryConditional).items().iterator(); @@ -237,8 +224,8 @@ public static void scanMovies(DynamoDbClient ddb, String tableName) { System.out.println("******* Scanning all movies.\n"); try { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() - .dynamoDbClient(ddb) - .build(); + .dynamoDbClient(ddb) + .build(); DynamoDbTable custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class)); Iterator results = custTable.scan().items().iterator(); @@ -259,8 +246,8 @@ public static void scanMovies(DynamoDbClient ddb, String tableName) { // Load data into the table. public static void loadData(DynamoDbClient ddb, String tableName, String fileName) throws IOException { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() - .dynamoDbClient(ddb) - .build(); + .dynamoDbClient(ddb) + .build(); DynamoDbTable mappedTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class)); JsonParser parser = new JsonFactory().createParser(new File(fileName)); @@ -298,16 +285,16 @@ public static void updateTableItem(DynamoDbClient ddb, String tableName) { HashMap updatedValues = new HashMap<>(); updatedValues.put("info", AttributeValueUpdate.builder() - .value(AttributeValue.builder().s("{\"directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack\"]") - .build()) - .action(AttributeAction.PUT) - .build()); + .value(AttributeValue.builder().s("{\"directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack\"]") + .build()) + .action(AttributeAction.PUT) + .build()); UpdateItemRequest request = UpdateItemRequest.builder() - .tableName(tableName) - .key(itemKey) - .attributeUpdates(updatedValues) - .build(); + .tableName(tableName) + .key(itemKey) + .attributeUpdates(updatedValues) + .build(); try { ddb.updateItem(request); @@ -324,8 +311,8 @@ public static void updateTableItem(DynamoDbClient ddb, String tableName) { public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() - .tableName(tableName) - .build(); + .tableName(tableName) + .build(); try { ddb.deleteTable(request); @@ -340,8 +327,8 @@ public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { public static void putRecord(DynamoDbClient ddb) { try { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() - .dynamoDbClient(ddb) - .build(); + .dynamoDbClient(ddb) + .build(); DynamoDbTable table = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class)); @@ -364,17 +351,17 @@ public static void getItem(DynamoDbClient ddb) { HashMap keyToGet = new HashMap<>(); keyToGet.put("year", AttributeValue.builder() - .n("1933") - .build()); + .n("1933") + .build()); keyToGet.put("title", AttributeValue.builder() - .s("King Kong") - .build()); + .s("King Kong") + .build()); GetItemRequest request = GetItemRequest.builder() - .key(keyToGet) - .tableName("Movies") - .build(); + .key(keyToGet) + .tableName("Movies") + .build(); try { Map returnedItem = ddb.getItem(request).item(); diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java index 21e9645b1d9..34dee2b907f 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java @@ -8,6 +8,7 @@ import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; @@ -59,21 +60,7 @@ // snippet-start:[dynamodb.java2.scenario.partiql.main] public class ScenarioPartiQ { public static void main(String[] args) throws IOException { - final String usage = """ - - Usage: - - - Where: - fileName - The path to the moviedata.json file that you can download from the Amazon DynamoDB Developer Guide. - """; - - if (args.length != 1) { - System.out.println(usage); - System.exit(1); - } - - String fileName = args[0]; + String fileName = "../../../resources/sample_files/movies.json"; String tableName = "MoviesPartiQ"; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() @@ -136,10 +123,7 @@ public static void createTable(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .keySchema(tableKey) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(new Long(10)) - .writeCapacityUnits(new Long(10)) - .build()) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. .attributeDefinitions(attributeDefinitions) .tableName(tableName) .build(); diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQLBatch.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQLBatch.java index 9f16f5c337b..f4f1b367937 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQLBatch.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQLBatch.java @@ -13,6 +13,7 @@ import software.amazon.awssdk.services.dynamodb.model.BatchExecuteStatementRequest; import software.amazon.awssdk.services.dynamodb.model.BatchExecuteStatementResponse; import software.amazon.awssdk.services.dynamodb.model.BatchStatementRequest; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; @@ -156,10 +157,7 @@ public static void createTable(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .keySchema(tableKey) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(10L) - .writeCapacityUnits(10L) - .build()) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. .attributeDefinitions(attributeDefinitions) .tableName(tableName) .build(); From 3c9e65c2deee38f99767eacf85b27ad413cfffad Mon Sep 17 00:00:00 2001 From: scmacdon Date: Fri, 28 Feb 2025 20:00:41 -0500 Subject: [PATCH 2/5] removed provisionedThroughput from Java v2 examples --- .../com/example/dynamodb/CreateTableCompositeKey.java | 5 ++--- .../example/dynamodb/enhanced/EnhancedCreateTable.java | 10 +++------- .../dynamodbasync/DynamoDBAsyncCreateTable.java | 7 +++---- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTableCompositeKey.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTableCompositeKey.java index 6ae7dd9810f..0cd41716b9b 100755 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTableCompositeKey.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/CreateTableCompositeKey.java @@ -6,6 +6,7 @@ // snippet-start:[dynamodb.java2.create_table_composite_key.main] // snippet-start:[dynamodb.java2.create_table_composite_key.import] import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; @@ -74,9 +75,7 @@ public static String createTableComKey(DynamoDbClient ddb, String tableName) { .attributeName("Greeting") .keyType(KeyType.RANGE) .build()) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(10L) - .writeCapacityUnits(10L).build()) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. .tableName(tableName) .build(); diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/enhanced/EnhancedCreateTable.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/enhanced/EnhancedCreateTable.java index 40e44cb21b7..07dd499300e 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/enhanced/EnhancedCreateTable.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/enhanced/EnhancedCreateTable.java @@ -7,6 +7,7 @@ import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; +import software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest; import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter; // snippet-end:[dynamodb.java2.mapping.enhancedcreatetable.import] @@ -16,14 +17,9 @@ public class EnhancedCreateTable { public static void createTable(DynamoDbEnhancedClient enhancedClient) { // Create a DynamoDbTable object DynamoDbTable customerTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); - // Create the table - customerTable.createTable(builder -> builder - .provisionedThroughput(b -> b - .readCapacityUnits(10L) - .writeCapacityUnits(10L) - .build()) - ); + // Create the table (defaults to PAY_PER_REQUEST if no provisioned throughput is set) + customerTable.createTable(CreateTableEnhancedRequest.builder().build()); System.out.println("Waiting for table creation..."); try (DynamoDbWaiter waiter = DynamoDbWaiter.create()) { // DynamoDbWaiter is Autocloseable diff --git a/javav2/example_code/dynamodbasync/src/main/java/com/example/dynamodbasync/DynamoDBAsyncCreateTable.java b/javav2/example_code/dynamodbasync/src/main/java/com/example/dynamodbasync/DynamoDBAsyncCreateTable.java index c0d8c997d91..a2f52811dd3 100644 --- a/javav2/example_code/dynamodbasync/src/main/java/com/example/dynamodbasync/DynamoDBAsyncCreateTable.java +++ b/javav2/example_code/dynamodbasync/src/main/java/com/example/dynamodbasync/DynamoDBAsyncCreateTable.java @@ -18,6 +18,8 @@ import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; import java.util.concurrent.CompletableFuture; + +import software.amazon.awssdk.services.dynamodb.model.BillingMode; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; @@ -77,10 +79,7 @@ public static void createTable(DynamoDbAsyncClient client, String tableName, Str .attributeName(key) .keyType(KeyType.HASH) .build()) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(new Long(10)) - .writeCapacityUnits(new Long(10)) - .build()) + .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. .tableName(tableName) .build(); From ebd4ab3b9c6ae5520f48471331c4f8dcc42458b8 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 3 Mar 2025 11:04:01 -0500 Subject: [PATCH 3/5] fixed formatting --- .../dynamodb/scenario/ScenarioPartiQ.java | 110 +++++++++--------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java index 34dee2b907f..931f1f3dbf6 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java @@ -4,6 +4,7 @@ package com.example.dynamodb.scenario; // snippet-start:[dynamodb.java2.scenario.partiql.import] + import com.fasterxml.jackson.databind.JsonNode; import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; @@ -27,6 +28,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -37,13 +39,13 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * + *

* This Java example performs the following tasks: - * + *

* 1. Creates the Amazon DynamoDB movie table with a partition and sort key. * 2. Puts data into the Amazon DynamoDB table from a JSON document. * 3. Adds a new item. @@ -52,7 +54,7 @@ * 6. Uses a Scan to query items using the Enhanced client. * 7. Queries all items where the year is 2013 using the Enhanced Client. * 8. Deletes the table. - * + *

* To see another code example with more options using PartiQL and Batch * commands, see the ScenarioPartiQBatch code example. */ @@ -64,11 +66,11 @@ public static void main(String[] args) throws IOException { String tableName = "MoviesPartiQ"; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build(); + .region(region) + .build(); System.out.println( - "******* Creating an Amazon DynamoDB table named MoviesPartiQ with a key named year and a sort key named title."); + "******* Creating an Amazon DynamoDB table named MoviesPartiQ with a key named year and a sort key named title."); createTable(ddb, tableName); System.out.println("Loading data into the MoviesPartiQ table."); @@ -97,42 +99,42 @@ public static void createTable(DynamoDbClient ddb, String tableName) { // Define attributes. attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("year") - .attributeType("N") - .build()); + .attributeName("year") + .attributeType("N") + .build()); attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("title") - .attributeType("S") - .build()); + .attributeName("title") + .attributeType("S") + .build()); ArrayList tableKey = new ArrayList<>(); KeySchemaElement key = KeySchemaElement.builder() - .attributeName("year") - .keyType(KeyType.HASH) - .build(); + .attributeName("year") + .keyType(KeyType.HASH) + .build(); KeySchemaElement key2 = KeySchemaElement.builder() - .attributeName("title") - .keyType(KeyType.RANGE) // Sort - .build(); + .attributeName("title") + .keyType(KeyType.RANGE) // Sort + .build(); // Add KeySchemaElement objects to the list. tableKey.add(key); tableKey.add(key2); CreateTableRequest request = CreateTableRequest.builder() - .keySchema(tableKey) + .keySchema(tableKey) .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. - .attributeDefinitions(attributeDefinitions) - .tableName(tableName) - .build(); + .attributeDefinitions(attributeDefinitions) + .tableName(tableName) + .build(); try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() - .tableName(tableName) - .build(); + .tableName(tableName) + .build(); // Wait until the Amazon DynamoDB table is created. WaiterResponse waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); @@ -168,16 +170,16 @@ public static void loadData(DynamoDbClient ddb, String fileName) throws IOExcept String info = currentNode.path("info").toString(); AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf(year)) - .build(); + .n(String.valueOf(year)) + .build(); AttributeValue att2 = AttributeValue.builder() - .s(title) - .build(); + .s(title) + .build(); AttributeValue att3 = AttributeValue.builder() - .s(info) - .build(); + .s(info) + .build(); parameters.add(att1); parameters.add(att2); @@ -199,12 +201,12 @@ public static void getItem(DynamoDbClient ddb) { String sqlStatement = "SELECT * FROM MoviesPartiQ where year=? and title=?"; List parameters = new ArrayList<>(); AttributeValue att1 = AttributeValue.builder() - .n("2012") - .build(); + .n("2012") + .build(); AttributeValue att2 = AttributeValue.builder() - .s("The Perks of Being a Wallflower") - .build(); + .s("The Perks of Being a Wallflower") + .build(); parameters.add(att1); parameters.add(att2); @@ -226,16 +228,16 @@ public static void putRecord(DynamoDbClient ddb) { List parameters = new ArrayList<>(); AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2020")) - .build(); + .n(String.valueOf("2020")) + .build(); AttributeValue att2 = AttributeValue.builder() - .s("My Movie") - .build(); + .s("My Movie") + .build(); AttributeValue att3 = AttributeValue.builder() - .s("No Information") - .build(); + .s("No Information") + .build(); parameters.add(att1); parameters.add(att2); @@ -255,12 +257,12 @@ public static void updateTableItem(DynamoDbClient ddb) { String sqlStatement = "UPDATE MoviesPartiQ SET info = 'directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack' where year=? and title=?"; List parameters = new ArrayList<>(); AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2013")) - .build(); + .n(String.valueOf("2013")) + .build(); AttributeValue att2 = AttributeValue.builder() - .s("The East") - .build(); + .s("The East") + .build(); parameters.add(att1); parameters.add(att2); @@ -282,8 +284,8 @@ public static void queryTable(DynamoDbClient ddb) { List parameters = new ArrayList<>(); AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2013")) - .build(); + .n(String.valueOf("2013")) + .build(); parameters.add(att1); // Get items in the table and write out the ID value. @@ -299,8 +301,8 @@ public static void queryTable(DynamoDbClient ddb) { public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() - .tableName(tableName) - .build(); + .tableName(tableName) + .build(); try { ddb.deleteTable(request); @@ -313,11 +315,11 @@ public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { } private static ExecuteStatementResponse executeStatementRequest(DynamoDbClient ddb, String statement, - List parameters) { + List parameters) { ExecuteStatementRequest request = ExecuteStatementRequest.builder() - .statement(statement) - .parameters(parameters) - .build(); + .statement(statement) + .parameters(parameters) + .build(); return ddb.executeStatement(request); } From cfee426ea063a5991b962b2fbcee1635fab26d9a Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 3 Mar 2025 11:06:39 -0500 Subject: [PATCH 4/5] fixed formatting --- .../main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java index 931f1f3dbf6..54e8d201117 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java @@ -125,7 +125,7 @@ public static void createTable(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .keySchema(tableKey) - .billingMode(BillingMode.PAY_PER_REQUEST) // DynamoDB automatically scales based on traffic. + .billingMode(BillingMode.PAY_PER_REQUEST) // Scales based on traffic. .attributeDefinitions(attributeDefinitions) .tableName(tableName) .build(); From 62db5c1d107e85f453134c6e42952fa0e93eaf3e Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 3 Mar 2025 11:10:12 -0500 Subject: [PATCH 5/5] fixed formatting --- .../main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java index 54e8d201117..d0956af079a 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/scenario/ScenarioPartiQ.java @@ -125,7 +125,7 @@ public static void createTable(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .keySchema(tableKey) - .billingMode(BillingMode.PAY_PER_REQUEST) // Scales based on traffic. + .billingMode(BillingMode.PAY_PER_REQUEST) //Scales based on traffic. .attributeDefinitions(attributeDefinitions) .tableName(tableName) .build();