- AWS Code Sample demonstrates how to create a DynamoDB table with a composite key.

/* Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package aws.example.dynamodb; import com.amazonaws.AmazonServiceException; import; import; import; import; import; import; import; import; import; /** * Create a DynamoDB table. * * Takes the name of the table to create. The table will contain a * composite key. * * This code expects that you have AWS credentials set up per: * */ public class CreateTableCompositeKey { public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateTable <table>\n\n" + "Where:\n" + " table - the table to create.\n\n" + "Example:\n" + " CreateTable GreetingsTable\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } /* Read the name from command args */ String table_name = args[0]; System.out.format("Creating table %s\n with a composite primary key:\n"); System.out.format("* Language - partition key\n"); System.out.format("* Greeting - sort key\n"); CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions( new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)) .withProvisionedThroughput( new ProvisionedThroughput(new Long(10), new Long(10))) .withTableName(table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { CreateTableResult result = ddb.createTable(request); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); } }

Sample Details

Service: dynamodb

Last tested: 2018-01-15

Author: soo-aws

Type: full-example