问题
Is there a way to query multiple hash keys using a single query in Amazon's AWS SDK for Java?
Here's my issue; I have a DB table for project statuses. The Hash Key is the status of a project (ie: new, assigned, processing, or complete). The range key is a set of project IDs. Currently, I've got a query setup to simply find all the projects listed as a status(hash) of "assigned" and another query set to look for a status of "processing". Is there a way to do this using a single query rather than sending multiple queries for each status I need to find? Code below:
DynamoDBMapper mapper = new DynamoDBMapper(new AmazonDynamoDBClient(credentials));
PStatus assignedStatus = new PStatus();
assignedStatus.setStatus("assigned");
PStatus processStatus = new PStatus();
processStatus.setStatus("processing");
DynamoDBQueryExpression<PStatus> queryAssigned = new DynamoDBQueryExpression<PStatus>().withHashKeyValues(assignedStatus);
DynamoDBQueryExpression<PStatus> queryProcessing = new DynamoDBQueryExpression<PStatus>().withHashKeyValues(processStatus);
List<PStatus> assigned = mapper.query(PStatus.class, queryAssigned);
List<PStatus> process = mapper.query(PStatus.class, queryProcessing);
So basically, I'd like to know if it's possible to eliminate the queryAssigned
and assigned
variables and handle both assignedStatus
and processStatus
via the same query, process
, to find projects that are not new or complete.
回答1:
No, as of today there is no way to send multiple queries in the same request. If you're concerned about latency, you could make multiple requests simultaneously in different threads. This would require the same amount of network bandwidth as a "dual query" would if Dynamo offered it (assuming you're making 2, not hundreds).
回答2:
There is no way to query by multiple hash keys, but, as of April 2014, you can use QueryFilter so you can filter by non key fields in addition to hash key fields.
In a blog post on 24 April 2014, AWS announced the release of the "QueryFilter" option:
With today's release, we are extending this model with support for query filtering on non-key attributes. You can now include a QueryFilter as part of a call to the Query function. The filter is applied after the key-based retrieval and before the results are returned to you. Filtering in this manner can reduce the amount of data returned to your application while also simplifying and streamlining your code
Check this out there http://aws.amazon.com/blogs/aws/improved-queries-and-updates-for-dynamodb/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+AmazonWebServicesBlog+%28Amazon+Web+Services+Blog%29
回答3:
Try this in C#. I think it's similar in Java. UserId it`s the hask key.
var table = Table.LoadTable(DynamoClient, "YourTableName");
var batchGet = table.CreateBatchGet();
batchGet.AddKey(new Dictionary<string, DynamoDBEntry>() { { "UserId", 123 } });
batchGet.AddKey(new Dictionary<string, DynamoDBEntry>() { { "UserId", 456 } });
batchGet.Execute();
var results = batchGet.Results;
回答4:
You might have a look at BatchGetItem
operation or the batchLoad()
method of the DynamoDBMapper
. Although a little different than a query in that it's not a query with an OR
condition on the hash key, it will allow you to accomplish (generally) the same thing. Here is the language agnostic documentation and here is the Javadoc.
回答5:
Amazon API doesn't support multiple hashkey filter but you can use HASH KEY + RANGE KEY filter to get the results using batchGetItem method ..
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/batch-operation-lowlevel-java.html#LowLevelJavaBatchGet
来源:https://stackoverflow.com/questions/17663695/is-there-a-way-to-query-multiple-hash-keys-in-dynamodb