在.net中使用dynamodbcontext查询DynamoDB.使用QueryOperator vs使用QueryF

本文关键字:使用 DynamoDB QueryOperator vs QueryF 查询 dynamodbcontext net | 更新日期: 2023-09-27 18:02:50

我正在尝试使用具有HashKey和RangeKey的全局二级索引查询DynamoDB表。

我想要QUERYIndexCustomerIdCustomerIdDateTime的范围(从到)。没有将索引RangeKey设置为DateTime的选项。唯一的选择是:String, NumberBinary

我使用 c#对象持久化模型Amazon.DynamoDBv2。. net Framework v3.5上的DataModel API。

表:

+-------------------+----------+---------------------------+----------------+------------+
| InputFlowCode     |  Counter |  OrderIssueDate           |  OrderTypeCode | CustomerId |
+-------------------+----------+---------------------------+----------------+------------+
| bac9-35df6ac533fc |  000004  |  2016-07-19T22:00:00.000Z | 220            | 123        |
+-------------------+----------+---------------------------+----------------+------------+
| a3db-9d6f56a5c611 |  000006  |  2016-06-30T22:00:00.000Z | 220            | 456        |
+-------------------+----------+---------------------------+----------------+------------+
| af1c-db5b089c1e32 |  000010  |  2016-07-02T22:00:00.000Z | 220            | 789        |
+-------------------+----------+---------------------------+----------------+------------+
| ...               | ...      | ...                       | ...            | ...        |
+-------------------+----------+---------------------------+----------------+------------+

全局二级索引定义:

IndexCustomerId:
- CustomerId (integer), 
- OrderIssueDate (DateTime on model, but string on Index definition)

代码:

try
{
    DateTime dateFrom = new DateTime(2016, 08, 01);
    DateTime dateTo = new DateTime(2016, 08, 15);
    List<MyDynamoDBItem> items = new List<MyDynamoDBItem>();
    DynamoDBOperationConfig operationConfig = new DynamoDBOperationConfig();
    operationConfig.OverrideTableName = "Transactions";
    operationConfig.IndexName = "IndexCustomerId";
    DynamoDBContext context = new DynamoDBContext(DynamoDBClient);
    // 1) Works
    items = context.Query<MyDynamoDBItem>(customerId, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();
    // 2) Doesn't work
    items = context.Query<MyDynamoDBItem>(customerId, QueryOperator.Between, dateFrom, dateTo, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();
    // 3) Works, but I don't know if it is the right choice...
    List<ScanCondition> conditions = new List<ScanCondition>();
    conditions.Add(new ScanCondition(PeppolOrdineDynamoDBTableAttributes.OrderIssueDate, ScanOperator.Between, dateFrom, dateTo));
    operationConfig.QueryFilter = conditions;
    items = context.Query<MyDynamoDBItem>(customerId, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();
}
catch (Exception)
{
    throw;
}

第一个不带参数的查询有效。

第二个查询使用Between操作符和2个DateTime抛出一个Exception:
{"Cannot cast objects of type 'System.Int32' to type 'System.String'."}

第三个查询使用QueryFilter是一个列表和ScanOperator也可以工作,但我不知道与QueryOperator的区别是什么,如果它是正确的选择,因为我想使QUERY而不是SCAN

在.net中使用dynamodbcontext查询DynamoDB.使用QueryOperator vs使用QueryF

我也有同样的问题,但是我使用的是greaterThan,我发现将参数值添加到对象数组中可以修复它。

可能是这样的:

items = context.Query<MyDynamoDBItem>(customerId, QueryOperator.Between, new object[] {dateFrom, dateTo}, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();