简体   繁体   中英

AWS Dynamo DB query async with partition key

We are using dynamo db scan functionality to fetch all the data from dynamo like this and it works fine:

var myScanConditions = new List<ScanCondition>();
myScanConditions .Add(new ScanCondition("PartitionKey", ScanOperator.BeginsWith, "data"));
var myData= await Context.ScanAsync(myScanConditions ).GetRemainingAsync();
//some code to filter some data from above

in our dynamo db partition key is like

data#rec1
data#rec2
data#rec3
and so on

I wanted to check if we can replace Scan with Query. I tried using the below code by passing scan condition to the Query but looks like its not correct. It returns me nothing.

var myData= await Context.QueryAsync("data", myScanConditions );

So my question is there an option to provide partial text for partition key to the QueryAsync method and still return all records from dynamo. For example in my case as above if I just pass " data " (partial text) to my query async.

Is there a way to do this?

Thanks

Unfortunately, you can't search the partition key with a Query. Queries require and only support the equal operator on the partition key.

If you truly need to search all records in your table, then you must perform a Scan as that is exactly what Scans are for although inspecting all data comes as a cost.

Some ideas to consider:

  • If you can exclude some data or focus your search on a specific category defined by another field in your dataset, you could add a Global Secondary Index (GSI) to your table that uses a different field as the partition key and the current partition key as the sort key. You could then perform a query on the GSI which will allow you more flexibility on searching the sort key.
  • You could also create a GSI that only include the partition key in it and no other fields/columns. If you then use this GSI for the Scan, it would improve the performance and the cost of the Scan since only the single key column is searched/loaded instead of the entire table. Once you have the results, you would then be required to do a GetItem or BatchGetItem on the table to pull the full records (if needed).

References:

You have to have a composite (hash key + sort key) primary key to use query.

If you had "data" as your hash (partition) key, and rec1, rec2, rec3 as the sort key, then you could query just with "data".

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