Using Amazon DynamoDB

AWS DynamoDB is a widely used NoSQL data store. Typically, relational databases are not the best option for large-scale, data-centric applications. In addition, operating these database servers at scale is complex and expensive. For such use cases, NoSQL data stores, such as Amazon DynamoDB, are an effective solution. DynamoDB can be used as a key/value store or as a document store. It is common to store data as JSON documents.

DynamoDB stores data in tables, and each table contains items. An item is a group of attributes that is uniquely identifiable among all the other items in the table. An item is similar to rows in a relational database table, and an attribute is a data element similar to fields or columns in a relational database. There is no upper limit to the number of items that can be stored in a DynamoDB table. A primary key uniquely identifies the items in a table, and the key values need to be supplied for CRUD operations on a given table.

DynamoDB's flexible schema means that not all the items necessarily contain all the attributes. Amazon DynamoDB lets you specify your throughput needs in terms of units of read capacity and writes capacity for your table. During the creation of a table, you specify your required read and write capacity needs, and Amazon DynamoDB automatically partitions and reserves the appropriate amount of resources to meet your throughput requirements.

DynamoDB runs on Solid State Disks (SSDs) to give predictable performance. You can update the throughput capacity using the AWS console or DynamoDB API to match your application traffic. AWS manages the infrastructure provisioning, software installation and maintenance, and high availability for the DynamoDB service.

DynamoDB is integrated with other AWS and non-AWS services. For example, you can monitor it using CloudWatch, use IAM to control access to DynamoDB resources, log API calls using Cloutrail, search content using Elasticsearch, and so on.

