<tfoot id='eiCys'></tfoot>
<legend id='eiCys'><style id='eiCys'><dir id='eiCys'><q id='eiCys'></q></dir></style></legend>

    1. <i id='eiCys'><tr id='eiCys'><dt id='eiCys'><q id='eiCys'><span id='eiCys'><b id='eiCys'><form id='eiCys'><ins id='eiCys'></ins><ul id='eiCys'></ul><sub id='eiCys'></sub></form><legend id='eiCys'></legend><bdo id='eiCys'><pre id='eiCys'><center id='eiCys'></center></pre></bdo></b><th id='eiCys'></th></span></q></dt></tr></i><div id='eiCys'><tfoot id='eiCys'></tfoot><dl id='eiCys'><fieldset id='eiCys'></fieldset></dl></div>
    2. <small id='eiCys'></small><noframes id='eiCys'>

      • <bdo id='eiCys'></bdo><ul id='eiCys'></ul>

      使用 Python/Boto 更新 DynamoDB 原子计数器

      时间:2023-07-05

    3. <i id='a3Qok'><tr id='a3Qok'><dt id='a3Qok'><q id='a3Qok'><span id='a3Qok'><b id='a3Qok'><form id='a3Qok'><ins id='a3Qok'></ins><ul id='a3Qok'></ul><sub id='a3Qok'></sub></form><legend id='a3Qok'></legend><bdo id='a3Qok'><pre id='a3Qok'><center id='a3Qok'></center></pre></bdo></b><th id='a3Qok'></th></span></q></dt></tr></i><div id='a3Qok'><tfoot id='a3Qok'></tfoot><dl id='a3Qok'><fieldset id='a3Qok'></fieldset></dl></div>

      1. <tfoot id='a3Qok'></tfoot>
        <legend id='a3Qok'><style id='a3Qok'><dir id='a3Qok'><q id='a3Qok'></q></dir></style></legend>
          <tbody id='a3Qok'></tbody>
          <bdo id='a3Qok'></bdo><ul id='a3Qok'></ul>
            • <small id='a3Qok'></small><noframes id='a3Qok'>

                本文介绍了使用 Python/Boto 更新 DynamoDB 原子计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 Python Boto 2.3.0 更新原子计数计数器,但找不到该操作的文档.

                I am trying to update an atomic count counter with Python Boto 2.3.0, but can find no documentation for the operation.

                似乎没有直接的接口,所以我尝试使用layer1接口进行原始"更新,但即使是简单的更新也无法完成.

                It seems there is no direct interface, so I tried to go to "raw" updates using the layer1 interface, but I was unable to complete even a simple update.

                我尝试了以下变体,但都没有运气

                I tried the following variations but all with no luck

                dynoConn.update_item(INFLUENCER_DATA_TABLE, 
                                     {'HashKeyElement': "9f08b4f5-d25a-4950-a948-0381c34aed1c"}, 
                                     {'new': {'Value': {'N':"1"}, 'Action': "ADD"}})    
                
                dynoConn.update_item('influencer_data', 
                                     {'HashKeyElement': "9f08b4f5-d25a-4950-a948-0381c34aed1c"}, 
                                     {'new': {'S' :'hello'}})                                 
                
                dynoConn.update_item("influencer_data", 
                                     {"HashKeyElement": "9f08b4f5-d25a-4950-a948-0381c34aed1c"},
                                     {"AttributesToPut" : {"new": {"S" :"hello"}}})      
                

                它们都产生相同的错误:

                They all produce the same error:

                  File "/usr/local/lib/python2.6/dist-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/layer1.py", line 164, in _retry_handler
                    data)
                boto.exception.DynamoDBResponseError: DynamoDBResponseError: 400 Bad Request
                {u'Message': u'Expected null', u'__type': u'com.amazon.coral.service#SerializationException'}
                

                我还研究了 API 文档 here,但它们非常简陋.

                I also investigated the API docs here but they were pretty spartan.

                我已经做了很多搜索和摆弄,我唯一剩下的就是使用 PHP API 并深入研究代码以找到它格式化"JSON 主体的位置,但这有点痛苦.请救我脱离痛苦!

                I have done a lot of searching and fiddling, and the only thing I have left is to use the PHP API and dive into the code to find where it "formats" the JSON body, but that is a bit of a pain. Please save me from that pain!

                推荐答案

                抱歉,我误解了您要查找的内容.尽管有一个小错误需要解决,但您可以通过 layer2 完成此操作.这是一些 Layer2 代码:

                Sorry, I misunderstood what you were looking for. You can accomplish this via layer2 although there is a small bug that needs to be addressed. Here's some Layer2 code:

                >>> import boto
                >>> c = boto.connect_dynamodb()
                >>> t = c.get_table('counter')
                >>> item = t.get_item('counter')
                >>> item
                {u'id': 'counter', u'n': 1}
                >>> item.add_attribute('n', 20)
                >>> item.save()
                {u'ConsumedCapacityUnits': 1.0}
                >>> item  # Here's the bug, local Item is not updated
                {u'id': 'counter', u'n': 1}
                >>> item = t.get_item('counter')  # Refetch item just to verify change occurred
                >>> item
                {u'id': 'counter', u'n': 21}
                

                这会产生与您在第 1 层代码中执行的相同的在线请求,如以下调试输出所示.

                This results in the same over-the-wire request as you are performing in your Layer1 code, as shown by the following debug output.

                2012-04-27 04:17:59,170 foo [DEBUG]:StringToSign:
                POST
                /
                
                host:dynamodb.us-east-1.amazonaws.com
                x-amz-date:Fri, 27 Apr 2012 11:17:59 GMT
                x-amz-security-    token:<removed> ==
                x-amz-target:DynamoDB_20111205.UpdateItem
                
                {"AttributeUpdates": {"n": {"Action": "ADD", "Value": {"N": "20"}}}, "TableName": "counter", "Key": {"HashKeyElement": {"S": "counter"}}}
                

                如果你想避免最初的 GetItem 调用,你可以这样做:

                If you want to avoid the initial GetItem call, you could do this instead:

                >>> import boto
                >>> c = boto.connect_dynamodb()
                >>> t = c.get_table('counter')
                >>> item = t.new_item('counter')
                >>> item.add_attribute('n', 20)
                >>> item.save()
                {u'ConsumedCapacityUnits': 1.0}
                

                如果项目已存在则更新该项目,如果项目尚不存在则创建它.

                Which will update the item if it already exists or create it if it doesn't yet exist.

                这篇关于使用 Python/Boto 更新 DynamoDB 原子计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用 boto3 手动从 DynamoDB 有线协议转换为本机 Python 对象? 下一篇:如何将地图插入 DynamoDB 表?

                相关文章

                    <bdo id='UEXqj'></bdo><ul id='UEXqj'></ul>
                  <tfoot id='UEXqj'></tfoot>

                1. <small id='UEXqj'></small><noframes id='UEXqj'>

                  <legend id='UEXqj'><style id='UEXqj'><dir id='UEXqj'><q id='UEXqj'></q></dir></style></legend>

                    <i id='UEXqj'><tr id='UEXqj'><dt id='UEXqj'><q id='UEXqj'><span id='UEXqj'><b id='UEXqj'><form id='UEXqj'><ins id='UEXqj'></ins><ul id='UEXqj'></ul><sub id='UEXqj'></sub></form><legend id='UEXqj'></legend><bdo id='UEXqj'><pre id='UEXqj'><center id='UEXqj'></center></pre></bdo></b><th id='UEXqj'></th></span></q></dt></tr></i><div id='UEXqj'><tfoot id='UEXqj'></tfoot><dl id='UEXqj'><fieldset id='UEXqj'></fieldset></dl></div>