Reputation: 617
Now i have a column like below (Statuscode_Env) in Dynamodb as a partition key.
1.Here when ever i pass a value like (421, 425, etc) it should give the corresponding code exist (like PRD here).
2.And vice versa, if i pass parameter as PRD, it should give the corresponding numbers like (421, 425, 427, etc).
I can achieve below by putting in 2 columns (part key, sort key). First case i can achieve , but second case i cant. Because i can search only using Partition key. Hence, storing data in a single column with separator as 'space'
Statuscode_Env
421 PRD
425 PRD
427 PRD
422 PRD
426 PRD
423 PRD
429 PRD
428 PRD
422 PRD
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('env')
response = table.query(KeyConditionExpression=Key('Statuscode_Env').eq('422 PRD'))
for i in response['Items']:
print(i['Statuscode_Env'])
Did in the above way, not correct... Tried with 'Contatins' and 'beginswith' but did not work out.
And suppose if i want to update code / name, how can i implement using python and dynamodb.
name | code
-----------
PRD | 425
PRD | 456
PRD | 427
Upvotes: 0
Views: 984
Reputation: 4832
KeyConditionExpression
can only be applied to partition and optionally to sort key. The documentation states the following.
Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression.
The same documentation continues with
To further refine the Query results, you can optionally provide a FilterExpression. A FilterExpression determines which items within the results should be returned to you. All of the other results are discarded.
Thus you can use FilterExpression
to narrow the result set more, which might provide support for your second use case.
If this is not enough, you'll probably need to look into using indexes, Global Secondary Index (GSI) spefically. Basically an index will create another table with different keys. To support your second requirement, I think the following GSI structure could work.
name | code
-----------
PRD | 425
PRD | 456
PRD | 427
....
Where name
is partition key amd code is sort key. But these attribute pairs need to always constitute an unique primary key for each record, otherwise you cannot use this structure.
Addendum: After reviewing my post and re-reading your question, I realized that you do not have separate name and code attributes, but a single attribute with data such as "PRD 425" and "PRD 426". I have no experience on this. The Query documentation again says the following about KeyConditionExpression
The condition that specifies the key value(s) for items to be retrieved by the Query action.
The condition must perform an equality test on a single partition key value.
The condition can optionally perform one of several comparison tests on a single sort key value. This allows Query to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values.
My understanding on this is that you cannot limit it the way required.
Addendum 2: After more thought, I remembered that DynamoDB documentations commonly suggest a strategy where the partition key is suffixed with random numbers for example. Unfortunately I don't have details on how to perform queries against that schema, but it should give you hints on organizing your data.
Upvotes: 1