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

查看:189
本文介绍了使用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>();
    }

我在启动文件的Configure Services方法中注入了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

它说您可以使用Query / Scan方法从数据库中检索项目,但是不幸的是,.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.

推荐答案

QueryAsync ScanAsync 只是<映射到DynamoDB 异步样式方法: //docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html rel = noreferrer>查询和扫描操作。

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,而没有任何客户端详细信息。您将学习分页,过滤器表达式等。然后,如果您不熟悉异步/等待,请继续阅读。最后将它们放在一起,您应该能够在自己的应用程序中使用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天全站免登陆