Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Java Code Samples

This Developer Guide contains Java code snippets and ready-to-run programs. You can find these code samples in the following sections:


The Amazon DynamoDB Getting Started Guide contains additional Java sample programs.

You can get started quickly by using Eclipse with the AWS Toolkit for Eclipse. In addition to a full-featured IDE, you also get the AWS SDK for Java with automatic updates, and preconfigured templates for building AWS applications.

To Run the Java Code Samples (using Eclipse)

  1. Download and install the Eclipse IDE.

  2. Download and install the AWS Toolkit for Eclipse.

  3. Start Eclipse and from the Eclipse menu, choose File, New, and then Other.

  4. In Select a wizard, choose AWS, choose AWS Java Project, and then chooseNext.

  5. In Create an AWS Java, do the following:

    1. In Project name, type a name for your project.

    2. In Select Account, choose your credentials profile from the list.

      If this is your first time using the AWS Toolkit for Eclipse, choose Configure AWS Accounts to set up your AWS credentials.

  6. Choose Finish to create the project.

  7. From the Eclipse menu, choose File, New, and then Class.

  8. In Java Class, type a name for your class in Name (use the same name as the code sample that you want to run), and then choose Finish to create the class.

  9. Copy the code sample from the documentation page you are reading into the Eclipse editor.

  10. To run the code, choose Run in the Eclipse menu.

The SDK for Java provides thread-safe clients for working with DynamoDB. As a best practice, your applications should create one client and reuse the client between threads.

For more information, see the AWS SDK for Java.


The code samples in this Developer Guide are intended for use with the latest version of the AWS SDK for Java.

If you are using the AWS Toolkit for Eclipse, you can configure automatic updates for the SDK for Java. To do this in Eclipse, go to Preferences and choose AWS Toolkit --> AWS SDK for Java --> Download new SDKs automatically.

Java: Setting Your AWS Credentials

The SDK for Java requires that you provide AWS credentials to your application at runtime. The code samples in this Developer Guide assume that you are using an AWS credentials file, as described in Set Up Your AWS Credentials in the AWS SDK for Java Developer Guide.

The following is an example of an AWS credentials file named ~/.aws/credentials, where the tilde character (~) represents your home directory:

aws_access_key_id = AWS access key ID goes here 
aws_secret_access_key = Secret key goes here         

Java: Setting the AWS Region and Endpoint

By default, the code samples access DynamoDB in the US West (Oregon)Region. You can change the region by modifying the AmazonDynamoDBClient properties.

The following code snippet instantiates a new AmazonDynamoDBClient. The client is then modified so that the code will run against DynamoDB in a different region.

import com.amazonaws.regions.Regions;
// This client will default to US West (Oregon)
AmazonDynamoDBClient client = new AmazonDynamoDBClient();

// Modify the client so that it accesses a different region.

You can use the withRegion method to run your code against Amazon DynamoDB in any region where it is available. For a complete list, see AWS Regions and Endpoints in the Amazon Web Services General Reference.

If you want to run the code samples using DynamoDB locally on your computer, you need to set the endpoint, as shown following:

AmazonDynamoDBClient client = new AmazonDynamoDBClient();

// Set the endpoint URL