0

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
4
  • You are asking for quite many questions but providing very little information. Details missing for example: What are the key attributes in your table? How the data needs to be accessed in other queries? In addition, AFAIK DynamoDB does not support querying attributes nested in JSON, which might effect your decision / limit your options on structuring the data. Commented Mar 5, 2019 at 20:09
  • Thanks for your update. Your comment is very helpful. Here i'm new to dynamodb. Value is partition key and Name is sort key. This is just an independent code, not linked to other tables. Regarding nested JSON querying i'll look what to do. Commented Mar 6, 2019 at 11:29
  • OK. I think you cannot have JSON data as your key attribute, so that rules out the second format. Still it is a bit unclear what you want to achieve with your two use cases. I'd recommend to restate your wording in the question. Commented Mar 6, 2019 at 12:52
  • Thanks for your time. I updated the details now. Please see whether you can help anything here.. Commented Mar 7, 2019 at 9:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the update, as i mentioned highlighted earlier how can update values in dynamodb using python. Updated my above question also.
I cannot be 100% sure, but I'd guess that you cannot update the values of key attributes. In general, updating record attributes is done with put and update operations. Boto3 documentation has examples of how this is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.