使用.NET Core对象持久性模型查询DynamoDB GSI复合键时出现问题

时间:2019-09-09 04:39:20

标签: asp.net amazon-dynamodb object-persistence

我想使用OPM查询GSI复合键。我遵循了文档示例,其链接如下: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBContext.QueryScan.html

但是我面临两个问题:

1)。上面示例中提到的相同代码对我来说引发错误。我相信 context.QueryAsync(Hask Key目标值,Operator.Between,RangeKey目标值较低,RangeKey目标值较高)是语法。

但是对于最后两个范围键目标值,我得到如下错误: 错误CS1503参数3:无法从“字符串”转换为“ System.Collections.Generic.IEnumerable”

2)。如何设置查询的目标是GSI而不是普通组合键。

为了指示查询目标GSI,我在Google上搜索了以下代码段:DynamoDBOperationConfig()。但是如何将其合并到最终的Context.QueryAsync方法中?

using Amazon.DynamoDBv2.DataModel;
using System;
using System.Collections.Generic;
using System.Text;

namespace EDCPA.Core.Models
{
    [DynamoDBTable("EDCBUILDDATA1")]
    public class AARes
    {

        [DynamoDBGlobalSecondaryIndexHashKey("PN-FGD-Index")]
        public string ProjectName { get; set; }

        [DynamoDBGlobalSecondaryIndexRangeKey("PN-FGD-Index")]
        public string FileGeneratedDate { get; set; }

        public string VehicleName { get; set; }

        public string FileNameKey { get; set; }

        [DynamoDBHashKey]
        public string Id { get; set; }

        [DynamoDBRangeKey]
        public string createdTimeStamp { get; set; }


    }
}
using Amazon.DynamoDBv2.DataModel;
using System;
using System.Collections.Generic;
using System.Text;

namespace EDCPA.Core.Models
{
    [DynamoDBTable("EDCBUILDDATA1")]
    public class AAReq
    {
        public string FileGeneratedFromDate { get; set; }

        public string FileGeneratedToDate { get; set; }

        public string ProjectName { get; set; }

        public string VehicleName { get; set; }

        [DynamoDBHashKey]
        public string Id { get; set; }

        [DynamoDBRangeKey]
        public string createdTimeStamp { get; set; }
    }
}

public async Task<IActionResult> AARequestAsync([FromBody] AAReq req)
        {
            try
            {
                headers = HeaderCollections.TryRetrieveToken(Request);


AmazonDynamoDBClient client = new AmazonDynamoDBClient(new StoredProfileAWSCredentials(),
                     RegionEndpoint.APSouth1);

                DynamoDBContext context = new DynamoDBContext(client);

                DynamoDBOperationConfig indexHashRangeOpConfig = new DynamoDBOperationConfig()
                {
                    IndexName = "PN-FGD-Index",
                    ConsistentRead = false,
                };

                IEnumerable<AARes> fileKeys = await
                context.QueryAsync<AARes>(req.ProjectName, QueryOperator.Between, req.FileGeneratedFromDate, req.FileGeneratedToDate);
                Console.WriteLine("\nFindRepliesInLast15Days: Printing result.....");
                foreach (AARes r in fileKeys)
                    Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.FileNameKey, r.VehicleName);

1)。我在context.QueryAsync语句中怎么了?

2)。另外,如何将DynamoDBOperationConfig indexHashRangeOpConfig合并到我的最终查询语句中?

1 个答案:

答案 0 :(得分:1)

这是我上面的问题的解决方案:

List<Dashboardreq> list =
                await context.QueryAsync<Dashboardreq>(req.ProjectName, QueryOperator.Between, new string[] {
                                                 req.FileGeneratedFromDate+" " + Constants.DayBeginTime,
                                                 req.FileGeneratedToDate+" " + Constants.DayEndTime
                                                 }, indexHashRangeOpConfig).GetRemainingAsync();