How to load data into a table in DynamoDB using Java

0 votes
Can someone help me with loading data into a table in DynamoDb using Java?
Feb 23, 2019 in AWS by Shuvodip
6,995 views

1 answer to this question.

0 votes

You can create a .json file with the data you want to enter. 

This is the content of my json file.

[ { "PhoneNo" : ... , "EmpName" : ... }]

Here is the code I have used to insert data in my database.

import java.io.File;
import java.util.Iterator;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
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 InsertDataDynamoDb 
{

private static final AWSCredentials AWS_CREDENTIALS;
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "access key",
                "secretkey"
        );
    }

public static void main(String[] args) throws Exception {
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);
        Table table = dynamoDB.getTable("Employee");
        JsonParser parser = new JsonFactory().createParser(new File("employeedata.json"));
        JsonNode rootNode = new ObjectMapper().readTree(parser);
        Iterator<JsonNode> iter = rootNode.iterator();
        ObjectNode currentNode;
        while (iter.hasNext()) {
            currentNode = (ObjectNode) iter.next();
            int phoneno = currentNode.path("PhoneNo").asInt();
            String empname = currentNode.path("EmpName").asText();
            try {
                table.putItem(new Item().withPrimaryKey("PhoneNo", phoneno, "EmpName", empname).withJSON("info",
                    currentNode.path("info").toString()));
                System.out.println("PutItem succeeded: " + phoneno + " " + empname);
            }
            catch (Exception e) {
                System.err.println("Unable to add movie: " + phoneno + " " + empname);
                System.err.println(e.getMessage());
                break;
            }
        }
        parser.close();
    }
}

Hope this helps.

secretkey

answered Feb 26, 2019 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer

How to create a DynamoDB table using Java SDK?

The code to create a table in ...READ MORE

answered Feb 23, 2019 in AWS by Priyaj
• 58,020 points
2,618 views
+1 vote
1 answer
+1 vote
1 answer

How to load CSV data to RDS Aurora table using lambda?

Launch an Amazon RDS MySQL database engine ...READ MORE

answered Jul 26, 2018 in AWS by Priyaj
• 58,020 points
4,698 views
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP