【问题标题】:Composite Primary Key - Dynamodb for Rust复合主键 - 用于 Rust 的 Dynamodb
【发布时间】:2022-09-28 06:38:22
【问题描述】:

如何在 rust 中定义 dynamodb 中的复合键?目前我们正在创建类似于 rust CRUD github 官方示例的表

    match client
        .create_table()
        .table_name(table_name)
        .key_schema(ks)
        .attribute_definitions(ad)
        .provisioned_throughput(pt)
        .send()
        .await
    {
        Ok(_) => println!(\"Added table {} with key {}\", table, key),
        Err(e) => {
            println!(\"Got an error creating table:\");
            println!(\"{}\", e);
        }
    };

在 python 和 JS 中,您可以将字典对象传递给 .key_scheme 和 .attributes,但在 Rust 实现中,似乎 key_scheme 被定义为

        pub fn key_schema(mut self, input: crate::model::KeySchemaElement) -> Self {
            self.inner = self.inner.key_schema(input);
            self
        }

所以我们不确定如何创建复合主键!

    标签: rust amazon-dynamodb


    【解决方案1】:

    离开documentation,您似乎会多次调用.key_schema 以获得复合键。文档说:

    将项目附加到 KeySchema

    要覆盖此集合的内容,请使用 set_key_schema

    [...]

    对于复合主键(分区键和排序键),您必须按以下顺序恰好提供两个元素:第一个元素必须有 KeyTypeHASH,第二个元素必须有 KeyType 的 @ 987654328@。

    所以看起来你的定义应该是这样的:

    client
        .create_table()
        .table_name(table_name)
        .key_schema(ks1)
        .key_schema(ks2)
    
    // or
    
    client
        .create_table()
        .table_name(table_name)
        .set_key_schema(Some(vec![ks1, ks2]))
    

    attribute_definitionsset_attribute_definitions 也记录了类似的模式。免责声明:我对 DynamoDB 不是很熟悉,我只是在转发文档。

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 2016-05-17
      • 2017-12-13
      • 2023-01-19
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      相关资源
      最近更新 更多