Reputation: 1294
Can I query in dynamodb tables using conditions just on secondary index. I don't want to keep any condition on the hashkey. Is there a way to do it?
Upvotes: 1
Views: 4583
Reputation: 79
Yes you can query DynamoDb with just secondary indexes without any use of HASH key..
Let's say you have DynamoDB table EmployeeData
with columns EmployeeID,Department and created_at_date..The partition key for this table is EmployeeID and sort key is Department..
You want to fetch data based on date created ..So first create secondary index with name date-index
on your DynamoDB table..
Then you can fetch data with secondary index using below code:
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('EmployeeData')
response = table.query(IndexName="date-index",
KeyConditionExpression=Key('created_at_date').eq('2020-01-03'))
print(response)
Upvotes: 0
Reputation: 3583
TL;DR Yes it is possible to query based on secondary indexes.
Example:
So consider a table with the following structure:
- user_id (partition key)
- created_at (sort key)
- account_id
- api_path
- execution_time
- platform
- ttl
- metadata
If you want to query based on account_id, you can create a GSI as follows:
account_id (partition key)
created_at (sort key)
lets say you called this index: account_id-created_at-index
Now its possible to query them as follows (python taken as language of choice for brevity, this applies to all their SDKs):
import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = db.Table('table_name')
response = table.query(KeyConditionExpression=Key('account_id').eq(some_account_id), IndexName=account_id-created_at-index)
Note the IndexName param here which is key.
If there are any items that apply to the KeyConditionExpression
, they are accessible in the response['Items']
as list of objects.
Upvotes: 0
Reputation: 79
You can query the dynamodb with secondry indexes.
let params = {
TableName: 'users',
KeyConditionExpression: "#_id = :_id",
IndexName: "_id-index",
ExpressionAttributeNames:{
"#_id": "_id"
},
ExpressionAttributeValues: {
":_id":userId
}
};
documentClient.query(params, function (err, data) {
if (err) {
callback(err);
}
else {
if (data && data.Items && data.Items[0].accessToken) {
userData = data.Items[0];
}
else userData ={};
callback(null,{userData:userData})
}
})
Upvotes: 0
Reputation: 47319
The Query
operation always requires the hash key, even if you are querying an LSI or a GSI.
Upvotes: 0
Reputation: 22105
As of December 2013, you can. Dynamo supports Global Secondary Indexes.
See the documentation page for how to perform queries. You basically have to specify the table and index. You can either get back a record if you projected all the attributes on the index, or else you get the original hash key which you can use to look up the object in the table like you might have previously.
Upvotes: 1
Reputation: 7450
No, to query DynamoDB you MUST know the hash key. If you don't know the hash key the only way to get data out is a full table Scan.
Upvotes: 0