DynamoDB – Update Items

DynamoDB – Update Items ”; Previous Next Updating an item in DynamoDB mainly consists of specifying the full primary key and table name for the item. It requires a new value for each attribute you modify. The operation uses UpdateItem, which modifies the existing items or creates them on discovery of a missing item. In updates, you might want to track the changes by displaying the original and new values, before and after the operations. UpdateItem uses the ReturnValues parameter to achieve this. Note − The operation does not report capacity unit consumption, but you can use the ReturnConsumedCapacity parameter. Use the GUI console, Java, or any other tool to perform this task. How to Update Items Using GUI Tools? Navigate to the console. In the navigation pane on the left side, select Tables. Choose the table needed, and then select the Items tab. Choose the item desired for an update, and select Actions | Edit. Modify any attributes or values necessary in the Edit Item window. Update Items Using Java Using Java in the item update operations requires creating a Table class instance, and calling its updateItem method. Then you specify the item”s primary key, and provide an UpdateExpression detailing attribute modifications. The Following is an example of the same − DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); Table table = dynamoDB.getTable(“ProductList”); Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put(“#M”, “Make”); expressionAttributeNames.put(“#P”, “Price expressionAttributeNames.put(“#N”, “ID”); Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); expressionAttributeValues.put(“:val1”, new HashSet<String>(Arrays.asList(“Make1″,”Make2”))); expressionAttributeValues.put(“:val2”, 1); //Price UpdateItemOutcome outcome = table.updateItem( “internalID”, // key attribute name 111, // key attribute value “add #M :val1 set #P = #P – :val2 remove #N”, // UpdateExpression expressionAttributeNames, expressionAttributeValues); The updateItem method also allows for specifying conditions, which can be seen in the following example − Table table = dynamoDB.getTable(“ProductList”); Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put(“#P”, “Price”); Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); expressionAttributeValues.put(“:val1”, 44); // change Price to 44 expressionAttributeValues.put(“:val2”, 15); // only if currently 15 UpdateItemOutcome outcome = table.updateItem (new PrimaryKey(“internalID”,111), “set #P = :val1”, // Update “#P = :val2”, // Condition expressionAttributeNames, expressionAttributeValues); Update Items Using Counters DynamoDB allows atomic counters, which means using UpdateItem to increment/decrement attribute values without impacting other requests; furthermore, the counters always update. The following is an example that explains how it can be done. Note − The following sample may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources). This sample also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project. package com.amazonaws.codesamples.document; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class UpdateItemOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); static String tblName = “ProductList”; public static void main(String[] args) throws IOException { createItems(); retrieveItem(); // Execute updates updateMultipleAttributes(); updateAddNewAttribute(); updateExistingAttributeConditionally(); // Item deletion deleteItem(); } private static void createItems() { Table table = dynamoDB.getTable(tblName); try { Item item = new Item() .withPrimaryKey(“ID”, 303) .withString(“Nomenclature”, “Polymer Blaster 4000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc.”, “LMNOP Inc.”))) .withNumber(“Price”, 50000) .withBoolean(“InProduction”, true) .withString(“Category”, “Laser Cutter”); table.putItem(item); item = new Item() .withPrimaryKey(“ID”, 313) .withString(“Nomenclature”, “Agitatatron 2000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc,”, “CDE Inc.”))) .withNumber(“Price”, 40000) .withBoolean(“InProduction”, true) .withString(“Category”, “Agitator”); table.putItem(item); } catch (Exception e) { System.err.println(“Cannot create items.”); System.err.println(e.getMessage()); } } private static void updateAddNewAttribute() { Table table = dynamoDB.getTable(tableName); try { Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put(“#na”, “NewAttribute”); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(“ID”, 303) .withUpdateExpression(“set #na = :val1”) .withNameMap(new NameMap() .with(“#na”, “NewAttribute”)) .withValueMap(new ValueMap() .withString(“:val1”, “A value”)) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Confirm System.out.println(“Displaying updated item…”); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println(“Cannot add an attribute in ” + tableName); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;

DynamoDB – Getting Items

DynamoDB – Getting Items ”; Previous Next Retrieving an item in DynamoDB requires using GetItem, and specifying the table name and item primary key. Be sure to include a complete primary key rather than omitting a portion. For example, omitting the sort key of a composite key. GetItem behaviour conforms to three defaults − It executes as an eventually consistent read. It provides all attributes. It does not detail its capacity unit consumption. These parameters allow you to override the default GetItem behaviour. Retrieve an Item DynamoDB ensures reliability through maintaining multiple copies of items across multiple servers. Each successful write creates these copies, but takes substantial time to execute; meaning eventually consistent. This means you cannot immediately attempt a read after writing an item. You can change the default eventually consistent read of GetItem, however, the cost of more current data remains consumption of more capacity units; specifically, two times as much. Note DynamoDB typically achieves consistency across every copy within a second. You can use the GUI console, Java, or another tool to perform this task. Item Retrieval Using Java Using Java in item retrieval operations requires creating a DynamoDB Class Instance, Table Class Instance, and calling the Table instance”s getItem method. Then specify the primary key of the item. You can review the following example − DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); Table table = dynamoDB.getTable(“ProductList”); Item item = table.getItem(“IDnum”, 109); In some cases, you need to specify the parameters for this operation. The following example uses .withProjectionExpression and GetItemSpec for retrieval specifications − GetItemSpec spec = new GetItemSpec() .withPrimaryKey(“IDnum”, 122) .withProjectionExpression(“IDnum, EmployeeName, Department”) .withConsistentRead(true); Item item = table.getItem(spec); System.out.println(item.toJSONPretty()); You can also review a the following bigger example for better understanding. Note − The following sample may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources). This sample also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project. package com.amazonaws.codesamples.document; import java.io.IOException import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class GetItemOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); static String tblName = “ProductList”; public static void main(String[] args) throws IOException { createItems(); retrieveItem(); // Execute updates updateMultipleAttributes(); updateAddNewAttribute(); updateExistingAttributeConditionally(); // Item deletion deleteItem(); } private static void createItems() { Table table = dynamoDB.getTable(tblName); try { Item item = new Item() .withPrimaryKey(“ID”, 303) .withString(“Nomenclature”, “Polymer Blaster 4000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc.”, “LMNOP Inc.”))) .withNumber(“Price”, 50000) .withBoolean(“InProduction”, true) .withString(“Category”, “Laser Cutter”); table.putItem(item); item = new Item() .withPrimaryKey(“ID”, 313) .withString(“Nomenclature”, “Agitatatron 2000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc,”, “CDE Inc.”))) .withNumber(“Price”, 40000) .withBoolean(“InProduction”, true) .withString(“Category”, “Agitator”); table.putItem(item); } catch (Exception e) { System.err.println(“Cannot create items.”); System.err.println(e.getMessage()); } } private static void retrieveItem() { Table table = dynamoDB.getTable(tableName); try { Item item = table.getItem(“ID”, 303, “ID, Nomenclature, Manufacturers”, null); System.out.println(“Displaying retrieved items…”); System.out.println(item.toJSONPretty()); } catch (Exception e) { System.err.println(“Cannot retrieve items.”); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;

DynamoDB – Batch Retrieve

DynamoDB – Batch Retrieve ”; Previous Next Batch Retrieve operations return attributes of a single or multiple items. These operations generally consist of using the primary key to identify the desired item(s). The BatchGetItem operations are subject to the limits of individual operations as well as their own unique constraints. The following requests in batch retrieval operations result in rejection − Make a request for more than 100 items. Make a request exceeding throughput. Batch retrieve operations perform partial processing of requests carrying the potential to exceed limits. For example − a request to retrieve multiple items large enough in size to exceed limits results in part of the request processing, and an error message noting the unprocessed portion. On return of unprocessed items, create a back-off algorithm solution to manage this rather than throttling tables. The BatchGet operations perform eventually with consistent reads, requiring modification for strongly consistent ones. They also perform retrievals in parallel. Note − The order of the returned items. DynamoDB does not sort the items. It also does not indicate the absence of the requested items. Furthermore, those requests consume capacity units. All the BatchGet operations require RequestItems parameters such as the read consistency, attribute names, and primary keys. Response − A successful operation results in an HTTP 200 response, which indicates characteristics like capacity units consumed, table processing metrics, and any unprocessed items. Batch Retrievals with Java Using Java in BatchGet operations requires creating a DynamoDB class instance, TableKeysAndAttributes class instance describing a primary key values list for the items, and passing the TableKeysAndAttributes object to the BatchGetItem method. The following is an example of a BatchGet operation − DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient ( new ProfileCredentialsProvider())); TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes (forumTableName); forumTableKeysAndAttributes.addHashOnlyPrimaryKeys ( “Title”, “Updates”, “Product Line 1” ); TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes ( threadTableName); threadTableKeysAndAttributes.addHashAndRangePrimaryKeys ( “ForumTitle”, “Topic”, “Product Line 1”, “P1 Thread 1”, “Product Line 1”, “P1 Thread 2”, “Product Line 2”, “P2 Thread 1” ); BatchGetItemOutcome outcome = dynamoDB.batchGetItem ( forumTableKeysAndAttributes, threadTableKeysAndAttributes); for (String tableName : outcome.getTableItems().keySet()) { System.out.println(“Table items ” + tableName); List<Item> items = outcome.getTableItems().get(tableName); for (Item item : items) { System.out.println(item); } } You can review the following larger example. Note − The following program may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources). This program also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project. package com.amazonaws.codesamples.document; import java.io.IOException; import java.util.List; import java.util.Map; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes; import com.amazonaws.services.dynamodbv2.model.KeysAndAttributes; public class BatchGetOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient ( new ProfileCredentialsProvider())); static String forumTableName = “Forum”; static String threadTableName = “Thread”; public static void main(String[] args) throws IOException { retrieveMultipleItemsBatchGet(); } private static void retrieveMultipleItemsBatchGet() { try { TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes(forumTableName); //Create partition key forumTableKeysAndAttributes.addHashOnlyPrimaryKeys ( “Name”, “XYZ Melt-O-tron”, “High-Performance Processing” ); TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName); //Create partition key and sort key threadTableKeysAndAttributes.addHashAndRangePrimaryKeys ( “ForumName”, “Subject”, “High-Performance Processing”, “HP Processing Thread One”, “High-Performance Processing”, “HP Processing Thread Two”, “Melt-O-Tron”, “MeltO Thread One” ); System.out.println(“Processing…”); BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes, threadTableKeysAndAttributes); Map<String, KeysAndAttributes> unprocessed = null; do { for (String tableName : outcome.getTableItems().keySet()) { System.out.println(“Table items for ” + tableName); List<Item> items = outcome.getTableItems().get(tableName); for (Item item : items) { System.out.println(item.toJSONPretty()); } } // Confirm no unprocessed items unprocessed = outcome.getUnprocessedKeys(); if (unprocessed.isEmpty()) { System.out.println(“All items processed.”); } else { System.out.println(“Gathering unprocessed items…”); outcome = dynamoDB.batchGetItemUnprocessed(unprocessed); } } while (!unprocessed.isEmpty()); } catch (Exception e) { System.err.println(“Could not get items.”); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;

DynamoDB – Delete Items

DynamoDB – Delete Items ”; Previous Next Deleting an item in the DynamoDB only requires providing the table name and the item key. It is also strongly recommended to use of a conditional expression which will be necessary to avoid deleting the wrong items. As usual, you can either use the GUI console, Java, or any other needed tool to perform this task. Delete Items Using the GUI Console Navigate to the console. In the navigation pane on the left side, select Tables. Then select the table name, and the Items tab. Choose the items desired for deletion, and select Actions | Delete. A Delete Item(s) dialog box then appears as shown in the following screeshot. Choose “Delete” to confirm. How to Delete Items Using Java? Using Java in item deletion operations merely involves creating a DynamoDB client instance, and calling the deleteItem method through using the item”s key. You can see the following example, where it has been explained in detail. DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); Table table = dynamoDB.getTable(“ProductList”); DeleteItemOutcome outcome = table.deleteItem(“IDnum”, 151); You can also specify the parameters to protect against incorrect deletion. Simply use a ConditionExpression. For example − Map<String,Object> expressionAttributeValues = new HashMap<String,Object>(); expressionAttributeValues.put(“:val”, false); DeleteItemOutcome outcome = table.deleteItem(“IDnum”,151, “Ship = :val”, null, // doesn”t use ExpressionAttributeNames expressionAttributeValues); The following is a larger example for better understanding. Note − The following sample may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources). This sample also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project. package com.amazonaws.codesamples.document; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class DeleteItemOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); static String tblName = “ProductList”; public static void main(String[] args) throws IOException { createItems(); retrieveItem(); // Execute updates updateMultipleAttributes(); updateAddNewAttribute(); updateExistingAttributeConditionally(); // Item deletion deleteItem(); } private static void createItems() { Table table = dynamoDB.getTable(tblName); try { Item item = new Item() .withPrimaryKey(“ID”, 303) .withString(“Nomenclature”, “Polymer Blaster 4000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc.”, “LMNOP Inc.”))) .withNumber(“Price”, 50000) .withBoolean(“InProduction”, true) .withString(“Category”, “Laser Cutter”); table.putItem(item); item = new Item() .withPrimaryKey(“ID”, 313) .withString(“Nomenclature”, “Agitatatron 2000”) .withStringSet( “Manufacturers”, new HashSet<String>(Arrays.asList(“XYZ Inc,”, “CDE Inc.”))) .withNumber(“Price”, 40000) .withBoolean(“InProduction”, true) .withString(“Category”, “Agitator”); table.putItem(item); } catch (Exception e) { System.err.println(“Cannot create items.”); System.err.println(e.getMessage()); } } private static void deleteItem() { Table table = dynamoDB.getTable(tableName); try { DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey(“ID”, 303) .withConditionExpression(“#ip = :val”) .withNameMap(new NameMap() .with(“#ip”, “InProduction”)) .withValueMap(new ValueMap() .withBoolean(“:val”, false)) .withReturnValues(ReturnValue.ALL_OLD); DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec); // Confirm System.out.println(“Displaying deleted item…”); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println(“Cannot delete item in ” + tableName); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;

DynamoDB – Batch Writing

DynamoDB – Batch Writing ”; Previous Next Batch writing operates on multiple items by creating or deleting several items. These operations utilize BatchWriteItem, which carries the limitations of no more than 16MB writes and 25 requests. Each item obeys a 400KB size limit. Batch writes also cannot perform item updates. What is Batch Writing? Batch writes can manipulate items across multiple tables. Operation invocation happens for each individual request, which means operations do not impact each other, and heterogeneous mixes are permitted; for example, one PutItem and three DeleteItem requests in a batch, with the failure of the PutItem request not impacting the others. Failed requests result in the operation returning information (keys and data) pertaining to each failed request. Note − If DynamoDB returns any items without processing them, retry them; however, use a back-off method to avoid another request failure based on overloading. DynamoDB rejects a batch write operation when one or more of the following statements proves to be true − The request exceeds the provisioned throughput. The request attempts to use BatchWriteItems to update an item. The request performs several operations on a single item. The request tables do not exist. The item attributes in the request do not match the target. The requests exceed size limits. Batch writes require certain RequestItem parameters − Deletion operations need DeleteRequest key subelements meaning an attribute name and value. The PutRequest items require an Item subelement meaning an attribute and attribute value map. Response − A successful operation results in an HTTP 200 response, which indicates characteristics like capacity units consumed, table processing metrics, and any unprocessed items. Batch Writes with Java Perform a batch write by creating a DynamoDB class instance, a TableWriteItems class instance describing all operations, and calling the batchWriteItem method to use the TableWriteItems object. Note − You must create a TableWriteItems instance for every table in a batch write to multiple tables. Also, check your request response for any unprocessed requests. You can review the following example of a batch write − DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); TableWriteItems forumTableWriteItems = new TableWriteItems(“Forum”) .withItemsToPut( new Item() .withPrimaryKey(“Title”, “XYZ CRM”) .withNumber(“Threads”, 0)); TableWriteItems threadTableWriteItems = new TableWriteItems(Thread) .withItemsToPut( new Item() .withPrimaryKey(“ForumTitle”,”XYZ CRM”,”Topic”,”Updates”) .withHashAndRangeKeysToDelete(“ForumTitle”,”A partition key value”, “Product Line 1”, “A sort key value”)); BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem ( forumTableWriteItems, threadTableWriteItems); The following program is another bigger example for better understanding of how a batch writes with Java. Note − The following example may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources). This example also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project. package com.amazonaws.codesamples.document; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Map; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.TableWriteItems; import com.amazonaws.services.dynamodbv2.model.WriteRequest; public class BatchWriteOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); static String forumTableName = “Forum”; static String threadTableName = “Thread”; public static void main(String[] args) throws IOException { batchWriteMultiItems(); } private static void batchWriteMultiItems() { try { // Place new item in Forum TableWriteItems forumTableWriteItems = new TableWriteItems(forumTableName) //Forum .withItemsToPut(new Item() .withPrimaryKey(“Name”, “Amazon RDS”) .withNumber(“Threads”, 0)); // Place one item, delete another in Thread // Specify partition key and range key TableWriteItems threadTableWriteItems = new TableWriteItems(threadTableName) .withItemsToPut(new Item() .withPrimaryKey(“ForumName”,”Product Support”,”Subject”,”Support Thread 1″) .withString(“Message”, “New OS Thread 1 message”) .withHashAndRangeKeysToDelete(“ForumName”,”Subject”, “Polymer Blaster”, “Support Thread 100”)); System.out.println(“Processing request…”); BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem ( forumTableWriteItems, threadTableWriteItems); do { // Confirm no unprocessed items Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems(); if (outcome.getUnprocessedItems().size() == 0) { System.out.println(“All items processed.”); } else { System.out.println(“Gathering unprocessed items…”); outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems); } } while (outcome.getUnprocessedItems().size() > 0); } catch (Exception e) { System.err.println(“Could not get items: “); e.printStackTrace(System.err); } } } Print Page Previous Next Advertisements ”;

DynamoDB – API Interface

DynamoDB – API Interface ”; Previous Next DynamoDB offers a wide set of powerful API tools for table manipulation, data reads, and data modification. Amazon recommends using AWS SDKs (e.g., the Java SDK) rather than calling low-level APIs. The libraries make interacting with low-level APIs directly unnecessary. The libraries simplify common tasks such as authentication, serialization, and connections. Manipulate Tables DynamoDB offers five low-level actions for Table Management − CreateTable − This spawns a table and includes throughput set by the user. It requires you to set a primary key, whether composite or simple. It also allows one or multiple secondary indexes. ListTables − This provides a list of all tables in the current AWS user”s account and tied to their endpoint. UpdateTable − This alters throughput, and global secondary index throughput. DescribeTable − This provides table metadata; for example, state, size, and indices. DeleteTable − This simply erases the table and its indices. Read Data DynamoDB offers four low-level actions for data reading − GetItem − It accepts a primary key and returns attributes of the associated item. It permits changes to its default eventually consistent read setting. BatchGetItem − It executes several GetItem requests on multiple items through primary keys, with the option of one or multiple tables. Its returns no more than 100 items and must remain under 16MB. It permits eventually consistent and strongly consistent reads. Scan − It reads all the table items and produces an eventually consistent result set. You can filter results through conditions. It avoids the use of an index and scans the entire table, so do not use it for queries requiring predictability. Query − It returns a single or multiple table items or secondary index items. It uses a specified value for the partition key, and permits the use of comparison operators to narrow scope. It includes support for both types of consistency, and each response obeys a 1MB limit in size. Modify Data DynamoDB offers four low-level actions for data modification − PutItem − This spawns a new item or replaces existing items. On discovery of identical primary keys, by default, it replaces the item. Conditional operators allow you to work around the default, and only replace items under certain conditions. BatchWriteItem − This executes both multiple PutItem and DeleteItem requests, and over several tables. If one request fails, it does not impact the entire operation. Its cap sits at 25 items, and 16MB in size. UpdateItem − It changes the existing item attributes, and permits the use of conditional operators to execute updates only under certain conditions. DeleteItem − It uses the primary key to erase an item, and also allows the use of conditional operators to specify the conditions for deletion. Print Page Previous Next Advertisements ”;

DynamoDB – Access Control

DynamoDB – Access Control ”; Previous Next DynamoDB uses credentials you provide to authenticate requests. These credentials are required and must include permissions for AWS resource access. These permissions span virtually every aspect of DynamoDB down to the minor features of an operation or functionality. Types of Permissions In this section, we will discuss regarding the various permissions and resource access in DynamoDB. Authenticating Users On signup, you provided a password and email, which serve as root credentials. DynamoDB associates this data with your AWS account, and uses it to give complete access to all resources. AWS recommends you use your root credentials only for the creation of an administration account. This allows you to create IAM accounts/users with less privileges. IAM users are other accounts spawned with the IAM service. Their access permissions/privileges include access to secure pages and certain custom permissions like table modification. The access keys provide another option for additional accounts and access. Use them to grant access, and also to avoid manual granting of access in certain situations. Federated users provide yet another option by allowing access through an identity provider. Administration AWS resources remain under ownership of an account. Permissions policies govern the permissions granted to spawn or access resources. Administrators associate permissions policies with IAM identities, meaning roles, groups, users, and services. They also attach permissions to resources. Permissions specify users, resources, and actions. Note administrators are merely accounts with administrator privileges. Operation and Resources Tables remain the main resources in DynamoDB. Subresources serve as additional resources, e.g., streams and indices. These resources use unique names, some of which are mentioned in the following table − Type ARN (Amazon Resource Name) Stream arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label Index arn:aws:dynamodb:region:account-id:table/table-name/index/index-name Table arn:aws:dynamodb:region:account-id:table/table-name Ownership A resource owner is defined as an AWS account which spawned the resource, or principal entity account responsible for request authentication in resource creation. Consider how this functions within the DynamoDB environment − In using root credentials to create a table, your account remains resource owner. In creating an IAM user and granting the user permission to create a table, your account remains the resource owner. In creating an IAM user and granting the user, and anyone capable of assuming the role, permission to create a table, your account remains the resource owner. Manage Resource Access Management of access mainly requires attention to a permissions policy describing users and resource access. You associate policies with IAM identities or resources. However, DynamoDB only supports IAM/identity policies. Identity-based (IAM) policies allow you to grant privileges in the following ways − Attach permissions to users or groups. Attach permissions to roles for cross-account permissions. Other AWS allow resource-based policies. These policies permit access to things like an S3 bucket. Policy Elements Policies define actions, effects, resources, and principals; and grant permission to perform these operations. Note − The API operations may require permissions for multiple actions. Take a closer look at the following policy elements − Resource − An ARN identifies this. Action − Keywords identify these resource operations, and whether to allow or deny. Effect − It specifies the effect for a user request for an action, meaning allow or deny with denial as the default. Principal − This identifies the user attached to the policy. Conditions In granting permissions, you can specify conditions for when policies become active such as on a particular date. Express conditions with condition keys, which include AWS systemwide keys and DynamoDB keys. These keys are discussed in detail later in the tutorial. Console Permissions A user requires certain basic permissions to use the console. They also require permissions for the console in other standard services − CloudWatch Data Pipeline Identity and Access Management Notification Service Lambda If the IAM policy proves too limited, the user cannot use the console effectively. Also, you do not need to worry about user permissions for those only calling the CLI or API. Common Use Iam Policies AWS covers common operations in permissions with standalone IAM managed policies. They provide key permissions allowing you to avoid deep investigations into what you must grant. Some of them are as follows − AmazonDynamoDBReadOnlyAccess − It gives read-only access via the console. AmazonDynamoDBFullAccess − It gives full access via the console. AmazonDynamoDBFullAccesswithDataPipeline − It gives full access via the console and permits export/import with Data Pipeline. You can also ofcourse make custom policies. Granting Privileges: Using The Shell You can grant permissions with the Javascript shell. The following program shows a typical permissions policy − { “Version”: “2016-05-22”, “Statement”: [ { “Sid”: “DescribeQueryScanToolsTable”, “Effect”: “Deny”, “Action”: [ “dynamodb:DescribeTable”, “dynamodb:Query”, “dynamodb:Scan” ], “Resource”: “arn:aws:dynamodb:us-west-2:account-id:table/Tools” } ] } You can review the three examples which are as follows − Block the user from executing any table action. { “Version”: “2016-05-23”, “Statement”: [ { “Sid”: “AllAPIActionsOnTools”, “Effect”: “Deny”, “Action”: “dynamodb:*”, “Resource”: “arn:aws:dynamodb:us-west-2:155556789012:table/Tools” } ] } Block access to a table and its indices. { “Version”: “2016-05-23”, “Statement”: [ { “Sid”: “AccessAllIndexesOnTools”, “Effect”: “Deny”, “Action”: [ “dynamodb:*” ], “Resource”: [ “arn:aws:dynamodb:us-west-2:155556789012:table/Tools”, “arn:aws:dynamodb:us-west-2:155556789012:table/Tools/index/*” ] } ] } Block a user from making a reserved capacity offering purchase. { “Version”: “2016-05-23”, “Statement”: [ { “Sid”: “BlockReservedCapacityPurchases”, “Effect”: “Deny”, “Action”: “dynamodb:PurchaseReservedCapacityOfferings”, “Resource”: “arn:aws:dynamodb:us-west-2:155556789012:*” } ] } Granting Privileges: Using the GUI Console You can also use the GUI console to create IAM policies. To begin with, choose Tables from the navigation pane. In the table list, choose the target table and follow these steps. Step 1 − Select the Access control tab. Step 2 − Select the identity provider, actions, and policy attributes. Select Create policy after entering all settings. Step 3 − Choose Attach policy instructions, and complete each required step to associate the policy with the appropriate IAM role. Print Page Previous Next Advertisements ”;

DynamoDB – Operations Tools

DynamoDB – Operations Tools ”; Previous Next DynamoDB provides three options for performing operations: a web-based GUI console, a JavaScript shell, and a programming language of your choice. In this tutorial, we will focus on using the GUI console and Java language for clarity and conceptual understanding. GUI Console The GUI console or the AWS Management Console for Amazon DynamoDB can be found at the following address − https://console.aws.amazon.com/dynamodb/home It allows you to perform the following tasks − CRUD View Table Items Perform Table Queries Set Alarms for Table Capacity Monitoring View Table Metrics in Real-Time View Table Alarms If your DynamoDB account has no tables, on access, it guides you through creating a table. Its main screen offers three shortcuts for performing common operations − Create Tables Add and Query Tables Monitor and Manage Tables The JavaScript Shell DynamoDB includes an interactive JavaScript shell. The shell runs inside a web browser, and the recommended browsers include Firefox and Chrome. Note − Using other browsers may result in errors. Access the shell by opening a web browser and entering the following address −http://localhost:8000/shell Use the shell by entering JavaScript in the left pane, and clicking the “Play” icon button in the top right corner of the left pane, which runs the code. The code results display in the right pane. DynamoDB and Java Use Java with DynamoDB by utilizing your Java development environment. Operations confirm to normal Java syntax and structure. Print Page Previous Next Advertisements ”;

DynamoDB – Load Table

DynamoDB – Load Table ”; Previous Next Loading a table generally consists of creating a source file, ensuring the source file conforms to a syntax compatible with DynamoDB, sending the source file to the destination, and then confirming a successful population. Utilize the GUI console, Java, or another option to perform the task. Load Table using GUI Console Load data using a combination of the command line and console. You can load data in multiple ways, some of which are as follows − The Console The Command Line Code and also Data Pipeline (a feature discussed later in the tutorial) However, for speed, this example uses both the shell and console. First, load the source data into the destination with the following syntax − aws dynamodb batch-write-item -–request-items file://[filename] For example − aws dynamodb batch-write-item -–request-items file://MyProductData.json Verify the success of the operation by accessing the console at − https://console.aws.amazon.com/dynamodb Choose Tables from the navigation pane, and select the destination table from the table list. Select the Items tab to examine the data you used to populate the table. Select Cancel to return to the table list. Load Table using Java Employ Java by first creating a source file. Our source file uses JSON format. Each product has two primary key attributes (ID and Nomenclature) and a JSON map (Stat) − [ { “ID” : … , “Nomenclature” : … , “Stat” : { … } }, { “ID” : … , “Nomenclature” : … , “Stat” : { … } }, … ] You can review the following example − { “ID” : 122, “Nomenclature” : “Particle Blaster 5000”, “Stat” : { “Manufacturer” : “XYZ Inc.”, “sales” : “1M+”, “quantity” : 500, “img_src” : “http://www.xyz.com/manuals/particleblaster5000.jpg”, “description” : “A laser cutter used in plastic manufacturing.” } } The next step is to place the file in the directory used by your application. Java primarily uses the putItem and path methods to perform the load. You can review the following code example for processing a file and loading it − import java.io.File; import java.util.Iterator; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.ObjectNode; public class ProductsLoadData { public static void main(String[] args) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient() .withEndpoint(“http://localhost:8000”); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(“Products”); JsonParser parser = new JsonFactory() .createParser(new File(“productinfo.json”)); JsonNode rootNode = new ObjectMapper().readTree(parser); Iterator<JsonNode> iter = rootNode.iterator(); ObjectNode currentNode; while (iter.hasNext()) { currentNode = (ObjectNode) iter.next(); int ID = currentNode.path(“ID”).asInt(); String Nomenclature = currentNode.path(“Nomenclature”).asText(); try { table.putItem(new Item() .withPrimaryKey(“ID”, ID, “Nomenclature”, Nomenclature) .withJSON(“Stat”, currentNode.path(“Stat”).toString())); System.out.println(“Successful load: ” + ID + ” ” + Nomenclature); } catch (Exception e) { System.err.println(“Cannot add product: ” + ID + ” ” + Nomenclature); System.err.println(e.getMessage()); break; } } parser.close(); } } Print Page Previous Next Advertisements ”;

DynamoDB – Overview

DynamoDB – Overview ”; Previous Next DynamoDB allows users to create databases capable of storing and retrieving any amount of data, and serving any amount of traffic. It automatically distributes data and traffic over servers to dynamically manage each customer”s requests, and also maintains fast performance. DynamoDB vs. RDBMS DynamoDB uses a NoSQL model, which means it uses a non-relational system. The following table highlights the differences between DynamoDB and RDBMS − Common Tasks RDBMS DynamoDB Connect to the Source It uses a persistent connection and SQL commands. It uses HTTP requests and API operations Create a Table Its fundamental structures are tables, and must be defined. It only uses primary keys, and no schema on creation. It uses various data sources. Get Table Info All table info remains accessible Only primary keys are revealed. Load Table Data It uses rows made of columns. In tables, it uses items made of attributes Read Table Data It uses SELECT statements and filtering statements. It uses GetItem, Query, and Scan. Manage Indexes It uses standard indexes created through SQL statements. Modifications to it occur automatically on table changes. It uses a secondary index to achieve the same function. It requires specifications (partition key and sort key). Modify Table Data It uses an UPDATE statement. It uses an UpdateItem operation. Delete Table Data It uses a DELETE statement. It uses a DeleteItem operation. Delete a Table It uses a DROP TABLE statement. It uses a DeleteTable operation. Advantages The two main advantages of DynamoDB are scalability and flexibility. It does not force the use of a particular data source and structure, allowing users to work with virtually anything, but in a uniform way. Its design also supports a wide range of use from lighter tasks and operations to demanding enterprise functionality. It also allows simple use of multiple languages: Ruby, Java, Python, C#, Erlang, PHP, and Perl. Limitations DynamoDB does suffer from certain limitations, however, these limitations do not necessarily create huge problems or hinder solid development. You can review them from the following points − Capacity Unit Sizes − A read capacity unit is a single consistent read per second for items no larger than 4KB. A write capacity unit is a single write per second for items no bigger than 1KB. Provisioned Throughput Min/Max − All tables and global secondary indices have a minimum of one read and one write capacity unit. Maximums depend on region. In the US, 40K read and write remains the cap per table (80K per account), and other regions have a cap of 10K per table with a 20K account cap. Provisioned Throughput Increase and Decrease − You can increase this as often as needed, but decreases remain limited to no more than four times daily per table. Table Size and Quantity Per Account − Table sizes have no limits, but accounts have a 256 table limit unless you request a higher cap. Secondary Indexes Per Table − Five local and five global are permitted. Projected Secondary Index Attributes Per Table − DynamoDB allows 20 attributes. Partition Key Length and Values − Their minimum length sits at 1 byte, and maximum at 2048 bytes, however, DynamoDB places no limit on values. Sort Key Length and Values − Its minimum length stands at 1 byte, and maximum at 1024 bytes, with no limit for values unless its table uses a local secondary index. Table and Secondary Index Names − Names must conform to a minimum of 3 characters in length, and a maximum of 255. They use the following characters: AZ, a-z, 0-9, “_”, “-”, and “.”. Attribute Names − One character remains the minimum, and 64KB the maximum, with exceptions for keys and certain attributes. Reserved Words − DynamoDB does not prevent the use of reserved words as names. Expression Length − Expression strings have a 4KB limit. Attribute expressions have a 255-byte limit. Substitution variables of an expression have a 2MB limit. Print Page Previous Next Advertisements ”;