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.