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 22 in AWS by Shuvodip
79 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 22 by Priyaj
• 56,940 points

Related Questions In AWS

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

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

answered Feb 27 in AWS by Priyaj
• 56,940 points
165 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 in AWS by Priyaj
• 56,940 points
89 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 in AWS by Priyaj
• 56,940 points
204 views
0 votes
1 answer