2013-06-22 22 views
2

我爲boto使用DynamoDb v2接口在我的表中創建計數器增量。 (我需要V2接口,因爲我將在稍後處理索引)使用Python中的boto.dynamodb2在AWS DynamoDb中增加計數器

不知何故,我無法找到如何做到這一點,而無需獲取項目&再次更新它。

這裏是代碼我使用

from boto.dynamodb2.table import Table 
from boto.dynamodb2.items import Item 

my_table = Table('my-table') 

# Update counter for existing record. 
data = {'key': 'my_key', 
     'range_key': 'my_range', 
     } 

item = Item(my_table, data) 
#### Do something here to increment 'counter' by 1 
item.save() 

我應該怎麼做才能增加一個「反」字段?

回答

5

退房這樣的回答:Update DynamoDB Atomic Counter with Python/Boto

這是處理DynamoDB V1,如果這仍與V2的作品我沒有測試過。

如果您已經獲取的當前值,看來

item.add_attribute('counter', 1) 
item.save() 

會做一個update_item請求。

你也可以做直接在update_item要求:

dynoConnLayer1.update_item("my_table", 
        {"key":{"S":"my_key"}, 'range_key' : {"S": "my_range"}}, 
        {"counter": 
         {"Action":"ADD","Value":{"N":"1"}} 
        } 
       ) 
+0

不DynamoDB V2博託API工作 –