简体   繁体   中英

AWS DynamoDB how to retrieve data from multiple tables using CreateMultiTableBatchGet without Range Key

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.

For a BatchGetItems you have to provide the full primary key, hash(partition) and range(sort) 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM