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.
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.
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.
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.
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.
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 ...
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.
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.
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" )
Details on dynamodb updates using boto3
seem incredibly sparse online, so I'm hoping these alternative solutions are useful.
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)
import boto3 table = boto3.resource('dynamodb').Table('my_table') table.update_item( Key={'pkey': 'asdf12345'}, AttributeUpdates={ 'status': 'complete', }, )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With