如何在 Dynamo 表中更新带有 2 个字段的计数。

huangapple go评论80阅读模式
英文:

How to update count in dynamo table with 2 fields

问题

# Dynamodb表格有1个分区键和2个字段。我试图将a或b增加1。

# 从表格中获取a或b的cur_count(为0)并将其加1。

# 错误:

发生错误(ValidationException)


调用UpdateItem操作时:

无效的UpdateExpression:语法错误;令牌:“=”附近:“#a =#cur_count”


[![io][1]][1]

def update_count(vote):
    logging.info('更新计数....')
    print('更新计数....')
    print('投票' + str(vote))
    '''
    table.update_item(
    Key={'voter': 'count'},
    UpdateExpression="ADD #vote :incr",
    ExpressionAttributeNames={'#vote': vote},
    ExpressionAttributeValues={':incr': 1}
    )
    '''
    cur_count = 0
    try:
        if vote == 'b':
            print('提取b的当前计数')
            q = table.get_item(Key={'voter':'count'})
            res = q['Item']
            print(res)
            cur_count = int(res['b'])
            print('****** 当前计数 %d ' % cur_count)
            cur_count = str(cur_count)
            table.update_item(
                    Key={'voter':'count'},
                    UpdateExpression="ADD #b =#cur_count + :incr",
                    ExpressionAttributeNames={'#cur_count': cur_count},
                    ExpressionAttributeValues={':incr': 1})
            print('******* b %d ' % b)
        elif vote == 'a':
            print('提取a的当前计数')
            q = table.get_item(Key={'voter':'count'})
            res = q['Item']
            print(res)
            cur_count = int(res['a'])
            print('****** 当前计数 %d ' % cur_count)
            cur_count = str(cur_count)

            table.update_item(
                    Key={'voter':'count'},
                    UpdateExpression="ADD #a =#cur_count + :incr",
                    ExpressionAttributeNames={'#cur_count': cur_count},
                    ExpressionAttributeValues={':incr': 1})
            print('***** a %d ' % a)
    except Exception as e:
        print('在这里捕获错误')
        print(e)
英文:

The Dynamodb table has 1 partition key and 2 fields. I'm trying to increment a or b by 1.

I get cur_count for a or b(which is 0) from the table and +1 to it.

The error:

An error occurred (ValidationException) 

When calling the UpdateItem operation:

Invalid UpdateExpression: Syntax error; token: "=", near: "#a =#cur_count"

如何在 Dynamo 表中更新带有 2 个字段的计数。

def update_count(vote):
    logging.info('update count....')
    print('update count....')
    print('vote' + str(vote))
    '''
    table.update_item(
    Key={'voter': 'count'},
    UpdateExpression="ADD #vote :incr",
    ExpressionAttributeNames={'#vote': vote},
    ExpressionAttributeValues={':incr': 1}
    )
    '''
    cur_count = 0
    try:
        if vote == 'b':
            print('extracting cur count for b')
            q = table.get_item(Key={'voter':'count'})
            res = q['Item']
            print(res)
            cur_count = int(res['b'])
            print('****** cur count %d ' % cur_count)
            cur_count = str(cur_count)
            table.update_item(
                    Key={'voter':'count'},
                    UpdateExpression="ADD #b =#cur_count + :incr",
                    ExpressionAttributeNames={'#cur_count': cur_count},
                    ExpressionAttributeValues={':incr': 1})
            print('******* b %d ' % b)
        elif vote == 'a':
            print('extracting cur count for a')
            q = table.get_item(Key={'voter':'count'})
            res = q['Item']
            print(res)
            cur_count = int(res['a'])
            print('****** cur count %d ' % cur_count)
            cur_count = str(cur_count)

            table.update_item(
                    Key={'voter':'count'},
                    UpdateExpression="ADD #a =#cur_count + :incr",
                    ExpressionAttributeNames={'#cur_count': cur_count},
                    ExpressionAttributeValues={':incr': 1})
            print('***** a %d ' % a)
    except Exception as e:
        print('catching error here')
        print(e)

答案1

得分: 1

你定义了 #a#b 作为列名表达式,但没有为这些列进行映射。

有两种修复此问题的选项。

table.update_item(
    Key={'voter': 'count'},
    UpdateExpression="ADD #column :incr",
    ExpressionAttributeNames={'#column': 'b'},
    ExpressionAttributeValues={':incr': 1}
)

table.update_item(
    Key={'voter': 'count'},
    UpdateExpression="ADD b :incr",
    ExpressionAttributeValues={':incr': 1}
)
英文:

You define #a or #b as column name expressions but there is no mapping for this columns.

There are two options to fix this issue.

table.update_item(
    Key={'voter':'count'},
    UpdateExpression="ADD #column :incr",
    ExpressionAttributeNames={'#column': 'b'},
    ExpressionAttributeValues={':incr': 1}
)

or

table.update_item(
    Key={'voter':'count'},
    UpdateExpression="ADD b :incr",
    ExpressionAttributeValues={':incr': 1}
)

huangapple
  • 本文由 发表于 2023年1月9日 15:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定