How to create a DynamoDB table using Java SDK

0 votes
How to create a table in DynamoDB using Java? Can someone help me with the steps and code?
Feb 23, 2019 in AWS by Shuvodip
2,471 views

1 answer to this question.

0 votes

The code to create a table in DynamoDB can be written as follows:-

import java.util.Arrays;


import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
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.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;


public class DynamoDb {
private static final AWSCredentials AWS_CREDENTIALS;
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "Access Key",
                "Secret Key"
        );
    }
    public static void main(String[] args) throws Exception {
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
            .build();
        DynamoDB dynamoDB = new DynamoDB(client);
        String tableName = "Employee";
        try {
            System.out.println("Creating Table");
            Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("EmpID", KeyType.HASH), // Partition
                    new KeySchemaElement("EmployeeName", KeyType.RANGE)), // Sort key
                Arrays.asList(new AttributeDefinition("EmpID", ScalarAttributeType.N),
                    new AttributeDefinition("EmployeeName", ScalarAttributeType.S)),
                new ProvisionedThroughput(10L, 10L));
            table.waitForActive();
            System.out.println("Success.Table status: " + table.getDescription().getTableStatus());
        }
        catch (Exception e) {
            System.err.println("Unable to create table: ");
            System.err.println(e.getMessage());
        }
    }
}

Hope this will suffice your need.

answered Feb 23, 2019 by Priyaj
• 58,100 points

Related Questions In AWS

0 votes
1 answer

How to load data into a table in DynamoDB using Java?

You can create a .json file with the ...READ MORE

answered Feb 26, 2019 in AWS by Priyaj
• 58,100 points
6,866 views
+1 vote
1 answer
0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

The Java code for creating an AWS ...READ MORE

answered Feb 27, 2019 in AWS by Priyaj
• 58,100 points
1,489 views
0 votes
1 answer

How to create a DynamoDB table in AWS?

Creating a DynamoDB table is made very ...READ MORE

answered Feb 22, 2019 in AWS by Priyaj
• 58,100 points
1,166 views
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer

How to create a VPC using Java SDK?

You can create a VPC using the ...READ MORE

answered Feb 21, 2019 in AWS by Priyaj
• 58,100 points
2,669 views
0 votes
1 answer

How to create a DynamoDB table using Python?

The code that you can use to ...READ MORE

answered Feb 22, 2019 in AWS by Priyaj
• 58,100 points
7,706 views
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