如何使用Rust将元素追加到DynamoDB中的列表

pjngdqdw  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(106)

我在Rust中很难以编程方式使用list_append
我有一个名为Humidities的表:

{
 "id": 177,
 "Measurements": [
  49
 ]
}

我想添加元素。例如:

{
 "id": 177,
 "Measurements": [
  49,
  53
 ]
}

这是一个python的工作解决方案,我已经找到了here

table = get_dynamodb_resource().Table("table_name")
result = table.update_item(
    Key={
        'hash_key': hash_key,
        'range_key': range_key
    },
    UpdateExpression="SET some_attr = list_append(some_attr, :i)",
    ExpressionAttributeValues={
        ':i': [some_value],
    },
    ReturnValues="UPDATED_NEW"
)
if result['ResponseMetadata']['HTTPStatusCode'] == 200 and 'Attributes' in result:
    return result['Attributes']['some_attr']

基于python解决方案,我尝试了以下方法:

#[tokio::main]
async fn main() -> Result<(), Error> {
    let Opt { base } = Opt::from_args();
    let shared_config = make_config(base).await?;
    let client = Client::new(&shared_config);
    client
        .update_item()
        .table_name("Humidities")
        .key("id", AttributeValue::N(177.to_string()))
        .update_expression("SET i = list_append(Measurements, :i)")
        .expression_attribute_values("i", AttributeValue::N(53.to_string()))
        .send()
        .await?;
    Ok(())
}

然而,结果却是:

Error: Unhandled(Error { code: Some("ValidationException"), message: Some("ExpressionAttributeValues contains invalid key: Syntax error; key: \"i\""), request_id: Some("05RSFGFJEEDPO7850LI2T91RGRVV4KQNSO5AEMVJF66Q9ASUAAJG"), extras: {} })

我做错了什么?
以下示例仅演示如何添加单个项目:https://github.com/awslabs/aws-sdk-rust/blob/main/examples/dynamodb/src/bin/add-item.rs
我也试过这个:

#[tokio::main]
async fn main() -> Result<(), Error> {
    let Opt { base } = Opt::from_args();
    let shared_config = make_config(base).await?;
    let client = Client::new(&shared_config);
    client
        .update_item()
        .table_name("Humidities")
        .key("id", AttributeValue::N(177.to_string()))
        .update_expression("set #Measurements = list_append(#Measurements, :value)")
        .expression_attribute_names("#Measurements", "Measurements")
        .expression_attribute_values("value", AttributeValue::N(53.to_string()))
        .return_values(aws_sdk_dynamodb::model::ReturnValue::AllNew)
        .send()
        .await?;
    Ok(())
}

查看:Append a new object to a JSON Array in DynamoDB using NodeJS
结果相同,值未知:

Error: Unhandled(Error { code: Some("ValidationException"), message: Some("ExpressionAttributeValues contains invalid key: Syntax error; key: \"value\""), request_id: Some("1A8VEOEVSB7LMAB47H12N7IKC7VV4KQNSO5AEMVJF66Q9ASUAAJG"), extras: {} })
n6lpvg4x

n6lpvg4x1#

我找到了一个解决办法。有两个问题:

  • list_append需要两个列表
  • 表达式属性值应为:value,而不是value

运行示例:

async fn main() -> Result<(), Error> {
    let Opt { base } = Opt::from_args();
    let shared_config = make_config(base).await?;
    let client = Client::new(&shared_config);
    client
        .update_item()
        .table_name("Humidities")
        .key("id", AttributeValue::N(177.to_string()))
        .update_expression("set #Measurements = list_append(#Measurements, :value)")
        .expression_attribute_names("#Measurements", "Measurements")
        .expression_attribute_values(
            ":value", // changed from "value" to ":value"
            AttributeValue::L(vec![AttributeValue::N(53.to_string())]), // use a vector of numbers instead of a number
        )
        .return_values(aws_sdk_dynamodb::model::ReturnValue::AllNew)
        .send()
        .await?;
    Ok(())
}

相关问题