无法使用python在cdk中为dynamodb添加二级索引

Mat*_*att 3 python-3.x amazon-dynamodb aws-cdk

我正在尝试创建一个带有分区和排序键的二级索引的 dynamoDB 表。我可以在没有二级索引的情况下创建表,但还没有找到添加二级索引的方法

我已经查看了这两个资源,但没有找到任何可以实际向我展示我需要在我的 cdk python 脚本中使用二级索引创建资源的代码 https://docs.aws.amazon.com/cdk /api/latest/docs/@aws-cdk_aws-dynamodb.Table.html https://docs.aws.amazon.com/cdk/api/latest/docs/aws-dynamodb-readme.html

这是将创建表的代码

table_name = 'event-table-name'
    event_table = dynamoDB.Table(self, 'EventsTable',
         table_name=table_name,
         partition_key=Attribute(
             name='composite',
             type=AttributeType.STRING
         ),
         sort_key=Attribute(
             name='switch_ref',
             type=AttributeType.STRING
         ),
         removal_policy=core.RemovalPolicy.DESTROY,
         billing_mode=BillingMode.PAY_PER_REQUEST,
         stream=StreamViewType.NEW_IMAGE,
                                 )
Run Code Online (Sandbox Code Playgroud)

这是我需要附加到它的二级索引

secondaryIndex = dynamoDB.GlobalSecondaryIndexProps(
        index_name='mpan-status-index',
        partition_key=Attribute(
            name='field1',
            type=AttributeType.STRING
        ),
        sort_key=Attribute(
            name='field2',
            type=AttributeType.STRING
        ),
    )
Run Code Online (Sandbox Code Playgroud)

我尝试在表创建中添加块并尝试在表上调用 addSecondaryindex 方法。但是两者都失败,要么说意外的关键字要么对象没有属性 addGlobalSecondaryIndex

小智 6

应在 Table 类上调用 addGlobalSecondaryIndex。

下面的代码(在打字稿中)非常适合我:

const table = new ddb.Table(this, "EventsTable", {
  tableName: "event-table-name",
  partitionKey: { name: 'composite', type: ddb.AttributeType.STRING },
  sortKey: { name: 'switch_ref', type: ddb.AttributeType.STRING },
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  billingMode: BillingMode.PAY_PER_REQUEST,
  stream: StreamViewType.NEW_IMAGE
});
table.addGlobalSecondaryIndex({
  indexName: 'mpan-status-idex',
  partitionKey: { name: 'field1', type: ddb.AttributeType.STRING },
  sortKey:  { name: 'field2', type: ddb.AttributeType.STRING }
});
Run Code Online (Sandbox Code Playgroud)