简体   繁体   English

Dynamodb-通过GSI使用python boto3查询

[英]Dynamodb - query by GSI using python boto3

I`m using python boto3 to work with dynamodb. 我正在使用python boto3与dynamodb一起工作。 I've created a table using the following script: 我使用以下脚本创建了一个表:

 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,
                }
            },
        ],
    )

But, when querying the table by its age-index global secondary index, i receive the following message: 但是,当通过age-index全局二级索引查询表时,我收到以下消息:

Query condition missed key schema element: age

Here is the params i pass to the boto3 query method: 这是我传递给boto3查询方法的参数:

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

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

} }

you cant query your global secondary index using your table hash key as conditions (id) 您不能使用表哈希键作为条件(id)查询全局二级索引

you only can: 您只能:

  1. Query only on your global secondary index (age) and in application level filter by id 仅查询您的全局二级索引(年龄)和应用程序级别的ID过滤器
  2. create local secondary index where hash is 'id' and range is 'age', then you can query where id=222 and age =23 创建哈希为“ id”且范围为“ age”的本地二级索引,然后可以查询id = 222和age = 23的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM