使用 DynamoDB 检索表中的所有项目 [英] Retrieving All items in a table with DynamoDB

查看:37
本文介绍了使用 DynamoDB 检索表中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Web 服务课程,对于我正在从事的项目,我决定使用 .NET Core 平台制作 Web API,以 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.

到目前为止,让 Dynamo 与 .NET Core 一起工作有点困难,因为我似乎找不到太多关于让两者协同工作的文章.我目前被困在如何从特定表中检索所有项目.

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.

我一直在阅读 Dynamo 文档,并决定使用对象持久性模型.

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

我在启动文件的配置服务方法中注入了 DynamoDB 上下文.然后我像这样将它注入控制器

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.

我还有一个我一直在使用的简单模型,它是从 DynamoDB 文档中获取的.

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; }
}

在这篇文章中

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

它说你可以只使用查询/扫描方法从数据库中检索项目,但不幸的是,.NET Core 平台不支持这些方法.在 .NET Core 上,它们被称为 QueryAsync 和 ScanAsync,我想也许我可以不带任何参数调用该方法,它只会检索表中的任何项目,但这不起作用.看起来该方法专门接受某些扫描条件,所以我不确定我是否使用了错误的方法,或者是否无法从表中检索任何和所有项目.

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.

推荐答案

QueryAsyncScanAsync 只是 async-style 映射到 DynamoDB 的方法 查询扫描 操作.

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

我建议从 扫描 API 的文档开始.它将解释没有任何客户端细节的实际 API.您将了解分页、过滤器表达式等.然后,如果您不熟悉 async/await,请仔细阅读.最后将它们放在一起,您应该能够在您自己的应用程序中使用 QueryAsync 和 ScanAsync.

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.

这篇关于使用 DynamoDB 检索表中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