How to update table in dynamoDB using Java – DynamoDB Tutorial

3 weeks ago Lalit Bhagtani 0

In this tutorial, we will learn about how to update a table in dynamoDB using java language.


Amazon DynamoDB is a fully managed NoSQL database services offered by Amazon as part of its Amazon Web Service (AWS) portfolio. It provides fast and predictable performance with seamless scalability. DynamoDB is a key-value datastore, where each item (row) in a table is a key-value pair.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.


For latest version of aws library, check this page.

Table Schema

In this example, we will update a JOB table, this table stores information about the job posted by the company on the job portal. Schema of this table is as follows:-

  1. Existing Attributes:
    1. CountryName ( Partition Key ) : It represents name of the country for which job is posted. It is stored as String data type ( ScalarAttributeType S ).
    2. JobId (Sort Key) : It represents unique id of the posted job. It is of Number datatype ( ScalarAttributeType N ).
  1. New Attributes:
    1. CompanyName : It represents name of the company by which job is posted. It is of String datatype ( ScalarAttributeType S ).
    2. JobTitle : As the name suggest, it represents tile of the job. It is of String datatype ( ScalarAttributeType S ).
    3. JobDescription : It represents details job description. It is of String datatype ( ScalarAttributeType S ).


DynamoDB Connection

Create an object of AmazonDynamoDB ( ) class for sending a client request to dynamoDB. To get instance of this class, we will use AmazonDynamoDBClientBuilder builder class. It requires three important parameters :- 

  1. Region :- It is a region where dynamoDB table will be stored.
  2. ACCESS_KEY :- It is a access key for using dynamoDB. You can generate this key, using aws management console.
  3. SECRET_KEY :- It is a secret key of above mentioned access key.

Here is a code example :-

AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
				.withCredentials(new AWSStaticCredentialsProvider
                                                    (new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))

Update Table

Steps to create and send UpdateTableRequest to dynamoDB are as follows:-

  1. Instantiate UpdateTableRequest object, this object will be used to send the client request to dynamoDB.
  2. Set name of the table which we like to update. In our case JOB.
  3. Create a list of AttributeDefinition objects to add new attributes in the table, It will define attribute names and their datatypes.
  4. Set Provisioned Throughput to update throughput of the table.
  5. Invoke the updateTable method on AmazonDynamoDB object by passing UpdateTableRequest object as an argument.


import java.util.Arrays;
import java.util.List;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;

public class UpdateTable {

	public static void main(String[] args) {
		/* Create DynamoDB Client Object */
		AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder
				.withCredentials(new AWSStaticCredentialsProvider(
                                    new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
		/* Create an Object of UpdateTableRequest */
		UpdateTableRequest request = new UpdateTableRequest();
		/* Setting Table Name */
		/* Create & Set a list of AttributeDefinition */
		List<AttributeDefinition> attributeDefinitions = Arrays.asList(
				new AttributeDefinition[] { new AttributeDefinition("CompanyName", ScalarAttributeType.S) });
		/* Setting Provisioned Throughput */
		request.setProvisionedThroughput(new ProvisionedThroughput(new Long(5), new Long(5)));
		try {
        	/* Send Update Table Request */
            UpdateTableResult result = dynamoDB.updateTable(request);
            System.out.println("Status : " +  result.getSdkHttpMetadata().getHttpStatusCode());
            System.out.println("Table Name : " +  result.getTableDescription().getTableName());
            /* Creating and Sending request using Fluent API, Adding JobTitle Attribute */
            UpdateTableResult resultFluent = dynamoDB.updateTable((new UpdateTableRequest()).withTableName("JOB")
                    .withAttributeDefinitions(new AttributeDefinition("JobTitle", ScalarAttributeType.S), 
                                                new AttributeDefinition("JobDescription", ScalarAttributeType.S))
                    .withProvisionedThroughput(new ProvisionedThroughput(new Long(1), new Long(1))));
            System.out.println("Status : " +  resultFluent.getSdkHttpMetadata().getHttpStatusCode());
            System.out.println("Table Name : " +  resultFluent.getTableDescription().getTableName());            
        } catch (AmazonServiceException e) {

References :-

  1. UpdateTableRequest Java Docs
  2. UpdateTableResult Java Docs

That’s all for how to update a table in dynamoDB using java language. If you liked it, please share your thoughts in comments section and share it with others too.