Skip to content

Commit

Permalink
.NET (v3): Add Hello DynamoDB example (#5037)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlhagerm authored Jul 7, 2023
1 parent d51ef89 commit dcf73e6
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 134 deletions.
8 changes: 8 additions & 0 deletions .doc_gen/metadata/dynamodb_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ dynamodb_Hello:
synopsis: get started using &DDB;.
category: Hello
languages:
.NET:
versions:
- sdk_version: 3
github: dotnetv3/dynamodb
excerpts:
- description:
snippet_tags:
- DynamoDB.dotnetv3.HelloDynamoDB
JavaScript:
versions:
- sdk_version: 3
Expand Down
44 changes: 28 additions & 16 deletions dotnetv3/dynamodb/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--Generated by WRITEME on 2023-04-25 16:05:47.538710 (UTC)-->
<!--Generated by WRITEME on 2023-07-06 14:23:51.103503 (UTC)-->
# DynamoDB code examples for the SDK for .NET

## Overview
Expand Down Expand Up @@ -30,24 +30,29 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3
<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->


### Get started

* [Hello DynamoDB](scenarios/DynamoDB_Basics/DynamoDB_Actions/HelloDynamoDB.cs#L4) (`ListTables`)

### Single actions

Code excerpts that show you how to call individual service functions.

* [Create a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L8) (`CreateTable`)
* [Delete a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L378) (`DeleteTable`)
* [Delete an item from a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L251) (`DeleteItem`)
* [Create a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L14) (`CreateTable`)
* [Delete a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L389) (`DeleteTable`)
* [Delete an item from a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L262) (`DeleteItem`)
* [Get a batch of items](low-level-api/LowLevelBatchGet/LowLevelBatchGet.cs#L4) (`BatchGetItem`)
* [Get an item from a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L165) (`GetItem`)
* [Get an item from a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L171) (`GetItem`)
* [Get information about a table](low-level-api/LowLevelTableExample/LowLevelTableExample.cs#L126) (`DescribeTable`)
* [List tables](low-level-api/LowLevelTableExample/LowLevelTableExample.cs#L102) (`ListTables`)
* [Put an item in a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L83) (`PutItem`)
* [Query a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L286) (`Query`)
* [Put an item in a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L89) (`PutItem`)
* [Query a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L297) (`Query`)
* [Run a PartiQL statement](scenarios/PartiQL_Basics_Scenario/PartiQL_Basics_Scenario/PartiQLMethods.cs#L163) (`ExecuteStatement`)
* [Run batches of PartiQL statements](scenarios/PartiQL_Batch_Scenario/PartiQL_Batch_Scenario/PartiQLBatchMethods.cs#L107) (`BatchExecuteStatement`)
* [Scan a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L339) (`Scan`)
* [Update an item in a table](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L113) (`UpdateItem`)
* [Write a batch of items](scenarios/DynamoDB_Basics/DynamoDB_Basics_Scenario/DynamoDbMethods.cs#L196) (`BatchWriteItem`)
* [Run batches of PartiQL statements](scenarios/PartiQL_Batch_Scenario/PartiQL_Batch_Scenario/PartiQLBatchMethods.cs#L10) (`BatchExecuteStatement`)
* [Scan a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L350) (`Scan`)
* [Update an item in a table](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L119) (`UpdateItem`)
* [Write a batch of items](scenarios/DynamoDB_Basics/DynamoDB_Actions/DynamoDbMethods.cs#L202) (`BatchWriteItem`)

### Scenarios

Expand All @@ -64,27 +69,30 @@ functions within the same service.

Sample applications that work across multiple AWS services.

* [Create a web application to track DynamoDB data](../cross_service/DynamoDbItemTracker)
* [Create a serverless application to manage photos](../cross-service/PhotoAssetManager)
* [Create a web application to track DynamoDB data](../cross-service/DynamoDbItemTracker)

## Run the examples

### Instructions


For general instructions to run the examples, see the [README](../README.md#building-and-running-the-code-examples) in the `dotnetv3` folder.
For general instructions to run the examples, see the
[README](../README.md#building-and-running-the-code-examples) in the `dotnetv3` folder.

Some projects might include a settings.json file. Before compiling the project,
you can change these values to match your own account and resources. Alternatively, add a settings.local.json file with
your local settings, which will be loaded automatically when the application runs.
you can change these values to match your own account and resources. Alternatively,
add a settings.local.json file with your local settings, which will be loaded automatically
when the application runs.

After the example compiles, you can run it from the command line. To do so, navigate to
the folder that contains the .csproj file and run the following command:

```
dotnet run
```
Alternatively, you can run the example from within your IDE.

Alternatively, you can run the example from within your IDE.

<!--custom.instructions.start-->
Before you compile the .NET application, you can optionally set configuration values
Expand All @@ -98,6 +106,10 @@ dotnet run
Alternatively, you can run the example from within your IDE.
<!--custom.instructions.end-->

#### Hello DynamoDB

This example shows you how to get started using DynamoDB.



#### Get started with tables, items, and queries
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.105.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace DynamoDB_Basics_Scenario
using System.Text.Json;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.DynamoDBv2.Model;

namespace DynamoDB_Actions
{
public class DynamoDbMethods
{
Expand Down Expand Up @@ -210,7 +216,12 @@ public static List<Movie> ImportMovies(string movieFileName)

using var sr = new StreamReader(movieFileName);
string json = sr.ReadToEnd();
var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json);
var allMovies = JsonSerializer.Deserialize<List<Movie>>(
json,
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});

// Now return the first 250 entries.
return allMovies.GetRange(0, 250);
Expand All @@ -237,11 +248,11 @@ public static async Task<long> BatchWriteItemsAsync(

var context = new DynamoDBContext(client);

var bookBatch = context.CreateBatchWrite<Movie>();
bookBatch.AddPutItems(movies);
var movieBatch = context.CreateBatchWrite<Movie>();
movieBatch.AddPutItems(movies);

Console.WriteLine("Adding imported movies to the table.");
await bookBatch.ExecuteAsync();
await movieBatch.ExecuteAsync();

return movies.Count;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[DynamoDB.dotnetv3.HelloDynamoDB]

using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;

namespace DynamoDB_Actions;

public static class HelloDynamoDB
{
static async Task Main(string[] args)
{
var dynamoDbClient = new AmazonDynamoDBClient();

Console.WriteLine($"Hello Amazon Dynamo DB! Following are some of your tables:");
Console.WriteLine();

// You can use await and any of the async methods to get a response.
// Let's get the first five tables.
var response = await dynamoDbClient.ListTablesAsync(
new ListTablesRequest()
{
Limit = 5
});

foreach (var table in response.TableNames)
{
Console.WriteLine($"\tTable: {table}");
Console.WriteLine();
}
}
}

// snippet-end:[DynamoDB.dotnetv3.HelloDynamoDB]
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace DynamoDB_Basics_Scenario
{
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DataModel;

namespace DynamoDB_Actions
{
[DynamoDBTable("movie_table")]
public class Movie
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace DynamoDB_Basics_Scenario
{
using System;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DataModel;

namespace DynamoDB_Actions
{
public class MovieInfo
{
[DynamoDBProperty("directors")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32328.378
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DynamoDB_Basics_Scenario", "DynamoDB_Basics_Scenario\DynamoDB_Basics_Scenario.csproj", "{C5E431E0-4ED7-4C38-A655-BFDEFD179C74}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DynamoDB_Basics_ScenarioTests", "DynamoDB_Basics_ScenarioTests\DynamoDB_Basics_ScenarioTests.csproj", "{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{6626DB2A-9F51-4984-9E97-74011A91A0F7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scenarios", "Scenarios", "{C1ED3556-F8C6-4E93-B650-B013AB277F75}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F2D31BC8-AB1A-463F-AB53-FDB670084778}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamoDB_Actions", "DynamoDB_Actions\DynamoDB_Actions.csproj", "{B4C172AE-AE05-499A-B16A-72C01A7F24C7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C5E431E0-4ED7-4C38-A655-BFDEFD179C74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5E431E0-4ED7-4C38-A655-BFDEFD179C74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5E431E0-4ED7-4C38-A655-BFDEFD179C74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5E431E0-4ED7-4C38-A655-BFDEFD179C74}.Release|Any CPU.Build.0 = Release|Any CPU
{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F}.Release|Any CPU.Build.0 = Release|Any CPU
{B4C172AE-AE05-499A-B16A-72C01A7F24C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4C172AE-AE05-499A-B16A-72C01A7F24C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4C172AE-AE05-499A-B16A-72C01A7F24C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4C172AE-AE05-499A-B16A-72C01A7F24C7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C5E431E0-4ED7-4C38-A655-BFDEFD179C74} = {C1ED3556-F8C6-4E93-B650-B013AB277F75}
{0B77DDFD-A2DF-41BD-8D61-3B93167EA34F} = {F2D31BC8-AB1A-463F-AB53-FDB670084778}
{B4C172AE-AE05-499A-B16A-72C01A7F24C7} = {6626DB2A-9F51-4984-9E97-74011A91A0F7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {39F603C9-C788-4E8C-8D3E-937478525CF9}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="DynamoDB_Basics.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[DynamoDB.dotnetv3.DynamoDB_Basics_Scenario]
// This example application performs the following basic Amazon DynamoDB
Expand All @@ -20,6 +19,9 @@
// Before you run this example, download 'movies.json' from
// https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/resources/sample_files,
// and put it in the same folder as the example.

using DynamoDB_Actions;

namespace DynamoDB_Basics_Scenario
{
public class DynamoDB_Basics
Expand All @@ -33,7 +35,7 @@ public static async Task Main()

var tableName = "movie_table";

// relative path to moviedata.json in the local repository.
// Relative path to moviedata.json in the local repository.
var movieFileName = @"..\..\..\..\..\..\..\..\resources\sample_files\movies.json";

DisplayInstructions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.9" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.3.13" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="AWSSDK.Core" Version="3.7.107.11" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.105.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DynamoDB_Actions\DynamoDB_Actions.csproj" />
</ItemGroup>
</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
<IsPackable>false</IsPackable>

<ImplicitUsings>enable</ImplicitUsings>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.100.17" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.100.17" />
<PackageReference Include="AWSSDK.Core" Version="3.7.107.11" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.105.2" />
<PackageReference Include="Dangl.Xunit.Extensions.Ordering" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using DynamoDB_Actions;

namespace DynamoDB_Basics_Scenario.Tests
{

[TestCaseOrderer("OrchestrationService.Project.Orderers.PriorityOrderer", "OrchestrationService.Project")]
public class DynamoDbMethodsTests
{
readonly AmazonDynamoDBClient client = new();
Expand Down
Loading

0 comments on commit dcf73e6

Please sign in to comment.