0
  1. I have a table called 'DATA' in dynamodb where I have 20 to 25 columns. But I need to pull only 3 columns from dynamodb.

  2. Required columns are status, ticket_id and country

table_name = 'DATA'
# dynamodb client 
dynamodb_client = boto3.client('dynamodb')

Required columns are status, ticket_id I'm able to achieve using scan as provided below. But I want to do the same using query method.

response = table.scan(AttributesToGet=['ticket_id','ticket_status'])

I tried the below code with query method. But I'm getting error.

response = table.query(ProjectionExpression=['ticket_id','ticket_status']),keyConditionExpression('opco_type').eq('cwc') or keyConditionExpression('opco_type').eq('cwp'))

Is there any way of getting only required columns from dynamo?

2
  • You need to use a projection expression docs.aws.amazon.com/amazondynamodb/latest/developerguide/… Commented Dec 21, 2021 at 12:06
  • Yes, I read about it..But got stucked, because there we need to put key value pairs. how can I use it inorder to get all values from specific columns. Please see my updated question. @luk2302 Commented Dec 21, 2021 at 12:22

1 Answer 1

2

As already commented, you need to use ProjectExpression:

dynamodb = boto3.resource('dynamodb', region_name=region)
table = dynamodb.Table(table_name)
item = table.get_item(Key={'Title': 'Scarface', 'Year': 1983}, ProjectionExpression='status, ticket_id, country')

Some things to note:

  • It is better to use resource instead of client. This will avoid special dynamodb json syntax.
  • You need to set the full (composite) key to get_item
  • Selected columns should be in a comma-separated string
  • It is a good idea to always use expression attribute names:
    item = table.get_item(Key={'Title': 'Scarface', 'Year': 1983}, 
                          ProjectionExpression='#status, ticket_id, country',
                          ExpressionAttributeNames={'#status': 'status'})
Sign up to request clarification or add additional context in comments.

6 Comments

#response = table.scan(AttributesToGet=['ticket_id','ticket_status']) I should achieve the same using projectionexpression. Any help @kgiannakakis
@Mahilan: Make sure that you understand the difference between scan and query.
Please see my updated question. I should fetch only columns with all the values. The above code is working but Is there any way to acheive the same using projectionexpression.
You mean get all items that have both ticket_id and ticket_status set? I don't think this is possible without creating a GSI.
yes, correct. Can you pls help me on this
|

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.