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 ”;
Category: dynamodb
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 – Querying
DynamoDB – Querying ”; Previous Next Queries locate items or secondary indices through primary keys. Performing a query requires a partition key and specific value, or a sort key and value; with the option to filter with comparisons. The default behavior of a query consists of returning every attribute for items associated with the provided primary key. However, you can specify the desired attributes with the ProjectionExpression parameter. A query utilizes the KeyConditionExpression parameters to select items, which requires providing the partition key name and value in the form of an equality condition. You also have the option to provide an additional condition for any sort keys present. A few examples of the sort key conditions are − Sr.No Condition & Description 1 x = y It evaluates to true if the attribute x equals y. 2 x < y It evaluates to true if x is less than y. 3 x <= y It evaluates to true if x is less than or equal to y. 4 x > y It evaluates to true if x is greater than y. 5 x >= y It evaluates to true if x is greater than or equal to y. 6 x BETWEEN y AND z It evaluates to true if x is both >= y, and <= z. DynamoDB also supports the following functions: begins_with (x, substr) It evaluates to true if attribute x starts with the specified string. The following conditions must conform to certain requirements − Attribute names must start with a character within the a-z or A-Z set. The second character of an attribute name must fall in the a-z, A-Z, or 0-9 set. Attribute names cannot use reserved words. Attribute names out of compliance with the constraints above can define a placeholder. The query processes by performing retrievals in sort key order, and using any condition and filter expressions present. Queries always return a result set, and on no matches, it returns an empty one. The results always return in sort key order, and data type based order with the modifiable default as the ascending order. Querying with Java Queries in Java allow you to query tables and secondary indices. They require specification of partition keys and equality conditions, with the option to specify sort keys and conditions. The general required steps for a query in Java include creating a DynamoDB class instance, Table class instance for the target table, and calling the query method of the Table instance to receive the query object. The response to the query contains an ItemCollection object providing all the returned items. The following example demonstrates detailed querying − DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient(new ProfileCredentialsProvider())); Table table = dynamoDB.getTable(“Response”); QuerySpec spec = new QuerySpec() .withKeyConditionExpression(“ID = :nn”) .withValueMap(new ValueMap() .withString(“:nn”, “Product Line 1#P1 Thread 1”)); ItemCollection<QueryOutcome> items = table.query(spec); Iterator<Item> iterator = items.iterator(); Item item = null; while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.toJSONPretty()); } The query method supports a wide variety of optional parameters. The following example demonstrates how to utilize these parameters − Table table = dynamoDB.getTable(“Response”); QuerySpec spec = new QuerySpec() .withKeyConditionExpression(“ID = :nn and ResponseTM > :nn_responseTM”) .withFilterExpression(“Author = :nn_author”) .withValueMap(new ValueMap() .withString(“:nn”, “Product Line 1#P1 Thread 1”) .withString(“:nn_responseTM”, twoWeeksAgoStr) .withString(“:nn_author”, “Member 123”)) .withConsistentRead(true); ItemCollection<QueryOutcome> items = table.query(spec); Iterator<Item> iterator = items.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } You can also 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 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.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import com.amazonaws.auth.profile.ProfileCredentialsProvider; 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.ItemCollection; import com.amazonaws.services.dynamodbv2.document.Page; import com.amazonaws.services.dynamodbv2.document.QueryOutcome; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; public class QueryOpSample { static DynamoDB dynamoDB = new DynamoDB( new AmazonDynamoDBClient(new ProfileCredentialsProvider())); static String tableName = “Reply”; public static void main(String[] args) throws Exception { String forumName = “PolyBlaster”; String threadSubject = “PolyBlaster Thread 1”; getThreadReplies(forumName, threadSubject); } private static void getThreadReplies(String forumName, String threadSubject) { Table table = dynamoDB.getTable(tableName); String replyId = forumName + “#” + threadSubject; QuerySpec spec = new QuerySpec() .withKeyConditionExpression(“Id = :v_id”) .withValueMap(new ValueMap() .withString(“:v_id”, replyId)); ItemCollection<QueryOutcome> items = table.query(spec); System.out.println(“ngetThreadReplies results:”); Iterator<Item> iterator = items.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } } } Print Page Previous Next Advertisements ”;
DynamoDB – Permissions API
DynamoDB – Permissions API ”; Previous Next DynamoDB API offers a large set of actions, which require permissions. In setting permissions, you must establish the actions permitted, resources permitted, and conditions of each. You can specify actions within the Action field of the policy. Specify resource value within the Resource field of the policy. But do ensure that you use the correct syntax containing the Dynamodb: prefix with the API operation. For example − dynamodb:CreateTable You can also employ condition keys to filter permissions. Permissions and API Actions Take a good look at the API actions and associated permissions given in the following table − API Operation Necessary Permission BatchGetItem dynamodb:BatchGetItem BatchWriteItem dynamodb:BatchWriteItem CreateTable dynamodb:CreateTable DeleteItem dynamodb:DeleteItem DeleteTable dynamodb:DeleteTable DescribeLimits dynamodb:DescribeLimits DescribeReservedCapacity dynamodb:DescribeReservedCapacity DescribeReservedCapacityOfferings dynamodb:DescribeReservedCapacityOfferings DescribeStream dynamodb:DescribeStream DescribeTable dynamodb:DescribeTable GetItem dynamodb:GetItem GetRecords dynamodb:GetRecords GetShardIterator dynamodb:GetShardIterator ListStreams dynamodb:ListStreams ListTables dynamodb:ListTables PurchaseReservedCapacityOfferings dynamodb:PurchaseReservedCapacityOfferings PutItem dynamodb:PutItem Query dynamodb:Query Scan dynamodb:Scan UpdateItem dynamodb:UpdateItem UpdateTable dynamodb:UpdateTable Resources In the following table, you can review the resources associated with each permitted API action − API Operation Resource BatchGetItem arn:aws:dynamodb:region:account-id:table/table-name BatchWriteItem arn:aws:dynamodb:region:account-id:table/table-name CreateTable arn:aws:dynamodb:region:account-id:table/table-name DeleteItem arn:aws:dynamodb:region:account-id:table/table-name DeleteTable arn:aws:dynamodb:region:account-id:table/table-name DescribeLimits arn:aws:dynamodb:region:account-id:* DescribeReservedCapacity arn:aws:dynamodb:region:account-id:* DescribeReservedCapacityOfferings arn:aws:dynamodb:region:account-id:* DescribeStream arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label DescribeTable arn:aws:dynamodb:region:account-id:table/table-name GetItem arn:aws:dynamodb:region:account-id:table/table-name GetRecords arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label GetShardIterator arn:aws:dynamodb:region:account-id:table/table-name/stream/stream-label ListStreams arn:aws:dynamodb:region:account-id:table/table-name/stream/* ListTables * PurchaseReservedCapacityOfferings arn:aws:dynamodb:region:account-id:* PutItem arn:aws:dynamodb:region:account-id:table/table-name Query arn:aws:dynamodb:region:account-id:table/table-name or arn:aws:dynamodb:region:account-id:table/table-name/index/index-name Scan arn:aws:dynamodb:region:account-id:table/table-name or arn:aws:dynamodb:region:account-id:table/table-name/index/index-name UpdateItem arn:aws:dynamodb:region:account-id:table/table-name UpdateTable arn:aws:dynamodb:region:account-id:table/table-name Print Page Previous Next Advertisements ”;
DynamoDB – Delete Table
DynamoDB – Delete Table ”; Previous Next In this chapter, we will discuss regarding how we can delete a table and also the different ways of deleting a table. Table deletion is a simple operation requiring little more than the table name. Utilize the GUI console, Java, or any other option to perform this task. Delete Table using the GUI Console Perform a delete operation by first accessing the console at − https://console.aws.amazon.com/dynamodb. Choose Tables from the navigation pane, and choose the table desired for deletion from the table list as shown in the following screeenshot. Finally, select Delete Table. After choosing Delete Table, a confirmation appears. Your table is then deleted. Delete Table using Java Use the delete method to remove a table. An example is given below to explain the concept better. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; public class ProductsDeleteTable { 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”); try { System.out.println(“Performing table delete, wait…”); table.delete(); table.waitForDelete(); System.out.print(“Table successfully deleted.”); } catch (Exception e) { System.err.println(“Cannot perform table delete: “); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;
DynamoDB – Basic Concepts
DynamoDB – Basic Concepts ”; Previous Next Before using DynamoDB, you must familiarize yourself with its basic components and ecosystem. In the DynamoDB ecosystem, you work with tables, attributes, and items. A table holds sets of items, and items hold sets of attributes. An attribute is a fundamental element of data requiring no further decomposition, i.e., a field. Primary Key The Primary Keys serve as the means of unique identification for table items, and secondary indexes provide query flexibility. DynamoDB streams record events by modifying the table data. The Table Creation requires not only setting a name, but also the primary key; which identifies table items. No two items share a key. DynamoDB uses two types of primary keys − Partition Key − This simple primary key consists of a single attribute referred to as the “partition key.” Internally, DynamoDB uses the key value as input for a hash function to determine storage. Partition Key and Sort Key − This key, known as the “Composite Primary Key”, consists of two attributes. The partition key and The sort key. DynamoDB applies the first attribute to a hash function, and stores items with the same partition key together; with their order determined by the sort key. Items can share partition keys, but not sort keys. The Primary Key attributes only allow scalar (single) values; and string, number, or binary data types. The non-key attributes do not have these constraints. Secondary Indexes These indexes allow you to query table data with an alternate key. Though DynamoDB does not force their use, they optimize querying. DynamoDB uses two types of secondary indexes − Global Secondary Index − This index possesses partition and sort keys, which can differ from table keys. Local Secondary Index − This index possesses a partition key identical to the table, however, its sort key differs. API The API operations offered by DynamoDB include those of the control plane, data plane (e.g., creation, reading, updating, and deleting), and streams. In control plane operations, you create and manage tables with the following tools − CreateTable DescribeTable ListTables UpdateTable DeleteTable In the data plane, you perform CRUD operations with the following tools − Create Read Update Delete PutItem BatchWriteItem GetItem BatchGetItem Query Scan UpdateItem DeleteItem BatchWriteItem The stream operations control table streams. You can review the following stream tools − ListStreams DescribeStream GetShardIterator GetRecords Provisioned Throughput In table creation, you specify provisioned throughput, which reserves resources for reads and writes. You use capacity units to measure and set throughput. When applications exceed the set throughput, requests fail. The DynamoDB GUI console allows monitoring of set and used throughput for better and dynamic provisioning. Read Consistency DynamoDB uses eventually consistent and strongly consistent reads to support dynamic application needs. Eventually consistent reads do not always deliver current data. The strongly consistent reads always deliver current data (with the exception of equipment failure or network problems). Eventually consistent reads serve as the default setting, requiring a setting of true in the ConsistentRead parameter to change it. Partitions DynamoDB uses partitions for data storage. These storage allocations for tables have SSD backing and automatically replicate across zones. DynamoDB manages all the partition tasks, requiring no user involvement. In table creation, the table enters the CREATING state, which allocates partitions. When it reaches ACTIVE state, you can perform operations. The system alters partitions when its capacity reaches maximum or when you change throughput. Print Page Previous Next Advertisements ”;
DynamoDB – Query Table
DynamoDB – Query Table ”; Previous Next Querying a table primarily requires selecting a table, specifying a partition key, and executing the query; with the options of using secondary indexes and performing deeper filtering through scan operations. Utilize the GUI Console, Java, or another option to perform the task. Query Table using the GUI Console Perform some simple queries using the previously created tables. First, open the console at https://console.aws.amazon.com/dynamodb Choose Tables from the navigation pane and select Reply from the table list. Then select the Items tab to see the loaded data. Select the data filtering link (“Scan: [Table] Reply”) beneath the Create Item button. In the filtering screen, select Query for the operation. Enter the appropriate partition key value, and click Start. The Reply table then returns matching items. Query Table using Java Use the query method in Java to perform data retrieval operations. It requires specifying the partition key value, with the sort key as optional. Code a Java query by first creating a querySpec object describing parameters. Then pass the object to the query method. We use the partition key from the previous examples. You can review the following example − import java.util.HashMap; 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.ItemCollection; import com.amazonaws.services.dynamodbv2.document.QueryOutcome; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; public class ProductsQuery { 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”); HashMap<String, String> nameMap = new HashMap<String, String>(); nameMap.put(“#ID”, “ID”); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(“:xxx”, 122); QuerySpec querySpec = new QuerySpec() .withKeyConditionExpression(“#ID = :xxx”) .withNameMap(new NameMap().with(“#ID”, “ID”)) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = null; Iterator<Item> iterator = null; Item item = null; try { System.out.println(“Product with the ID 122”); items = table.query(querySpec); iterator = items.iterator(); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber(“ID”) + “: ” + item.getString(“Nomenclature”)); } } catch (Exception e) { System.err.println(“Cannot find products with the ID number 122”); System.err.println(e.getMessage()); } } } Note that the query uses the partition key, however, secondary indexes provide another option for queries. Their flexibility allows querying of non-key attributes, a topic which will be discussed later in this tutorial. The scan method also supports retrieval operations by gathering all the table data. The optional .withFilterExpression prevents items outside of specified criteria from appearing in results. Later in this tutorial, we will discuss scanning in detail. Now, take a look at the following example − 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.ItemCollection; import com.amazonaws.services.dynamodbv2.document.ScanOutcome; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec; import com.amazonaws.services.dynamodbv2.document.utils.NameMap; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; public class ProductsScan { 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”); ScanSpec scanSpec = new ScanSpec() .withProjectionExpression(“#ID, Nomenclature , stat.sales”) .withFilterExpression(“#ID between :start_id and :end_id”) .withNameMap(new NameMap().with(“#ID”, “ID”)) .withValueMap(new ValueMap().withNumber(“:start_id”, 120) .withNumber(“:end_id”, 129)); try { ItemCollection<ScanOutcome> items = table.scan(scanSpec); Iterator<Item> iter = items.iterator(); while (iter.hasNext()) { Item item = iter.next(); System.out.println(item.toString()); } } catch (Exception e) { System.err.println(“Cannot perform a table scan:”); System.err.println(e.getMessage()); } } } Print Page Previous Next Advertisements ”;