【问题标题】:Dynamodb - query by GSI using python boto3Dynamodb - GSI 使用 python boto3 查询
【发布时间】:2016-02-16 14:07:50
【问题描述】:

我正在使用 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,
                }
            },
        ],
    )

但是,当通过其age-index 全局二级索引查询表时,我收到以下消息:

Query condition missed key schema element: age

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

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

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

}

【问题讨论】:

    标签: python python-2.7 amazon-dynamodb boto boto3


    【解决方案1】:

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

    你只能:

    1. 仅在您的全局二级索引(年龄)和应用级过滤器中按 id 查询
    2. 创建本地二级索引,其中hash为'id',范围为'age',然后可以查询其中id=222和age =23

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      相关资源
      最近更新 更多