

# Running the code examples in this Developer Guide
<a name="CodeSamples"></a>

The AWS SDKs provide broad support for Amazon DynamoDB in the following languages:
+ [ Java](https://aws.amazon.com/sdk-for-java)
+ [JavaScript in the browser](https://aws.amazon.com/sdk-for-browser)
+ [.NET](https://aws.amazon.com/sdk-for-net)
+ [Node.js](https://aws.amazon.com/sdk-for-node-js)
+ [PHP](https://aws.amazon.com/sdk-for-php)
+ [Python](https://aws.amazon.com/sdk-for-python)
+ [Ruby](https://aws.amazon.com/sdk-for-ruby)
+ [C\$1\$1](https://aws.amazon.com/sdk-for-cpp)
+ [Go](https://aws.amazon.com/sdk-for-go)
+ [Android](https://aws.amazon.com/mobile/sdk/)
+ [iOS](https://aws.amazon.com/mobile/sdk/)

The code examples in this developer guide provide more in-depth coverage of DynamoDB operations, using the following programming languages:
+ [Java code examples](CodeSamples.Java.md)
+ [.NET code examples](CodeSamples.DotNet.md)

Before you can begin with this exercise, you need to create an AWS account, get your access key and secret key, and set up the AWS Command Line Interface (AWS CLI) on your computer. For more information, see [Setting up DynamoDB (web service)](SettingUp.DynamoWebService.md).

**Note**  
If you are using the downloadable version of DynamoDB, you need to use the AWS CLI to create the tables and sample data. You also need to specify the `--endpoint-url` parameter with each AWS CLI command. For more information, see [Setting the local endpoint](DynamoDBLocal.UsageNotes.md#DynamoDBLocal.Endpoint).

# Creating tables and loading data for code examples in DynamoDB
<a name="SampleData"></a>

See below for the basics on creating tables in DynamoDB, loading in a sample dataset, querying the data, and updating the data.
+ [Step 1: Create a table in DynamoDB](getting-started-step-1.md)
+ [Step 2: Write data to a DynamoDB table](getting-started-step-2.md)
+ [Step 3: Read data from a DynamoDB table](getting-started-step-3.md)
+ [Step 4: Update data in a DynamoDB table](getting-started-step-4.md)

# Java code examples
<a name="CodeSamples.Java"></a>

**Topics**
+ [

## Java: Setting your AWS credentials
](#CodeSamples.Java.Credentials)
+ [

## Java: Setting the AWS Region and endpoint
](#CodeSamples.Java.RegionAndEndpoint)

This Developer Guide contains Java code snippets and ready-to-run programs. You can find these code examples in the following sections:
+ [Working with items and attributes in DynamoDB](WorkingWithItems.md)
+ [Working with tables and data in DynamoDB](WorkingWithTables.md)
+ [Querying tables in DynamoDB](Query.md)
+ [Scanning tables in DynamoDB](Scan.md)
+ [Improving data access with secondary indexes in DynamoDB](SecondaryIndexes.md)
+ [Java 1.x: DynamoDBMapper](DynamoDBMapper.md)
+ [Change data capture for DynamoDB Streams](Streams.md)

You can get started quickly by using Eclipse with the [AWS Toolkit for Eclipse](https://aws.amazon.com/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 examples (using Eclipse)**

1. Download and install the [Eclipse](http://www.eclipse.org) IDE.

1. Download and install the [AWS Toolkit for Eclipse](https://aws.amazon.com/eclipse/).

1. Start Eclipse, and on the **Eclipse** menu, choose **File**, **New**, and then **Other**.

1. In **Select a wizard**, choose **AWS**, choose **AWS Java Project**, and then choose **Next**.

1. In **Create an AWS Java**, do the following:

   1. In **Project name**, enter a name for your project.

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

      If this is your first time using the [AWS Toolkit for Eclipse](https://aws.amazon.com/eclipse/), choose **Configure AWS Accounts** to set up your AWS credentials.

1. Choose **Finish** to create the project.

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

1. 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.

1. Copy the code example from the documentation page into the Eclipse editor.

1. 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 [AWS SDK for Java](https://aws.amazon.com/sdk-for-java).

**Note**  
The code examples in this 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
<a name="CodeSamples.Java.Credentials"></a>

The SDK for Java requires that you provide AWS credentials to your application at runtime. The code examples in this guide assume that you are using an AWS credentials file, as described in [Set up your AWS credentials](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/set-up-creds.html) 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.

```
[default]
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
<a name="CodeSamples.Java.RegionAndEndpoint"></a>

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 software.amazon.dynamodb.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 [AWS regions and endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region) 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.

### AWS SDK V1
<a name="CodeSamples.Java.RegionAndEndpoint.V1"></a>

```
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
.build();
```

### AWS SDK V2
<a name="CodeSamples.Java.RegionAndEndpoint.V2"></a>

```
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();
```

# .NET code examples
<a name="CodeSamples.DotNet"></a>

**Topics**
+ [

## .NET: Setting your AWS credentials
](#CodeSamples.DotNet.Credentials)
+ [

## .NET: Setting the AWS Region and endpoint
](#CodeSamples.DotNet.RegionAndEndpoint)

This guide contains .NET code snippets and ready-to-run programs. You can find these code examples in the following sections:
+ [Working with items and attributes in DynamoDB](WorkingWithItems.md)
+ [Working with tables and data in DynamoDB](WorkingWithTables.md)
+ [Querying tables in DynamoDB](Query.md)
+ [Scanning tables in DynamoDB](Scan.md)
+ [Improving data access with secondary indexes in DynamoDB](SecondaryIndexes.md)
+ [Working with the .NET document model in DynamoDB](DotNetSDKMidLevel.md)
+ [Working with the .NET object persistence model and DynamoDB](DotNetSDKHighLevel.md)
+ [Change data capture for DynamoDB Streams](Streams.md)

You can get started quickly by using the AWS SDK for .NET with the Toolkit for Visual Studio.

**To run the .NET code examples (using Visual Studio)**

1. Download and install [Microsoft Visual Studio](https://www.visualstudio.com).

1. (Optional) Download and install the [Toolkit for Visual Studio](https://aws.amazon.com/visualstudio/).

1. Set up your AWS credentials. Configure a credentials profile in your shared AWS credentials file (`~/.aws/credentials`). For more information, see [Configure AWS credentials](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html) in the *AWS SDK for .NET Developer Guide*.

1. Start Visual Studio. Choose **File**, **New**, **Project**.

1. Search for **Console App**, select the C\$1 template that targets .NET, and then choose **Next**. Configure your project name and location, and then choose **Create**.

1. Add the AWS SDK for DynamoDB NuGet package to your project:

   1. In Solution Explorer, open the context (right-click) menu for your project, and then choose **Manage NuGet Packages**.

   1. In NuGet Package Manager, choose **Browse**.

   1. In the search box, enter **AWSSDK.DynamoDBv2**, and wait for the search to complete.

   1. Choose **AWSSDK.DynamoDBv2**, and then choose **Install**.

1. In your Visual Studio project, open `Program.cs`. Replace the contents with the code example from the documentation page that you want to run.

1. To run the code, choose **Start** in the Visual Studio toolbar.

The SDK for .NET 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 [AWS SDK for .NET](https://aws.amazon.com/sdk-for-net).

**Note**  
The code examples in this guide are intended for use with the latest version of the AWS SDK for .NET.

## .NET: Setting your AWS credentials
<a name="CodeSamples.DotNet.Credentials"></a>

The SDK for .NET requires that you provide AWS credentials to your application at runtime. The code examples in this guide assume that you are using the SDK Store to manage your AWS credentials file, as described in [Using the SDK store](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#sdk-store) in the *AWS SDK for .NET Developer Guide*.

The Toolkit for Visual Studio supports multiple sets of credentials from any number of accounts. Each set is referred to as a *profile*. Visual Studio adds entries to the project's `App.config` file so that your application can find the AWS credentials at runtime.

The following example shows the default `App.config` file that is generated when you create a new project using Toolkit for Visual Studio.

```
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
    <add key="AWSProfileName" value="default"/>
    <add key="AWSRegion" value="us-west-2" />
 </appSettings>
</configuration>
```

At runtime, the program uses the `default` set of AWS credentials, as specified by the `AWSProfileName` entry. The AWS credentials themselves are kept in the SDK Store in encrypted form. The Toolkit for Visual Studio provides a graphical user interface for managing your credentials, all from within Visual Studio. For more information, see [Specifying credentials](https://docs.aws.amazon.com/AWSToolkitVS/latest/UserGuide/tkv_setup.html#creds) in the *AWS Toolkit for Visual Studio User Guide*.

**Note**  
By default, the code examples access DynamoDB in the US West (Oregon) Region. You can change the Region by modifying the `AWSRegion` entry in the App.config file. You can set `AWSRegion` to any Region where DynamoDB is available. For a complete list, see [AWS regions and endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region) in the *Amazon Web Services General Reference*.

## .NET: Setting the AWS Region and endpoint
<a name="CodeSamples.DotNet.RegionAndEndpoint"></a>

By default, the code examples access DynamoDB in the US West (Oregon) Region. You can change the Region by modifying the `AWSRegion` entry in the `App.config` file. Or, you can change the Region by modifying the `AmazonDynamoDBClient` properties.

The following code example instantiates a new `AmazonDynamoDBClient`. The client is modified so that the code runs against DynamoDB in a different Region.

```
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
// This client will access the US East 1 region.
clientConfig.RegionEndpoint = RegionEndpoint.USEast1;
AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig);
```

For a complete list of Regions, see [AWS regions and endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region) 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.

```
AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
// Set the endpoint URL
clientConfig.ServiceURL = "http://localhost:8000";
AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig);
```