我正在努力在Rust中使用list_append编程。
我有一个名为Humidities
:
{
"id": 177,
"Measurements": [
49
]
}
,我想添加元素。例如:
{
"id": 177,
"Measurements": [
49,
53
]
}
这是python的一个工作解决方案,我在这里找到了:
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(())
}
with a look:在DynamoDB中使用NodeJS将一个新对象添加到JSON数组
相同的结果,值未知:
Error: Unhandled(Error { code: Some("ValidationException"), message: Some("ExpressionAttributeValues contains invalid key: Syntax error; key: "value""), request_id: Some("1A8VEOEVSB7LMAB47H12N7IKC7VV4KQNSO5AEMVJF66Q9ASUAAJG"), extras: {} })
我找到了解决办法。有两个问题:
- list_append期望两个列表
- expression_attribute_values期望
: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(())
}