Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a list of strings with duplicates in DynamoDB

Using the AWS SDK for .net I'm trying to save a list of non-unique strings. This list isn't the primary hash or anything, I just want to be able to reference them by their order and include duplicates.

The model

[DynamoDBTable("RMS.Accounts")]
public class Account
{
    [DynamoDBHashKey]
    public int Id { get; set; } 
    public string SomeValue { get; set; }
    public List<string> MoreValues{ get; set; }

    public Account()
    {
        // set default value
        MoreValues= new List<string>();
    }
}

The controller

Account account = new Account();
account.SomeValue = "testvalue";
account.MoreValues.Add("value1");
account.MoreValues.Add("value1");

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
context.Save(account, new DynamoDBOperationConfig() { SkipVersionCheck = true });

When I run the above code I get:

"An exception of type 'Amazon.DynamoDBv2.AmazonDynamoDBException' occurred in Apfm.RMS.Common.dll but was not handled in user code

Additional information: One or more parameter values were invalid: Input collection [value1, value1] contains duplicates.

I've read everything I can get my hands on but can't figure out a way around this. Maybe I need to use some other kind of collection so Dynamo knows the items don't need to be unique.

like image 319
caltrop Avatar asked Oct 22 '25 13:10

caltrop


1 Answers

Take a look at this blog post that details how the .NET SDK converts .NET types such as List<string> or HashSet<string> into DynamoDB data types.

In short, the SDK defaults to V1 conversion schema, where List<string> is stored as DynamoDB SS type, which does not allow duplicate items. You want to switch to using the V2 conversion schema. This can be done in multiple ways:

1.Set the conversion for the application:

<configSections> <section name="aws" type="Amazon.AWSSection, AWSSDK"/> </configSections> <aws> <dynamoDB conversionSchema="V2" /> </aws>

2.Set the conversion on the context:

var context = new DynamoDBContext(client, new DynamoDBContextConfig { Conversion = DynamoDBEntryConversion.V2 });

3.Set the conversion on the operation:

context.Save(account, new DynamoDBOperationConfig { Conversion = DynamoDBEntryConversion.V2 });

The approaches are listed as progressively granular. Switching conversion schemas will change how some types are stored (the blog post goes into a bit of detail on this), so take this into consideration.

like image 184
Pavel Safronov Avatar answered Oct 25 '25 21:10

Pavel Safronov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!