我正在使用带有 dynamodb [在本地] 的无服务器框架。尝试使用二级索引字段进行查询。目标是像我们在 Mongo 中的基本查找查询中那样使用很少的键进行查询:{url:'<Somevalue>'}
或者可能是这样{url:<somevalue>,ha:<somevalue>}
我目前使用的表配置:
无服务器.yml
resources:
Resources:
TableName:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: ${file(./serverless.js):Tables.TableName.name}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: 'id'
AttributeType: 'S'
- AttributeName: 'url'
AttributeType: 'S'
- AttributeName: 'ha'
AttributeType: 'S'
- AttributeName: 'GSI_1_PK'
AttributeType: 'S'
- AttributeName: 'GSI_1_SK'
AttributeType: 'S'
KeySchema:
- AttributeName: 'id'
KeyType: 'HASH'
GlobalSecondaryIndexes:
- IndexName: 'GSI_1'
KeySchema:
- AttributeName: 'GSI_1_PK'
KeyType: 'HASH'
- AttributeName: 'GSI_1_SK'
KeyType: 'RANGE'
Projection:
ProjectionType: 'ALL'
- IndexName: 'URI_1'
KeySchema:
- AttributeName: 'url'
KeyType: 'HASH'
Projection:
ProjectionType: 'ALL'
- IndexName: 'HASH_1'
KeySchema:
- AttributeName: 'ha'
KeyType: 'HASH'
Projection:
ProjectionType: 'ALL'
Outputs:
TableNameARN:
Value: { 'Fn::GetAtt': [TableName, Arn] }
Export:
Name: ${file(./serverless.js):Exports.TableNameARN}
有了这个,目前我只能用id
字段搜索,
问:
1> 使用不同字段进行查询需要进行哪些更改?[这是二级索引,没有id
在查询中使用]
2> 如何搜索多个属性?[即:{url:<somevalue>,ha:<somevalue>}
]
我正在使用的查询:
var params = {
TableName: '<TableName>',
IndexName:'URI_1',
Key: {
url: 'http://something.com'
}
};
docClient.get(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
查询的输出:
{
message:"One of the required keys was not given a value",
code:"ValidationException,
...
}