查询字段未定义的项目的全局二级索引
Query global secondary index for items where field is undefined
我有这个 table test1
和散列键 s1
和范围键 s2
并且 table 有一个全局二级索引 gsi1
使用散列键 s3
和范围键 s4
.
$ aws dynamodb describe-table \
--table-name test1
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "s1",
"AttributeType": "S"
},
{
"AttributeName": "s2",
"AttributeType": "S"
},
{
"AttributeName": "s3",
"AttributeType": "S"
},
{
"AttributeName": "s4",
"AttributeType": "S"
}
],
"TableName": "test1",
"KeySchema": [
{
"AttributeName": "s1",
"KeyType": "HASH"
},
{
"AttributeName": "s2",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2020-11-19T23:36:40.457000+08:00",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T08:00:00+08:00",
"LastDecreaseDateTime": "1970-01-01T08:00:00+08:00",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 3
},
"TableSizeBytes": 50,
"ItemCount": 3,
"TableArn": "arn:aws:dynamodb:ap-southeast-1:000000000000:table/test1",
"GlobalSecondaryIndexes": [
{
"IndexName": "gsi1",
"KeySchema": [
{
"AttributeName": "s3",
"KeyType": "HASH"
},
{
"AttributeName": "s4",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 3
},
"IndexSizeBytes": 37,
"ItemCount": 1,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test1/index/gsi1"
}
]
}
}
我有这个项目。请注意 s4
不存在。
$ aws dynamodb get-item \
--table-name test1 \
--key '{"s1":{"S":"chen"}, "s2":{"S":"yan"}}'
{
"Item": {
"s3": {
"S": "fei"
},
"s1": {
"S": "chen"
},
"s2": {
"S": "yan"
}
}
}
我尝试使用此查询查询该项目的索引 gsi1
,但出现以下错误。
$ aws dynamodb query \
--table-name test1 \
--index-name gsi1 \
--expression-attribute-values '{
":s3": {"S": "fei"},
":s4": {"NULL": true}
}' \
--key-condition-expression 's3 = :s3 AND s4 = :s4'
An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type
我也试过这个查询,但没有得到任何项目。
$ aws dynamodb query \
--table-name test1 \
--index-name gsi1 \
--expression-attribute-values '{
":s3": {"S": "fei"}
}' \
--key-condition-expression 's3 = :s3'
{
"Items": [],
"Count": 0,
"ScannedCount": 0,
"ConsumedCapacity": null
}
知道如何查询该项目的索引吗?
你不能。
如果项目没有 GSI 密钥所需的属性,则不会将其添加到 GSI。
这被称为 "sparse index"
我有这个 table test1
和散列键 s1
和范围键 s2
并且 table 有一个全局二级索引 gsi1
使用散列键 s3
和范围键 s4
.
$ aws dynamodb describe-table \
--table-name test1
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "s1",
"AttributeType": "S"
},
{
"AttributeName": "s2",
"AttributeType": "S"
},
{
"AttributeName": "s3",
"AttributeType": "S"
},
{
"AttributeName": "s4",
"AttributeType": "S"
}
],
"TableName": "test1",
"KeySchema": [
{
"AttributeName": "s1",
"KeyType": "HASH"
},
{
"AttributeName": "s2",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2020-11-19T23:36:40.457000+08:00",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T08:00:00+08:00",
"LastDecreaseDateTime": "1970-01-01T08:00:00+08:00",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 3
},
"TableSizeBytes": 50,
"ItemCount": 3,
"TableArn": "arn:aws:dynamodb:ap-southeast-1:000000000000:table/test1",
"GlobalSecondaryIndexes": [
{
"IndexName": "gsi1",
"KeySchema": [
{
"AttributeName": "s3",
"KeyType": "HASH"
},
{
"AttributeName": "s4",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 3
},
"IndexSizeBytes": 37,
"ItemCount": 1,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test1/index/gsi1"
}
]
}
}
我有这个项目。请注意 s4
不存在。
$ aws dynamodb get-item \
--table-name test1 \
--key '{"s1":{"S":"chen"}, "s2":{"S":"yan"}}'
{
"Item": {
"s3": {
"S": "fei"
},
"s1": {
"S": "chen"
},
"s2": {
"S": "yan"
}
}
}
我尝试使用此查询查询该项目的索引 gsi1
,但出现以下错误。
$ aws dynamodb query \
--table-name test1 \
--index-name gsi1 \
--expression-attribute-values '{
":s3": {"S": "fei"},
":s4": {"NULL": true}
}' \
--key-condition-expression 's3 = :s3 AND s4 = :s4'
An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type
我也试过这个查询,但没有得到任何项目。
$ aws dynamodb query \
--table-name test1 \
--index-name gsi1 \
--expression-attribute-values '{
":s3": {"S": "fei"}
}' \
--key-condition-expression 's3 = :s3'
{
"Items": [],
"Count": 0,
"ScannedCount": 0,
"ConsumedCapacity": null
}
知道如何查询该项目的索引吗?
你不能。
如果项目没有 GSI 密钥所需的属性,则不会将其添加到 GSI。
这被称为 "sparse index"