-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathtransact_write.py
58 lines (56 loc) · 1.75 KB
/
transact_write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import print_function # Python 2/3 compatibility
import boto3, json
from botocore.exceptions import ClientError
client = boto3.client('dynamodb', region_name='us-west-2')
try:
response = client.transact_write_items(
TransactItems=[
{
'Put': {
'TableName': 'RetailDatabase',
'Item': {
'PK': {
'S': '123'
},
'SK': {
'S': 'abc'
},
'User': {
'S': 'lhnng'
},
'Other': {
'S': 'Other attribute field'
},
},
'ConditionExpression': 'attribute_not_exists(PK)',
}
},
{
'Update': {
'TableName': 'RetailDatabase',
'Key': {
'PK': {
'S': '789'
},
'SK': {
'S': 'xyz'
}
},
'UpdateExpression': 'SET #o = :val',
'ExpressionAttributeValues': {
':val': {
'S': 'Hello, updated value'
}
},
'ExpressionAttributeNames': {
'#o': 'Other'
}
}
}
]
)
except ClientError as e:
print(e)
print(e.response['Error']['Message'])
else:
print("Transact Write succeeded")