Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of update_item in dynamodb boto3

Following the documentation, I'm trying to create an update statement that will update or add if not exists only one attribute in a dynamodb table.

I'm trying this

response = table.update_item(     Key={'ReleaseNumber': '1.0.179'},     UpdateExpression='SET',     ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')',     ExpressionAttributeNames={'attr1': 'val1'},     ExpressionAttributeValues={'val1': 'false'} ) 

The error I'm getting is:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "attr1"

If anyone has done anything similar to what I'm trying to achieve please share example.

like image 828
Dmitry R Avatar asked Dec 24 '15 04:12

Dmitry R


People also ask

How do you query DynamoDB with Boto3?

Query Tables in DynamoDB using Boto3 To query items in DynamoDB, you can use the query() method to fetch items based on primary key values. In addition, you can use the KeyConditionExpression to specify the value of the partition key and return all items from the table with that partition key.

How does Boto3 connect to DynamoDB?

Connecting AWS Python SDK (Boto3) with DynamoDBInstall the latest version of Boto3 by running the command below. This will install the Boto3 Python dependency, which is required for our code to run. Now we will connect with our local instance of DynamoDB using Python. We will use the code below to do so.

How do you update items in DynamoDB table?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

How do I perform a bulk update in DynamoDB?

Fetch the items that you wish to update. Perform an Update operation on each item. For example, we will update all the rows that have a hash key starting with "user" by introducing a new attribute named - "visibility" that has the value "private." Let us look at how we can perform this bulk update in DynamoDB.

What are the examples of DynamoDB boto3 query?

List of DynamoDB Boto3 Query Examples 1 Connecting Boto3 to DynamoDB 2 Create Table 3 Get All Items / Scan 4 Get Item 5 Batch Get Item 6 Put Item 7 Query Set of Items 8 Update Item 9 Conditionally Update Item 10 Increment Item Attribute 11 Delete Item 12 Delete All Items 13 Query with Sorting 14 Query Pagination 15 Run DynamoDB Local More ...

How to get all items from DynamoDB table?

To get all items from DynamoDB table, you can use Scan operation. The problem is that Scan has 1 MB limit on the amount of data it will return in a request, so we need to paginate through the results in a loop. import boto3 dynamodb = boto3. resource ('dynamodb', region_name = region) table = dynamodb.

How do I restrict the update logic in boto3?

Boto3 Conditionally Update Item Moreover, you can also add a ConditionExpression parameter, which restricts the update logic only if the evaluated expression equals true. import boto3 dynamodb = boto3. resource ('dynamodb', region_name = region) table = dynamodb.


2 Answers

Found working example here, very important to list as Keys all the indexes of the table, this will require additional query before update, but it works.

response = table.update_item(     Key={         'ReleaseNumber': releaseNumber,         'Timestamp': result[0]['Timestamp']     },     UpdateExpression="set Sanity = :r",     ExpressionAttributeValues={         ':r': 'false',     },     ReturnValues="UPDATED_NEW" ) 
like image 136
Dmitry R Avatar answered Oct 01 '22 01:10

Dmitry R


Details on dynamodb updates using boto3 seem incredibly sparse online, so I'm hoping these alternative solutions are useful.

get / put

import boto3  table = boto3.resource('dynamodb').Table('my_table')  # get item response = table.get_item(Key={'pkey': 'asdf12345'}) item = response['Item']  # update item['status'] = 'complete'  # put (idempotent) table.put_item(Item=item) 

actual update

import boto3  table = boto3.resource('dynamodb').Table('my_table')  table.update_item(     Key={'pkey': 'asdf12345'},     AttributeUpdates={         'status': 'complete',     }, ) 
like image 34
ryantuck Avatar answered Oct 01 '22 02:10

ryantuck