curl 基于属性更新每个Dynamodb项的有效方法

rxztt3cl  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(168)

我们有一个DynamoDB表,其中有大约8,000条记录,我们需要对每条记录进行更新。每条记录的结构类似于:id: 1492357, appIds: [123232], info: "{stringified json}",我们想要做的是获取“appIds”数组并将其插入到“info”的字符串化json值中,以便如果我们解析json,appIds属性现在是json中的属性。
我们有一个API可以通过调用myApi/:id/update来完成这一任务,还有一个shell脚本可以从dynamo中获取表中每条记录的id。我们可以只循环id数组,并使用每个id来 curl API,但这似乎效率不高。
在Dynamo中有没有原生的方法来做类似的事情(绕过api)?我也不太熟悉脚本或curl,如果没有原生的方法来做,有没有更有效的方法来循环我们的id数组,并为每个id调用api?我们根本不关心api响应,除了它是否有200响应。

当前脚本:

#!/bin/bash

records=`aws dynamodb scan --table-name myTable --attributes-to-get '["id"]' | jq '.'`
count=$(jq '.Count' <<< "${records}")
ids=$(jq -r '.Items | .[] | {id: .id.S} | @base64' <<< "${records}")

echo ========== fetched $count records =========
for id in $ids; do
  _jq() {
    echo ${id} | base64 --decode | jq -r ${1}
  }
  userId=$(_jq '.id')
  url=$(curl myApi/${userId}/update)
  echo $userId :
done
xhv8bpkk

xhv8bpkk1#

我有一个类似于你的bash脚本。我可以使用bash从dynamoDB读取记录。然后我有另一个python脚本,它可以上传dynamoDB数据库中的项目。你可以做的是在for循环中调用python脚本,并向它发送相关参数。
猛击:

for item in "${table_data[@]}"; do
  argument1=$(jq '.argument1' <<< "$item")
  argument2=$(jq '.argument2' <<< "$item")
  argument3=$(jq '.argument3' <<< "$item")
  key = $(jq '.key' <<< "$item")
  sortkey = $(jq '.sortkey' <<< "$item")

  python3 cli.py update-dynamo-table --key $key --sortkey $sortkey \
                        --argument1 $argument1 \
                        --argument2 $argument2 \
                        --argument3 $argument3  #  > /dev/null 2>&1 &  Uncomment no to see the outputs of the python command

  echo "DynamoDB item updated correclty"

Python:

import click
import sys
import os
import boto3

## Connecting to aws profile, boto3, dynamoDB table
aws_profile = os.environ.get('AWS_PROFILE')
aws_region = os.environ.get('AWS_REGION')
click.echo(f"Using profile {aws_profile} and region {aws_region}" )
session = boto3.session.Session(profile_name=aws_profile)
dynamodb = boto3.resource('dynamodb', region_name=aws_region)
table = dynamodb.Table('Name-of-dynamodb-table')

@click.group()
    def updateCommand():
    pass

@updateCommand.command()
@click.option('--key',  help='Primary Key of item on dynamoDB.')
@click.option('--sortkey',  help='Sort key of item on dynamoDB.')
@click.option('--argument1',  help='Value of argument1 to update on dynamoDB.')
@click.option('--argument2',  help='Value of argument2 to update on dynamoDB.')
@click.option('--argument3',  help='Value of argument3 to update on dynamoDB.')
## Declaration of function to update a table in DynamoDB.
## This function checks that the table exists annd if exists it updates the corresponding attributes.
def update_dynamo_table(key, sortkey, argument1, argument2, argument3):
    """Update an entry in dynamoDB table"""
    
    ## Validate that the item to update is actually in your dynamodb table
    response = pii_non_pii_table.get_item(
            Key={
                'key' : key,
                'sortkey' : sortkey
            }
        )
                
    if 'Item' not in response:
        click.echo("Item with specified keys does not exist, create it instead.")
        return None

    ## Table update
    ## Update expression string formation
    updateExpression = "set "
    expressionAttributeValues = {}
    expressionAttributeNames = {}

    if argument1 is not None :
        updateExpression += '#argument1=:argument1,'
        expressionAttributeNames['#argument1'] = "argument1"
        expressionAttributeValues[':argument1'] = argument1
    
    if argument2 is not None :
        updateExpression += '#argument2=:argument2,'
        expressionAttributeNames['#argument2'] = "argument2"
        expressionAttributeValues[':argument2'] = argument2
    
    if argument3 is not None :
        updateExpression += '#argument3=:argument3,'
        expressionAttributeNames['#argument3'] = "argument3"
        expressionAttributeValues[':argument3'] = argument3

    updateExpression = updateExpression[:-1]

    click.echo(updateExpression)
    click.echo(expressionAttributeValues)

    response = table.update_item(
        Key={
            'key': key,
            'sortkey': sortkey 
        },
        UpdateExpression=updateExpression,
        ExpressionAttributeNames=expressionAttributeNames,
        ExpressionAttributeValues=expressionAttributeValues,
        ReturnValues="UPDATED_NEW"
    )
    ## return response
    ## Output message.
    click.echo(f"Table UPDATED correctly.")

if __name__ == '__main__':
    updateCommand()

如果这有帮助,让我知道!!

相关问题