Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法使用python在cdk中为dynamodb添加二级索引_Python 3.x_Amazon Dynamodb_Aws Cdk - Fatal编程技术网

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

Python 3.x 无法使用python在cdk中为dynamodb添加二级索引,python-3.x,amazon-dynamodb,aws-cdk,Python 3.x,Amazon Dynamodb,Aws Cdk,我正在尝试创建一个dynamoDB表,其中包含一个带有分区和排序键的二级索引。 我可以创建没有辅助索引的表,但是还没有找到添加辅助索引的方法 我已经查看了这两个资源,但没有发现任何东西能够实际显示我需要在cdk python脚本中使用什么代码来创建具有二级索引的资源 这是创建表的代码 table_name = 'event-table-name' event_table = dynamoDB.Table(self, 'EventsTable', table_name=

我正在尝试创建一个dynamoDB表,其中包含一个带有分区和排序键的二级索引。 我可以创建没有辅助索引的表,但是还没有找到添加辅助索引的方法

我已经查看了这两个资源,但没有发现任何东西能够实际显示我需要在cdk python脚本中使用什么代码来创建具有二级索引的资源

这是创建表的代码

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,
                                 )
这是我需要附加的第二个索引

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

我尝试在表创建中添加块,并尝试在表上调用addSecondaryindex方法。但是,如果说意外关键字或对象没有属性addGlobalSecondaryIndex,这两种方法都会失败。您是否尝试使用中的addGlobalSecondaryIndex方法

event_table.addGlobalSecondaryIndex({indexName: "...", partitionKey: "...", ...})

查看该方法的示例。

您是否尝试过使用中的addGlobalSecondaryIndex方法

event_table.addGlobalSecondaryIndex({indexName: "...", partitionKey: "...", ...})

请查看方法的。应在Table类上调用addGlobalSecondaryIndex

typescript中的以下代码非常适合我:

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 }
});

应在表类上调用addGlobalSecondaryIndex

typescript中的以下代码非常适合我:

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 }
});

对于任何通过谷歌搜索寻找并在上面绊倒的人:

使用通常的方法创建表格:

from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk.aws_dynamodb import Attribute, AttributeType, ProjectionType

table = dynamodb.Table(self, 'tableID',
                    partition_key=Attribute(name='partition_key', type = AttributeType.STRING))
然后以大致相同的方式添加全局二级索引:

table.add_global_secondary_index( 
           partition_key=Attribute(name='index_hash_key', type=AttributeType.NUMBER),
           sort_key=Attribute(name='range_key', type=AttributeType.STRING),
           index_name='some_index')
可以使用以下参数添加投影属性:

projection_type = ProjectionType.INCLUDE,
non_key_attributes= ['list', 'of', 'attribute','names']
如果不包括投影类型,则默认为“全部”

我知道这些文件在很多方面都不完整,但在这里可以找到:


对于任何通过谷歌搜索寻找并在上面绊倒的人:

使用通常的方法创建表格:

from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk.aws_dynamodb import Attribute, AttributeType, ProjectionType

table = dynamodb.Table(self, 'tableID',
                    partition_key=Attribute(name='partition_key', type = AttributeType.STRING))
然后以大致相同的方式添加全局二级索引:

table.add_global_secondary_index( 
           partition_key=Attribute(name='index_hash_key', type=AttributeType.NUMBER),
           sort_key=Attribute(name='range_key', type=AttributeType.STRING),
           index_name='some_index')
可以使用以下参数添加投影属性:

projection_type = ProjectionType.INCLUDE,
non_key_attributes= ['list', 'of', 'attribute','names']
如果不包括投影类型,则默认为“全部”

我知道这些文件在很多方面都不完整,但在这里可以找到:


这只是返回AttributeError:“Table”对象没有属性“AddGlobalSecondaryIndex”这只是返回AttributeError:“Table”对象没有属性“AddGlobalSecondaryIndex”我正在寻找python解决方案。但是简单地思考python并没有得到正确的支持,我可能需要简单地使用typescriptversion@Matt-简单地说,Python使用下划线命名约定,因此必须使用add_global_secondary_索引,而不是add GLOBALSecondary索引。它工作得很好。我正在寻找python解决方案。但是简单地思考python并没有得到正确的支持,我可能需要简单地使用typescriptversion@Matt-简单地说,Python使用下划线命名约定,因此必须使用add_global_secondary_索引,而不是add GLOBALSecondary索引。它工作得很好。