Access Amazon DynamoDB Locally In .NET Core Console Application

In this article, you will see how to setup Amazon DynamoDB locally and access the data from DynamoDB table in .NET Core Console application for development purpose.

Prerequisites

  1. Download and install DynamoDB locally on your computer.
  2. Ensure to start and running DynamoDB locally during development to access DynamoDB locally.

To start DynamoDB on your computer, open a command prompt window, navigate to the directory where you extracted DynamoDBLocal.jar, and enter the following command.

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
  1. Download and install NoSQL Workbench.
  2. Download and install AWS CLI.

Connect to Local Dynamo DB in NoSQL Workbench

Open NoSQL Workbench.

Choose Amazon DynamoDB and click Launch.

Access Amazon DynamoDB locally in .NET Core Console Application

Click Operation builder and then click Add connection.

Access Amazon DynamoDB locally in .NET Core Console Application

Click DynamoDB local tab, enter the Connection name and click Connect.

Access Amazon DynamoDB locally in .NET Core Console Application

Open the connection.

Access Amazon DynamoDB locally in .NET Core Console Application

Create sample table and load data

Open command prompt. Create a sample table using AWS CLI as shown below.

aws dynamodb create-table ^
    --table-name ProductCatalog ^
    --attribute-definitions AttributeName=Id,AttributeType=N ^
    --key-schema AttributeName=Id,KeyType=HASH ^
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 ^
    --tags Key=Owner,Value=blueTeam ^
    --endpoint-url http://localhost:8000

Download the sample data. Open Command prompt and navigate to the location where JSON files are available. Run the following command.

aws dynamodb batch-write-item --request-items file://ProductCatalog.json --endpoint-url http://localhost:8000

Once the data is loaded you can view the items and tables in NoSQL Workbench.

Access Amazon DynamoDB locally in .NET Core Console Application

Create sample .NET Core Console Application for testing

Create a sample .NET Core Console Application using Visual Studio.

Add AWSSDK.DynamDBv2 NuGet package.

Create a new class and name it as Book.cs. Copy the paste the following code.

using Amazon.DynamoDBv2.DataModel;
using System.Collections.Generic;

namespace AWSDynamoDBNETConsole
{
    [DynamoDBTable("ProductCatalog")]
    public class Book
    {
        [DynamoDBHashKey]
        public int Id { get; set; }

        public string Title { get; set; }
        public string ISBN { get; set; }
        public string ProductCategory { get; set; }

        [DynamoDBProperty("Authors")]
        public List<string> BookAuthors { get; set; }

        [DynamoDBIgnore]
        public string CoverPage { get; set; }
    }
}

Update Program.cs with the following code.

using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace AWSDynamoDBNETConsole {
    class Program {
        static async Task Main(string[] args) {
            try {
                AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
                clientConfig.ServiceURL = "http://localhost:8000";
                AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig);
                DynamoDBContext context = new DynamoDBContext(client);
                await FindProductsWithSpecificTitleAsync(context, "Book");
                Console.WriteLine("To continue, press Enter");
                Console.ReadLine();
            } catch (AmazonDynamoDBException e) {
                Console.WriteLine(e.Message);
            } catch (AmazonServiceException e) {
                Console.WriteLine(e.Message);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
        private static async Task FindProductsWithSpecificTitleAsync(DynamoDBContext context, string title) {
            var itemsWithSpecificTitle = await context.ScanAsync < Book > (new List < ScanCondition > () {
                new ScanCondition("Title", ScanOperator.Contains, title),
                    new ScanCondition("ProductCategory", ScanOperator.Equal, "Book")
            }).GetRemainingAsync();
            Console.WriteLine("\nFindProductsWithSpecificTitleAsync: Printing result.....");
            foreach(Book book in itemsWithSpecificTitle)
            Console.WriteLine("{0}\t{1}\t{2}", book.Id, book.Title, book.ISBN);
        }
    }
}

Build and run the application.

Items are retrieved from local DynamoDB table.

Summary

Thus, in this article, you saw how to setup Amazon DynamoDB locally and access the data from DynamoDB table in .NET Core Console application.


Similar Articles