How to do it…

  1. Installing AWS Java SDK.

    In your Maven dependency section, add the following dependency for AWS Java SDK Version 1.9.28.1:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.9.28.1</version>
    </dependency>
  2. Initialize DynamoDB client.

    The following code initializes and returns a DynamoDB client.

        // Initialize DynamoDB client.
            public static AmazonDynamoDBClient Initialize() {
    
        // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
                "Access Key Id",
                " Secret Access Key");
        // Create DynamoDB client.
            AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(
                credentials);
    
        // Set endpoint.
            dynamoDBClient.setEndpoint("dynamodb.ap-southeast- 1.amazonaws.com");
    
        // Return client.
            return dynamoDBClient;
        }
  3. Create the DynamoDB table.

    The following code creates DynamoDB table with the name "user" and uses UserId as the primary key:

        // Create table.
            public static void CreateTable() {
    
        // Get DynamoDB client.
            AmazonDynamoDBClient dynamoDBClient = Initialize();
    
        // Create attribute definition collection.
            ArrayList<AttributeDefinition> definitions = new ArrayList<AttributeDefinition>();
    
        // Add attribute definition for UserId.
            definitions.add(new AttributeDefinition().withAttributeName("UserId")
                .withAttributeType("N"));
    
        // Create key schema element collection.
            ArrayList<KeySchemaElement> schema = new ArrayList<KeySchemaElement>();
    
        // Set UserId as primary key and type as HASH.
            schema.add(new KeySchemaElement().withAttributeName("UserId")
                .withKeyType(KeyType.HASH));
    
        // Set provisioned throughput.
            ProvisionedThroughput throughput = new ProvisionedThroughput()
                .withReadCapacityUnits(10L) .withWriteCapacityUnits(10L);
    
        // Prepare create table request.
            CreateTableRequest request = new CreateTableRequest()
                .withTableName("user").withKeySchema(schema)
                .withProvisionedThroughput(throughput);
    
        // Set attribute definitions.
            request.setAttributeDefinitions(definitions);
    
        // Creates table.
            dynamoDBClient.createTable(request);
        }
  4. Create the User class.
    @DynamoDBTable(tableName = "user")
    public class User {
    
        public int UserId;
    
        public String Name;
    
        public int Age;
    
        public String City;
    
        @DynamoDBHashKey(attributeName = "UserId")
        public int getUserId() {
            return UserId;
        }
    
        public void setUserId(int userId) {
            UserId = userId;
        }
    
        public String getName() {
            return Name;
        }
    
        public void setName(String name) {
            Name = name;
        }
        public int getAge() {
        return Age;
        }
    
        public void setAge(int age) {
            Age = age;
        }
    
        public String getCity() {
            return City;
        }
    
        public void setCity(String city) {
            City = city;
        }
    
    }
  5. After creating the User class, you can use the following function to save the user object into the DynamoDB user table:
    // Insert item.
        public static void InsertItem() {
    
        // Get DynamoDB client.
            AmazonDynamoDBClient dynamoDBClient = Initialize();
    
        // Create mapper object.
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
    
        // Initialize user object.
            User objUser = new User();
    
        // Set user id.
            objUser.UserId = 123;
    
        // Set user name.
            objUser.Name = "Andrew";
    
        // Set age.
            objUser.Age = 28;
    
        // Set city.
            objUser.City = "San Francisco";
        // Save user item.
            mapper.save(objUser);
        }
  6. Retrieve item from the table.

    The following code illustrates a query for retrieving a user from user table with the UserId value 123:

         // Retrieve item.
            public static void GetItem() {
    
        // Get DynamoDB client.
            AmazonDynamoDBClient dynamoDBClient = Initialize();
    
        // Create mapper object.
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
    
        // Initialize user object.
            User objUser = new User();
    
        // Set user id.
            objUser.UserId = 123;
    
        // Prepare query.
            DynamoDBQueryExpression<User> query = new DynamoDBQueryExpression<User>()
                    .withHashKeyValues(objUser);
    
        // Retrieve items from DynamoDB.
            List<User> list = mapper.query(User.class, query);
    
        // Iterate through all items.
            for (User user : list) {
    
        // Get user name.
                String userName = user.getName();
    
        // Get user age.
                int userAge = user.getAge();
            }

How it works…

In the first step, we downloaded AWS Java SDK. This SDK helps us access AWS services from Java applications. The AWS Java SDK provides an object persistence model to define the relationships between the database tables and the objects. After the mapping is done, the objects map to items in your table, and you can use the object methods for CRUD operations. In turn, the appropriate low-level APIs are invoked for you. The SDK also provides a set of annotations, for example, to map your class to a database table and the class attributes to the corresponding item attribute.

Before interacting with DynamoDB, we need to specify the access key ID and secret access key. Alternatively, we can create an IAM user and assign DynamoDB permissions to him/her, and then use his/her credentials to access DynamoDB.

Next, we create the DynamoDB client object that is used to interact with DynamoDB service.

Next, we create a DynamoDB table. When creating a DynamoDB table, you have to specify a primary key. This primary key uniquely identifies each row in the table. DynamoDB distributes the data based on the hash key element into multiple partitions. DynamoDB supports two types of primary keys, hash-type primary key and the hash and range type primary key.

You can specify the provisioned throughput capacity for read and write operations when you create or update a table. DynamoDB will reserve the required resources to meet your throughput requirements.

A unit of read capacity represents one strongly consistent read per second (or two eventually consistent reads per second) for items of size up to 4 KB. A unit of write represents one write per second for items of size up to 1 KB. You cannot group multiple items in a single read or write operation even if the items are 4 KB or smaller or 1 KB or smaller, respectively.

Items larger than 4 KB require more than one read operation. For example, the number of read operations required for a 7-KB item is 2, and for a 10-KB item is 3. The AWS console can be used to monitor the provisioned and actual throughput, and the provisioned throughput can be appropriately tuned to avoid throttling.

After creating the DynamoDB table, you can insert items into it. An item represents a database row, and each item can have multiple attributes. An attribute represents the database columns. DynamoDB supports multiple data types, including scalar types (number, string, binary, Boolean, and null), multivalued types (string set, number set, and binary set), and document types (list and map). The primary key must be a string, number, or binary.

We specify the DynamoDB table name and primary key attribute name using DynamoDB annotations. Hence, in our create User class, we annotate the class with @DynamoDBTable(tableName = "user") and the getUserId function with @DynamoDBHashKey(attributeName = "UserId").

Lastly, we test our insert by retrieving items from our DynamoDB table. DynamoDB supports both eventually consistent and strongly consistent read options. In eventual consistency, when you read data immediately following a write operation, the response might not reflect the results of the write. However, note that data consistency across the multiple copies of the data is typically reached within a second. Hence, if the read operation is retried after a short time, the result of the write operation is reflected in the response. In the case of strongly consistent reads, the latest post-write operation data is returned. If you use eventually consistent reads versus strongly consistent reads, then you will get twice as many reads per second.

There's more…

DynamoDB supports two kinds of primary keys, a primary key consisting of a hash attribute and a primary key consisting of hash and range attributes. In the first type, the primary key consists of a single attribute—the hash attribute. In the second type, there are two attributes—the hash attribute and the range attribute.

DynamoDB also supports secondary indexes. If you want to access the data using non-key attributes, then you would create these secondary indexes. However, DynamoDB consumes additional capacity units to update the indexes on write operations. You might need to adjust your provisioned capacity over a period of time as more secondary indexes are added to your tables.

There are two querying operations available—query and scan. You can use the query operation to query a table using the hash attribute and an optional range filter or the secondary index key. The Scan operation reads every item in the table or the secondary index, and is an expensive operation for large tables and secondary indexes.

You can download and run DynamoDB on your computer. This can be a good way to learn to work with DynamoDB as well as use it reduce your cloud development costs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset