Amazon dynamodb 使用全局辅助索引从dynamo db表中获取多条记录

Amazon dynamodb 使用全局辅助索引从dynamo db表中获取多条记录,amazon-dynamodb,asp.net-core-3.1,amazon-dynamodb-index,Amazon Dynamodb,Asp.net Core 3.1,Amazon Dynamodb Index,我有一个带有以下字段的dynamo db表CustomerOrders 主分区键:CustomerId(编号) 主要排序键:部门ID(编号) 顺序(序列化Json字符串) 我想在一个请求中查询多个客户,而不使用排序键(DepartmentId)。因此,我在CustomerId上创建了一个全局二级索引,并希望使用该索引仅使用CustomerId进行查询。对于运行批处理查询,我只看到与BatchGetItemAsync相关的文档。我看不到在BatchGetItemRequest上设置IndexNam

我有一个带有以下字段的dynamo db表
CustomerOrders

  • 主分区键:CustomerId(编号)
  • 主要排序键:部门ID(编号)
  • 顺序(序列化Json字符串)
  • 我想在一个请求中查询多个客户,而不使用排序键(DepartmentId)。因此,我在CustomerId上创建了一个全局二级索引,并希望使用该索引仅使用CustomerId进行查询。对于运行批处理查询,我只看到与
    BatchGetItemAsync
    相关的文档。我看不到在BatchGetItemRequest上设置IndexName的方法。如何做到这一点

    以下是迄今为止我的代码段:

    public async Task<List<CustomerOrder>> GetOrdersAsync(List<int> customerIds)
    {
        var orders = new List<CustomerOrder>();
    
        var tableKeys = new List<Dictionary<string, AttributeValue>>();
        foreach (var x in customerIds)
        {
            tableKeys.Add(new Dictionary<string, AttributeValue> { { "CustomerId", new AttributeValue { N = x.ToString() } } });
        }
    
        var dynamoTable = $"CustomerOrders";
    
        var keysAndAttributes = new KeysAndAttributes
        {
            AttributesToGet = new List<string> { "CustomerId", "DepartmentId", "Order" },
            Keys = tableKeys                
        };
    
        var request = new BatchGetItemRequest
        {
            ReturnConsumedCapacity = ReturnConsumedCapacity.INDEXES, // Not sure what this does
            RequestItems = new Dictionary<string, KeysAndAttributes> { { dynamoTable, keysAndAttributes } }
        };
    
        BatchGetItemResponse result;
        do
        {
            result = await dynamoDbClient.BatchGetItemAsync(request); // Exception gets thrown from here
    
            var responses = result.Responses;
            foreach (var tableName in responses.Keys)
            {
                var tableItems = responses[tableName];
                foreach (var item in tableItems)
                {
                    orders.Add(new CustomerOrder
                    {
                        CustomerId = int.Parse(item["CustomerId"].N),
                        DepartmentId = int.Parse(item["DepartmentId"].N),
                        Order = JsonConvert.DeserializeObject<Order>(item["Order"].S)
                    });
            }
        }
    
        //  Set RequestItems to the result's UnprocessedKeys and reissue request
        request.RequestItems = result.UnprocessedKeys;
    
        } while (result.UnprocessedKeys.Count > 0);
    
        return orders;
    }
    
    公共异步任务GetOrdersAsync(列出客户ID)
    {
    var orders=新列表();
    var tableKeys=新列表();
    foreach(CustomerID中的var x)
    {
    Add(新字典{“CustomerId”,新属性值{N=x.ToString()}});
    }
    var dynamoTable=$“客户订单”;
    var keysAndAttributes=新的keysAndAttributes
    {
    AttributesToGet=新列表{“CustomerId”、“DepartmentId”、“Order”},
    键=表键
    };
    var request=新的BatchGetItemRequest
    {
    ReturnConsumedCapacity=ReturnConsumedCapacity.index,//不确定这是做什么的
    RequestItems=新字典{{dynamoTable,keysAndAttributes}
    };
    BatchGetItemResponse结果;
    做
    {
    result=wait dynamoDbClient.BatchGetItemAsync(请求);//从此处引发异常
    var响应=结果响应;
    foreach(responses.Keys中的var tableName)
    {
    var tableItems=响应[tableName];
    foreach(tableItems中的变量项)
    {
    订单。添加(新客户订单)
    {
    CustomerId=int.Parse(项[“CustomerId”].N),
    DepartmentId=int.Parse(项[“DepartmentId”].N),
    Order=JsonConvert.DeserializeObject(项[“Order”].S)
    });
    }
    }
    //将RequestItems设置为结果的未处理密钥,然后重新发出请求
    request.RequestItems=result.UnprocessedKeys;
    }而(result.UnprocessedKeys.Count>0);
    退货订单;
    }
    
    我得到的
    提供的键元素与上述代码的架构
    错误不匹配。请帮忙

    您不能“在BatchGetItemRequest上设置IndexName”

    事实上,您也不能在GSI/LSI上获取Item()。GetItem()仅对表有效

    GetItem()始终需要完整的主键

    对于部分键,您需要执行多个Query(),每个哈希键一个

    GSI没有为你做任何事。部门作为一个排序键实际上也没有为您做任何事情,因为我假设customerId是唯一的


    更好的结构可能是让表定义为主键只有散列键

    这是一个现有表。我正在尝试使用GSI使其工作,但根据您的评论,我不能。但是CustomerId是唯一的。所以我想GSI是一个解决方案。看起来线程和查询组合是选项。让我与拥有该表的团队谈谈,我希望他们能够删除该排序键。