Menu
Amazon DynamoDB
Getting Started Guide (API Version 2012-08-10)

Step 1: Create a Table

In this step, you create a table named Movies. The primary key for the table is composed of the following attributes:

  • year – The partition key. The ScalarAttributeType is N for number.

  • title – The sort key. The ScalarAttributeType is S for string.

  1. Copy the following program into your Java development environment.

    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    // Licensed under the Apache License, Version 2.0.
    
    package com.amazonaws.codesamples.gsg;
    
    import java.util.Arrays;
    
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
    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 MoviesCreateTable {
    
        public static void main(String[] args) throws Exception {
    
            AmazonDynamoDBClient client = new AmazonDynamoDBClient()
                .withEndpoint("http://localhost:8000");
    
            DynamoDB dynamoDB = new DynamoDB(client);
    
            String tableName = "Movies";
    
            try {
                System.out.println("Attempting to create table; please wait...");
                Table table = dynamoDB.createTable(tableName,
                    Arrays.asList(
                        new KeySchemaElement("year", KeyType.HASH),  //Partition key
                        new KeySchemaElement("title", KeyType.RANGE)), //Sort key
                        Arrays.asList(
                            new AttributeDefinition("year", ScalarAttributeType.N),
                            new AttributeDefinition("title", 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());
            }
    
        }
    }
    

    Note

    • You set the endpoint (client.setEndpoint) to indicate that you are creating the table in DynamoDB on your computer.

    • In the createTable call, you specify table name, primary key attributes, and its data types.

    • The ProvisionedThroughput parameter is required; however, the downloadable version of DynamoDB ignores it. (Provisioned throughput is beyond the scope of this exercise.)

  2. Compile and run the program.

To learn more about managing tables, see Working with Tables in the Amazon DynamoDB Developer Guide.

Next Step

Step 2: Load Sample Data