全局二级索引上的查询是否缺少表的分区键?

huangapple go评论131阅读模式
英文:

Query on GlobalSecondaryIndex is missing table partition key?

问题

我有一个带有keySchema的表,其中对象为HASH,id为RANGE,并且有一个名为dateIndex的GlobalSecondaryIndex,其keySchema为artificialPK为HASH,date为RANGE。

我可以使用对象和id查询表,但当我使用日期和artificialPK查询GSI时,我收到了一个ValidationException,其中缺少关键模式元素'object'。但这不是索引keySchema的一部分。

我如何查询GSI?所有的文档和示例似乎只使用GSI中定义的keySchema,但对我来说不起作用。我是否漏掉了一些明显的东西?

编辑:不知何故,GSI的keySchema与下面的create_table命令中声明的不同,因此出现了问题。

英文:

I have a table with a keySchema of object as HASH and id as RANGE, and a GlobalSecondaryIndex called dateIndex with a keySchema of artificialPK as HASH, date as RANGE

I can query the table using object and id just fine, but when I query the GSI using date and artificialPK, I get a ValidationException of a missing key schema element 'object'. But that's not part of the index keySchema.

How do I query a GSI? All the docs & examples seem to use just the keySchema defined in the GSI, but that's not working for me. Am I missing something obvious?

edit: somehow the GSI has been declared with a different keyschema from the create_table command below, hence the problem.

>>> ddb_resource.create_table(
                TableName=self.table_name,
                KeySchema=[
                    {'AttributeName': 'object', 'KeyType': 'HASH'},
                    {'AttributeName': 'id', 'KeyType': 'RANGE'}
                ],
                AttributeDefinitions=[
                    {'AttributeName': 'object', 'AttributeType': 'S'},
                    {'AttributeName': 'id', 'AttributeType': 'S'},
                    {'AttributeName': 'artificialPK', 'AttributeType': 'N'},
                    {'AttributeName': 'date', 'AttributeType': 'N'}
                ],
                GlobalSecondaryIndexes=[
                    {
                        'IndexName': 'dateIndex',
                        'KeySchema': [
                            {'AttributeName': 'artificialPK', 'KeyType': 'HASH'},
                            {'AttributeName': 'date', 'KeyType': 'RANGE'}
                        ],
                        'Projection': {
                            'ProjectionType': 'ALL'
                        },
                    }
                ]
            )

>>> resp = self.table.query(KeyConditionExpression=Key('object').eq('foo') & Key('id').gt('0'))
>>> len(resp['Items'])
123

>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('date').gt(0) & Key('artificialPK').eq('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: object

>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('object').eq('foo') & Key('id').gt('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: date

>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('date').gt(0) & Key('object').eq('foo') & Key('id').gt('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Conditions can be of length 1 or 2 only

</details>


# 答案1
**得分**: 0

第一个应该可以工作,对我来说看起来很完美。

您确定您正在访问表格,并且索引包含您期望的键,请检查`DescribeTable`输出。

<details>
<summary>英文:</summary>

The first one should work, it looks perfect to me. 

Are you sure you&#39;re accessing the table and that the index that has the keys that you expect, check the `DescribeTable` output.

</details>



huangapple
  • 本文由 发表于 2023年7月13日 17:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定