Java code examples
This Developer Guide contains Java code snippets and ready-to-run programs. You can find these code examples in the following sections:
You can get started quickly by using Eclipse with the Amazon Toolkit for Eclipse
To run the Java code examples (using Eclipse)
-
Download and install the Eclipse
IDE. -
Download and install the Amazon Toolkit for Eclipse
. -
Start Eclipse, and on the Eclipse menu, choose File, New, and then Other.
-
In Select a wizard, choose Amazon, choose Amazon Java Project, and then choose Next.
-
In Create an Amazon Java, do the following:
-
In Project name, enter a name for your project.
-
In Select Account, choose your credentials profile from the list.
If this is your first time using the Amazon Toolkit for Eclipse
, choose Configure Amazon Accounts to set up your Amazon credentials.
-
-
Choose Finish to create the project.
-
From the Eclipse menu, choose File, New, and then Class.
-
In Java Class, enter a name for your class in Name (use the same name as the code example that you want to run), and then choose Finish to create the class.
-
Copy the code example from the documentation page into the Eclipse editor.
-
To run the code, choose Run on 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 Amazon SDK for Java
Note
The code examples in this guide are intended for use with the latest version of the Amazon SDK for Java.
If you are using the Amazon Toolkit for Eclipse, you can configure automatic updates for the SDK for Java. To do this in Eclipse, go to Preferences and choose Amazon Toolkit, Amazon SDK for Java, Download new SDKs automatically.
Java: Setting your Amazon credentials
The SDK for Java requires that you provide Amazon credentials to your application at runtime. The code examples in this guide assume that you are using an Amazon credentials file, as described in Set up your Amazon credentials in the Amazon SDK for Java Developer Guide.
The following is an example of an Amazon credentials file named
~/.aws/credentials
, where the tilde character (~
)
represents your home directory.
[default] aws_access_key_id =
Amazon access key ID goes here
aws_secret_access_key =Secret key goes here
Java: Setting the Amazon Region and endpoint
By default, the code examples access DynamoDB in the US West (Oregon) Region. You
can change the Region by modifying the AmazonDynamoDB
properties.
The following code example instantiates a new AmazonDynamoDB
.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.regions.Regions; ... // This client will default to US West (Oregon) AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(Regions.US_WEST_2) .build();
You can use the withRegion
method to run your code against DynamoDB in
any Region where it is available. For a complete list, see Amazon regions and endpoints in the
Amazon Web Services General Reference.
If you want to run the code examples using DynamoDB locally on your computer, set the endpoint as follows.
Amazon SDK V1
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build();
Amazon SDK V2
DynamoDbClient client = DynamoDbClient.builder() .endpointOverride(URI.create("http://localhost:8000")) // The region is meaningless for local DynamoDb but required for client builder validation .region(Region.US_EAST_1) .credentialsProvider(StaticCredentialsProvider.create( AwsBasicCredentials.create("dummy-key", "dummy-secret"))) .build();