【问题标题】:AWS DynamoDB index attribute with multiple type具有多种类型的 AWS DynamoDB 索引属性
【发布时间】:2019-02-23 21:40:24
【问题描述】:

我有一张这样的桌子

account_id      email               deactivation_date           deleted_on
1               test1@test.com      2018-09-26T16:28:41.143Z    NULL
2               test2@test.com      2018-09-19T16:28:41.143Z    2018-09-19T16:28:41.143Z

我正在使用 AWS CLI 填充测试数据,如图所示,如果已停用,deleted_on 字段将为 null 或具有字符串。

我正在使用 cloudformation 模板来构建索引,但它一直失败。 我想在 deactivation_date 上建立一个索引,以便我可以查询 检索所有帐户

deactivation_date < tomorrow, and deleted_on is null

这是我的 cft

  AttributeDefinitions:        
    - AttributeName: "account_id"
      AttributeType: "S"     
    - AttributeName: "email"
      AttributeType: "S"
    - AttributeName: "deactivation_date"
      AttributeType: "S"          
  KeySchema:
    -
      AttributeName: "account_id"
      KeyType: "HASH"
  GlobalSecondaryIndexes:     
    - IndexName: "email-index"
      KeySchema:
        - AttributeName: "email"
          KeyType: "HASH"
      Projection:
        ProjectionType: "ALL"
      ProvisionedThroughput:
        ReadCapacityUnits: !If [conditionIsProd, 10, 5]
        WriteCapacityUnits: !If [conditionIsProd, 10, 5]  
    - IndexName: "deactivation_date-index"
      KeySchema:
        - AttributeName: "deactivation_date"
          KeyType: "RANGE"
      Projection:
        ProjectionType: "ALL"
      ProvisionedThroughput:
        ReadCapacityUnits: !If [conditionIsProd, 10, 5]
        WriteCapacityUnits: !If [conditionIsProd, 10, 5]    

它一直在抱怨

无效的 KeySchema:第一个 KeySchemaElement 不是 HASH 键类型(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求 ID:xxxxxxx)

如果我把 deleted_on 作为索引, 比如

- AttributeName: "deleted_on"
  AttributeType: "S"    

它会抱怨因为我试图插入 NULL,但是如果我这样做了

- AttributeName: "deleted_on"
  AttributeType: NULL   

它还会抱怨我试图添加字符串。

我不确定这样做的正确方法是什么

【问题讨论】:

    标签: database amazon-web-services aws-lambda amazon-dynamodb aws-sdk


    【解决方案1】:

    全局二级索引必须始终有一个哈希键 - 您只是定义一个范围键,这就是为什么您会收到“第一个 KeySchemaElement 不是哈希键类型”错误。

    其次,您不能在任何主键字段中放置空值或空值。

    在任何情况下,即使您正确设置了哈希和范围键,您也无法运行您想要的查询。原因是您在执行 DynamoDB 查询时必须始终指定准确的哈希键。所以'deactivation_date

    如果你能提供更多关于你试图用这个来完成什么的信息,也许可以提供更好的表格设计

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多