Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving All items in a table with DynamoDB

I am currently in a web services class, and for the project I am working on, I decided to make a Web API using the .NET Core platform, with DynamoDB as the Database.

So far it has been a little tough getting Dynamo to work with .NET Core as I cannot seem to find too many articles on getting the two to work together. I am currently stuck on how to retrieve all items from a specific table.

I have been reading through the Dynamo documentation, and decided to go with the Object Persistence model.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
        services.AddAWSService<IAmazonDynamoDB>();
    }

I inject the DynamoDB context in the Configure Services method of the Startup file. Then I inject it into the controller like this

    DynamoDBContext context;
    public ValuesController(IAmazonDynamoDB context)
    {
        this.context = new DynamoDBContext(context);
    }

Again, I am in no way an expert on these two technologies, so if there is a better way to do this please let me know.

I also have a simple model that I have been using that I got from the DynamoDB documentation.

[DynamoDBTable("AnimalsInventory")]
public class Item
{
    [DynamoDBHashKey]
    public Guid Id { get; set; }
    [DynamoDBRangeKey]
    public string Type { get; set; }
    public string Name { get; set; }
}

In this article here

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBContext.QueryScan.html

it says you can just use the Query/Scan methods to retrieve items from the DB, but unfortunately those methods are not supported on the .NET Core platform. On .NET Core they are called QueryAsync and ScanAsync, and I thought maybe I could just call the method without any arguments and it would just retrieve any items in the table, but that did not work. It looks like the method specifically takes in some scan conditions, so I am not sure if I am using the wrong methods, or if there is no way to just retrieve any and all items from a table.

like image 312
wjvukasovic Avatar asked Feb 05 '18 21:02

wjvukasovic


Video Answer


1 Answers

The QueryAsync and ScanAsync are just async-style methods that map to the DynamoDB Query and Scan operations.

You should be able to scan all items in your table using something like this:

var conditions = new List<ScanCondition>();
// you can add scan conditions, or leave empty
var allDocs = await context.ScanAsync<Item>(conditions).GetRemainingAsync();

I recommend starting with the documentation for the Scan API. It will explain the actual API without any client specifics. You will learn about paging, filter expressions etc. Then, if you're not familiar with async/await, read up on that. Finally put them together and you should be able to use QueryAsync and ScanAsync in your own application.

like image 144
Mike Dinescu Avatar answered Sep 29 '22 10:09

Mike Dinescu