c# - 尝试了解 DynamoDB .NET 中的查询表操作

标签 c# .net amazon-web-services amazon-dynamodb nosql

我正在使用 DynamoDB .NET 并试图理解它,但我对执行以下句子有疑问:“获取具有特定值的所有行”。例如,我有下表:

编号 |公司 |代码

其中 Id 和 Company 是主键(组合),我的查询将是“获取属于公司的所有代码及其 ID”

我读到扫描操作比查询更昂贵,如果我的表有一个由散列和范围属性组成的主键,我应该使用查询语句。

好的,所以我开始编写我的句子,我有这个:

Table table = Table.LoadTable(dynamoClient, tablename );

QueryFilter filter = new QueryFilter();
filter.AddCondition("Company", QueryOperator.Equal, company); //company is a parameter

QueryOperationConfig config = new QueryOperationConfig()
{
    Filter = filter,
    Select = SelectValues.SpecificAttributes,
    AttributesToGet = new List<string> { "Id","Code" },
    ConsistentRead = true
};

Search tableResult = myTable.Query(config);

当我执行它时,出现以下错误:

Cannot specify the AttributesToGet when choosing to get ALL_ATTRIBUTES

我阅读了文档,但我不明白,我正在看这个问题 How to query range key programmatically in DynamoDB但对我没有帮助。

很明显我做得不好,我的知识也有限。那么,是否有人可以向我解释如何实现查询,或者我是否必须使用扫描操作?

非常感谢。 如果需要更多信息,请告诉我。

最佳答案

好吧,在针对我的问题搜索和测试了许多可能的解决方案之后,我明白我对我的 table 所做的是错误的。事情是这样的:

我的表格由以下人员组成:

  • HASH KEY = id
  • RANGE = Company
  • ATTRIBUTE NOT KEY = Code

我想获取所有具有特定公司的代码。所以,我试图使用 RANGE 条件而不是使用 HASH KEY 来查询表,这是错误的!

将表格模型更改为

  • HASH KEY = Company
  • RANGE = id
  • ATTRIBUTE NOT KEY = Code

完美运行

这解释了一切(来自官方 Amazon Dynamodb 文档)

A Query operation finds items in a table or a secondary index using only primary key attribute values. You must provide a hash key attribute name and a distinct value to search for. You can optionally provide a range key attribute name and value, and use a comparison operator to refine the search results. By default, a Query operation returns all of the data attributes for items with the specified primary key(s); however, you can use the ProjectionExpression parameter so that the Query operation only returns some of the attributes, rather than all of them.

这是 link如果你想了解更多

[更新] 深入阅读后,我理解了GSI(Global Secondary Index)的概念。考虑到我的表的初始模型并将 GSI 添加到 [Company,Code],我可以毫无问题地进行查询。

关于c# - 尝试了解 DynamoDB .NET 中的查询表操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31254470/

相关文章:

c# - 从一串完整的 html 构建 HtmlGenericControl

c# - "System.Threading.Tasks.Task` 1[System.Web.Mvc.ActionResult]”显示而不是 View

c# - NULLS 应该在代码中还是在数据库中处理?的优点和缺点?

c# - ASP.NET MVC3 和服务器端验证

c# - 如何优化此方法中的数学运算?

amazon-web-services - 使用 AWS 网络 ACL 与 SG 进行访问控制?

amazon-web-services - CloudFormation - 引用资源作为参数的默认值

c# - 64 位 SQLite.dll 和任何 CPU

amazon-web-services - 如何在 AWS ALB 中实现客户端凭证授予

c# - 如何实现类似 "Fences"的东西