0

我正在尝试使用以下代码有条件地更新 DynamoDB 中的项目:

from boto.dynamodb2.table import Table

conn = get_layer1_ddb_connection()
values_table = Table(table_name, connection=conn)
attrs = { 'values' : new_values,
          'version' : existing_item['version'] + 1}
condition_expression = 'version = :v'
values_table.update_item(table_name, key=customer_id, attribute_updates=attrs, condition_expression=condition_expression, expression_attribute_values={':v': existing_item['version'],}, return_values='ALL_OLD',)

其中,layer1 连接是这样创建的:

from boto.dynamodb2.layer1 import DynamoDBConnection

def get_layer1_ddb_connection(self):
    return DynamoDBConnection(region=self.region, aws_access_key_id=self.creds[CRED_ACCESS_KEY], aws_secret_access_key=self.creds[CRED_SECRET_KEY])

self.region是 RegionInfo 类型,而 self.creds 对于其他高级 API 调用来说总是很完美。

4

2 回答 2

0

如果我没记错的话,这可能是table.py 源码中的一个提示。在私有_update_item()方法内部,它实际上调用了conn.update_item.

self.connection.update_item(self.table_name, raw_key, item_data, **kwargs)

所以也许尝试调用update_item()连接而不是表,看看是否有帮助。

于 2015-06-13T15:37:33.593 回答
0

这对我有用:

ddb_conn = dynamodb2.connect_to_region(region, self.creds[CRED_ACCESS_KEY], self.creds[CRED_SECRET_KEY], self.creds[CRED_SECURITY_TOKEN])

values_table = Table(table_name, connection=ddb_conn)
key = {"id": {"S": customer_id }}
update_expression = "SET #cv = :new_values, version = :new_version"
condition_expression = "version = :existing_version"
expression_attribute_names = {"#cv" : "customer-values"}

existing_item = values_table.get_item(id=customer_id)
existing_version = existing_item['version']
new_version = existing_version + 1

expression_attribute_values = {":new_values": {"N": str(new_values)}, ":new_version": {"N": str(new_version)}, ":existing_version": {"N": str(existing_version)}}  

ddb_conn.update_item(table_name, key, update_expression=update_expression, condition_expression=condition_expression, expression_attribute_names=expression_attribute_names, expression_attribute_values=expression_attribute_values)
于 2015-06-13T19:47:28.070 回答