【问题标题】:AWS DynamoDB how to retrieve data from multiple tables using CreateMultiTableBatchGet without Range KeyAWS DynamoDB how to retrieve data from multiple tables using CreateMultiTableBatchGet without Range Key
【发布时间】:2022-12-19 09:44:09
【问题描述】:

I'm trying to retrieve data from dynamoDB multiple tables, in each table I have hash key and range key however I just have hash key so without range key it doesn't work, it complains like range key missing.

var batchGets = new List<Amazon.DynamoDBv2.DataModel.BatchGet>();

public void SetBatchGet<T>(T entity)
        {
            var batch = context.CreateBatchGet<T>(null);
            batch.AddKey(entity);         /// ERROR : Range key missing 
            batchGets.Add(batch);
        }

public async Task<List<object>> CreateMultiTableBatchGet(DynamoDBOperationConfig dynamoDBOperationConfig = null)
        {
            try
            {
                
                var result = context.CreateMultiTableBatchGet(batchGets.ToArray());
                await result.ExecuteAsync();
                batchGets = new List<Amazon.DynamoDBv2.DataModel.BatchGet>();

                return null;
            }
            catch (System.Exception ex)
            {
                batchGets = new List<Amazon.DynamoDBv2.DataModel.BatchGet>();
                throw ex;
            }
        }


// Caller

 var pr = new Participant();
            pr.Code = Code;  // Just passing hash key.
_repo.SetBatchGet(pr);
_repo.CreateMultiTableBatchGet();

How to make Multitable work without range key.

【问题讨论】:

    标签: c# amazon-web-services amazon-dynamodb asp.net-core-webapi


    【解决方案1】:

    For a BatchGetItems you have to provide the full primary key, hash and range key. If the range key is unknown you will need to use a Query request where you only need to provide a hash key. However, there is no such thing as a BatchQueryItems.

    The closest thing being the PartiQL API ExecuteStatement where you can spoof a "BatchQuery" using the IN operator:

    SELECT * FROM mytable WHERE pk IN [1,2,3]

    【讨论】:

    • @Ermiya Eskandary But does it work on multiple tables at once ??
    • same question as above
    • Use BatchExecuteStatement and it does.
    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2022-12-01
    • 2022-12-27
    • 2022-12-01
    • 2022-12-27
    • 1970-01-01
    • 2022-12-02
    • 2022-12-27
    相关资源
    最近更新 更多