Is there a way to query multiple hash keys in DynamoDB?

dynamodb query multiple primary keys
dynamodb query multiple hash keys
dynamodb query multiple keys nodejs
dynamodb query multiple indexes
dynamodb query sort key
dynamodb query multiple keys java
dynamodb query begins_with
dynamodb query without partition key

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();
    PStatus processStatus = new PStatus();

    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.

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).

Query multiple Hash Keys in DynamoDB, Querying on Multiple Attributes in Amazon DynamoDB But as in any key/value store, it can be tricky to store data in a way that allows you to id (PrimaryKey), resourceId-Action-AccessedBy (GSI HashKey), resourceId  The HASH key is how your data is partitioned, while the RANGE key is how that data is sorted within a particular HASH key. The HASH key is particularly important -- you can only grab data for a single HASH key in a Query operation. The HASH and RANGE keys allow for a one-to-many like structure -- for a single HASH key, there can be multiple RANGE keys.

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

Querying on Multiple Attributes in Amazon DynamoDB, Learn how to retrieve multiple items with the Query operation with AWS you to select multiple Items that have the same partition ("HASH") key but different sort  Amazon DynamoDB is a non-relational key/value store database that provides incredible single-digit millisecond response times for reading or writing, and is unbounded by scaling issues. But as in any key/value store, it can be tricky to store data in a way that allows you to retrieve it efficiently.

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 } });
        var results = batchGet.Results;

Querying, In the next lessons, we'll work with this data using the Query and Scan API calls. The HASH key is how your data is partitioned, while the RANGE key is how  The way to fetch multiple items by a primary key query (which sounds weird at first), is to specify the hash key and then a range on the range key. It’s as if your key is split in two. Part 1

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.

Working with Multiple Items, where A is the primary hash key You can't have more than 2 fields as primary key in DynamoDB. But you will not be able to build query with multiple range keys simultaneously because DynamoDB can only use one index at a time. data is received from db) than you need but get the desired results. Querying Querying is a very powerful operation in DynamoDB. It allows you to select multiple Items that have the same partition ("HASH") key but different sort ("RANGE") keys. In this lesson, we'll learn some basics around the Query operation including using Queries to:

Amazon API doesn't support multiple hashkey filter but you can use HASH KEY + RANGE KEY filter to get the results using batchGetItem method ..

DynamoDB - Multiple range keys, 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  If you don't have a large number of invoices per company, you can query by only the Hash Key and do the filtering on the client side. If you have a large number of invoices per company and need to be able to query only the items for a single invoice, then I would create a secondary index on CompanyCode and InvoiceNumber.

Core Components of Amazon DynamoDB, You'll discover the basics of tables, items, attributes, and how they work DynamoDB uses the partition key's value as input to an internal hash function. and then query the index in much the same way as you'd query the Music table. DynamoDB provides similar functionality with its Query and Scan actions, which are described in Querying a Table and Scanning a Table. The SQL SELECT statement can perform table joins, allowing you to retrieve data from multiple tables at the same time.

Working with Queries in DynamoDB, Learn to search DynamoDB table data where the query operations search only The Query operation in Amazon DynamoDB finds items based on primary key values. You need to perform multiple Query operations to retrieve all the results (see By default, a Query operation does not return any data on how much read​  DynamoDB calculates the partition key's hash value, yielding the partition in which the item can be found. You can read multiple items from the table in a single operation (Query) if the items you want have the same partition key value. DynamoDB returns all of the items with that partition key value.

AWS DynamoDB, Check this DynamoDB documentation for more information, so use the latest AWS SDK for JAVA. It would look like this: Map<String  DynamoDB allows for specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.

  • Sorry, that's not even close to what I was asking for and I already know about secondary indexes.
  • I think your problem suggests that your schema needs to be different. If you repeatedly need to query 2 hash keys then perhaps that should be a special hash key on its own (duplicating the data from both states).
  • It was more a thought of sending the strings once and letting the server compare the current item it's checking to both before moving to the next item to check. It would be more efficient to query if it could do this I would imagine, but I guess I'll have to wait and see if they do decide to put this in some day. hopeful, but not expecting much Thanks for the response at least... I'll have to do the double/triple queries for now.
  • @DGolberg Actually, to my knowledge dynamodb stores the range-key indexes for each hash-key totally separate from each other, possibly even on different partitions/hosts. Thus by implementing a multi-query, dynamo would have to add overhead like "what happens when 1 fails and 1 doesn't?" "What if one gets throttled?", "What if one is faster? (which host waits and does the join of the data)?", etc. This is my guess as to why they didn't implement it. That said, the "simplicity" wins are pretty big for the customer, so it wouldn't surprise me if they added it some day.
  • Interesting. I'm not overly familiar with the inner workings of DynamoDB yet, so thanks for the info on that. I'll have to be sure to update this if/when they implement something like this as it would certainly help speed things up when multiple queries are necessary.
  • @CoryKendall And what about this answer?… What does UNION ?
  • @AybatDuyshokov Is the performance of QueryFilter filtering part the same as with Scan?
  • Tuukka Mustonen 1. select by hash keys 2. filter resultset with query filter (yep, performance of this step is the same as scan) 3. return final resultset.
  • Does not work. You MUST supply a range key for the batchLoad() operation when a table contains one or you get a DynamoDBMappingException. This prevents you from simply grabbing all of the items whose hash keys are the same. For example; say you have a list of servers. This list tells you what servers are online/offline/suspended/etc. via the hash key and what the server name is via the range key. The ONLY way I know of to get all of the servers that are listed as online as well as those listed as offline (without knowing their individual names) is to do a scan or 2 queries.
  • This is what the last poster suggested and it does not work for this situation. The whole purpose of the query is to find the IDs that match a certain status (which, incidentally, are also the range key). The batchGetItem request requires that you know both the hash and range key. The only reason I even have a range key is so that I can have multiple hash keys with the same value which can be queried.