我正在尝试使用带有HashKey和RangeKey的全局二级索引来查询DynamoDB表。
我想QUERY
IndexCustomerId
到CustomerId
和DateTime
范围(从 - 到)。没有选项将Index RangeKey设置为DateTime
。唯一的选择是:String
,Number
和Binary
。
我在.NET Framework v3.5上使用 C#Object Persistence Model Amazon.DynamoDBv2.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
。
答案 0 :(得分:0)
我遇到了同样的问题,但我使用的是更大的问题,我发现将参数值添加到对象数组中就修复了它。
所以可能是这样的:
items = context.Query<MyDynamoDBItem>(customerId, QueryOperator.Between, new object[] {dateFrom, dateTo}, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();