How to insert an item in dynamoDB using Java – DynamoDB PutItem

3 weeks ago Lalit Bhagtani 0

In this tutorial, we will learn about how to insert an item in dynamoDB (PutItem) 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. The primary key is the only required attribute for an item and uniquely identifies each item. DynamoDB is schema-less, which means that each item in a table can have any number of attributes and the types of attributes can vary from item to item.

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 insert an item in JOB table. Schema of this table is as follows:-

  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).
  3. CompanyName :- It represents name of the company by which job is posted. It is of String datatype (ScalarAttributeType S).
  4. JobTitle :- As the name suggest, it represents tile of the job. It is of String datatype (ScalarAttributeType S).
  5. JobDescription :- It represents details job description. It is of String datatype (ScalarAttributeType S).

dynamodb putitem

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")))


The putItem request inserts a new item in the dynamoDB table but when an item with the same primary key is already present, it overwrites that item. Steps to create and send PutItemRequest to dynamoDB are as follows:-

  1. Instantiate PutItemRequest object, this object will send the client request to dynamoDB.
  2. Set name of the table where items will be inserted. In our case JOB.
  3. Set return consumed capacity as TOTAL, this returns aggregate consumed capacity.
  4. Set return values as ALL_OLD, this returns old item values in case of item is already present.
  5. Create a map of attribute names and their values. It must include primary key attributes.
  6. Invoke the putItem method on AmazonDynamoDB object by passing PutItemRequest object as an argument.


import java.util.HashMap;
import java.util.Map;

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

public class PutItem {
	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 PutItemRequest */
		PutItemRequest request = new PutItemRequest();
		/* Setting Table Name */
		/* Setting Consumed Capacity */
		/* To get old value of item's attributes before it is overwritten */
		/* Create a Map of attributes */
		Map<String, AttributeValue> map = new HashMap<>();
		map.put("CountryId", new AttributeValue("18"));		
		map.put("JobId", (new AttributeValue()).withN("1"));
		map.put("CompanyName", new AttributeValue("Amazon"));
		map.put("JobTitle", new AttributeValue("Software Engineer"));
		try {
        	/* Send Put Item Request */
            PutItemResult result = dynamoDB.putItem(request);
            System.out.println("Status : " + result.getSdkHttpMetadata().getHttpStatusCode());
            System.out.println("Consumed Capacity : " + result.getConsumedCapacity().getCapacityUnits());
            /* Printing Old Attributes Name and Values */
            if(result.getAttributes() != null) {
					.forEach( e -> System.out.println(e.getKey() + " " + e.getValue()));  
        } catch (AmazonServiceException e) {


References :-

  1. PutItemRequest Java Docs
  2. PutItemResult Java Docs

That’s all for how to insert an item in dynamoDB (PutItem) using java language. If you liked it, please share your thoughts in comments section and share it with others too.