Dynamodb-通过GSI使用python boto3进行查询

Dynamodb-通过GSI使用python boto3进行查询,python,python-2.7,amazon-dynamodb,boto,boto3,Python,Python 2.7,Amazon Dynamodb,Boto,Boto3,我正在使用python boto3与dynamodb一起工作。我使用以下脚本创建了一个表: cls.table = dynamodb.create_table( TableName='table-unittest', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH', },

我正在使用python boto3与dynamodb一起工作。我使用以下脚本创建了一个表:

 cls.table = dynamodb.create_table(
        TableName='table-unittest',
        KeySchema=[
            {
                'AttributeName': 'id',
                'KeyType': 'HASH',
            },
            {
                'AttributeName': 'user_name',
                'KeyType': 'RANGE',
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'id',
                'AttributeType': 'N',
            },
            {
                'AttributeName': 'user_name',
                'AttributeType': 'S',
            },
            {
                'AttributeName': 'age',
                'AttributeType': 'N',
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 2,
            'WriteCapacityUnits': 2,
        },
        GlobalSecondaryIndexes=[
            {
                'IndexName': 'age-index',
                'KeySchema': [
                    {
                        'AttributeName': 'age',
                        'KeyType': 'HASH',
                    },
                ],
                'Projection': {
                    'ProjectionType': 'KEYS_ONLY',
                },
                'ProvisionedThroughput': {
                    'ReadCapacityUnits': 2,
                    'WriteCapacityUnits': 2,
                }
            },
        ],
    )
但是,当通过表的
年龄索引
全局二级索引查询表时,我收到以下消息:

查询条件缺少关键架构元素:年龄

以下是我传递给boto3查询方法的参数:

{
'ConsistentRead': False,
'IndexName': 'age-index',
'QueryFilter': {
    'age': {
        'ComparisonOperator': 'GT',
        'AttributeValueList': [18]
    }
},

'TableName': 'table-unittest',
'ScanIndexForward': False,
'KeyConditions': {
    'id': {
        'ComparisonOperator': 'EQ',
        'AttributeValueList': [222]
    }
}

}

不能使用表哈希键作为条件(id)查询全局二级索引

您只能:

  • 仅查询全局二级索引(年龄)和应用程序级筛选器(按id)
  • 创建本地二级索引,其中哈希为'id',范围为'age',然后可以查询其中id=222和age=23