DynamoDB - Create Table

DynamoDB - Create Table

Creating a table usually consists of generating the table, giving it a name, setting its primary key attributes, and setting the attribute's data types.

Use the GUI Console, Java, or another option to complete these tasks.

Create table using GUI console

Create a table by opening the console at https://console.aws.amazon.com/dynamodb . Then select the "Create Table" option.

GUI Console

In our example, a table is created filled with product information, with products with unique attributes identified by an identification number (number attribute). On the Create Table screen, enter a table name in the table name field; enter the primary key (ID) in the partition key field; and enter "Number" for the data type.

Create table

After entering all the information, select Create .

Create table using Java

Use Java to create the same table. Its primary key consists of the following two attributes:

  • ID - Use the section key and ScalarAttributeType N , which means a number.

  • Nomenclature - Use sort key and ScalarAttributeType S , which means string.

ID - Use the section key and ScalarAttributeType N , which means a number.

Nomenclature - Use sort key and ScalarAttributeType S , which means string.

Java uses the createTable method to generate a table; and the call specifies the table name, primary key attributes, and attribute data types.

You can view the following example −

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 ProductsCreateTable { public static void main ( String [] args ) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient () . withEndpoint ( "http://localhost:8000" );     
         
         
           
      
      DynamoDB dynamoDB = new DynamoDB ( client ); String tableName = "Products" ; try { System . out . println ( "Creating the table, wait..." ); Table table = dynamoDB . createTable ( tableName , Arrays . asList ( new KeySchemaElement ( "ID" , KeyType . HASH ), // the partition key   
         
        
          
          
             
                  
                                                         // the sort key new KeySchemaElement ( "Nomenclature" , KeyType . RANGE ) ), Arrays . asList ( new AttributeDefinition ( "ID" , ScalarAttributeType . N ), new AttributeDefinition ( "Nomenclature" , ScalarAttributeType . S ) ), new ProvisionedThroughput ( 10L , 10L ) ); 
         table . waitForActive ();
                 
            
             
                  
                 
            
              
          
         System . out . println ( "Table created successfully. Status: " +  
            table . getDescription (). getTableStatus ()); 
            
      } catch ( Exception e ) { System . err . println ( "Cannot create the table: " ); System . err . println ( e.getMessage ( ) ); } } }   
          
          
       
    

In the example above, notice the endpoint: .withEndpoint .

This indicates a local install using localhost. Also note the required parameter ProvisionedThroughput , which the local installation ignores.