You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用serde_dynamodb写入DynamoDB时AttributeValue类型不匹配问题求解

解决Rust写入DynamoDB时的AttributeValue类型不匹配问题

问题描述

我尝试将嵌套JSON字符串写入DynamoDB,使用的JSON字符串如下:

json_string = "{\"name\":\"as.up.data.forward\",\"time\":\"2023-03-13T18:58:41.933481750Z\",\"identifiers\":[{\"device_ids\":{\"device_id\":\"eui-a84041411184ad68\",\"application_ids\":{\"application_id\":\"thermometer2\"}}}"

对应的Rust代码:

use std::collections::{HashMap};
use lambda_http::{service_fn, Body, Error, IntoResponse, Request, RequestExt};
use rusoto_core::Region;
use rusoto_dynamodb::{AttributeValue, DynamoDb, DynamoDbClient, PutItemInput};
use serde_dynamodb::to_hashmap;

async fn write_to_dynamodb(json_string: &String) -> Result<(), Box<dyn std::error::Error>> {
    let client = DynamoDbClient::new(Region::EuWest2);

    let data: HashMap<String, AttributeValue> = to_hashmap(&json_string).unwrap();
    client.put_item(PutItemInput{
        item: data,
        table_name: "MyTable".to_string(),
        ..PutItemInput::default()
    }).await?;

    Ok(())
}

运行时触发类型不匹配错误:

error[E0308]: mismatched types
  --> src/main.rs:97:49
   |
97 |     let data: HashMap<String, AttributeValue> = to_hashmap(&json_string).unwrap();
   |               -------------------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `AttributeValue`, found struct `rusoto_dynamodb::generated::AttributeValue`
   |               |
   |               expected due to this
   |
   = note: struct `rusoto_dynamodb::generated::AttributeValue` and struct `AttributeValue` have similar names, but are actually distinct types
note: struct `rusoto_dynamodb::generated::AttributeValue` is defined in crate `rusoto_dynamodb`
  --> /Users/anna/.cargo/registry/src/github.com-1ecc6299db9ec823/rusoto_dynamodb-0.47.0/src/generated.rs:84:1
   |
84 | pub struct AttributeValue {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: struct `AttributeValue` is defined in crate `rusoto_dynamodb`
  --> /Users/anna/.cargo/registry/src/github.com-1ecc6299db9ec823/rusoto_dynamodb-0.48.0/src/generated.rs:84:1
   |
84 | pub struct AttributeValue {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: perhaps two different versions of crate `rusoto_dynamodb` are being used?

解决方案

错误根源是项目中同时引入了两个不同版本的rusoto_dynamodb crate:代码直接依赖的是0.48.0版本的AttributeValue,但serde_dynamodb间接依赖的是0.47.0版本,导致两者的AttributeValue属于不同类型,无法兼容。

具体修复步骤:

  1. 统一依赖版本
    打开Cargo.toml,强制所有涉及rusoto_dynamodb的依赖使用同一版本。例如指定为0.48.0:

    [dependencies]
    rusoto_dynamodb = "0.48.0"
    serde_dynamodb = { version = "0.4.0", features = ["rusoto_0_48"] }
    # 保留其他原有依赖
    

    注意serde_dynamodb需要启用对应rusoto版本的特性(如rusoto_0_48),特性名称可参考serde_dynamodb的官方说明确认。

  2. 清理缓存并重建
    执行命令清理旧依赖缓存,避免版本冲突残留:

    cargo clean
    cargo build
    
  3. 修复JSON格式问题
    你提供的JSON字符串存在语法错误(缺少闭合的}]}),需要修正为完整的JSON格式,否则后续反序列化会失败。

内容的提问来源于stack exchange,提问作者Richard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:07:04