具有多种类型的 AWS DynamoDB 索引属性

AWS DynamoDB index attribute with multiple type

我有一个 table 看起来像这样

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 字段将为空或具有字符串。

我正在使用 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   

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

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

Global Secondary Index 必须始终有一个散列键 - 您只定义了一个范围键,这就是您收到 'first KeySchemaElement is not a HASH key type' 错误的原因。

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

在任何情况下,即使您正确设置了散列键和范围键,您也无法运行 查询您想要的查询。原因是您在执行 DynamoDB 查询时必须始终指定一个准确的散列键。所以 'deactivation_date < tomorrow' 将不起作用。

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