Amazon dynamodb DynamoDB查询使用二级索引,如何使用不同的键进行查询
我使用的是带有dynamodb的无服务器框架[在本地]。 正在尝试使用辅助索引字段进行查询。 我们的目标是像Mongo:Amazon dynamodb DynamoDB查询使用二级索引,如何使用不同的键进行查询,amazon-dynamodb,serverless-framework,dynamodb-queries,dynamo-local,amazon-dynamodb-index,Amazon Dynamodb,Serverless Framework,Dynamodb Queries,Dynamo Local,Amazon Dynamodb Index,我使用的是带有dynamodb的无服务器框架[在本地]。 正在尝试使用辅助索引字段进行查询。 我们的目标是像Mongo:{url:'}中的基本查找查询那样使用很少的键进行查询,或者可能是这样的{url:,ha:} 我当前使用的表配置: serverless.yml resources: Resources: TableName: Type: 'AWS::DynamoDB::Table' Properties: TableName: ${file
{url:'}
中的基本查找查询那样使用很少的键进行查询,或者可能是这样的{url:,ha:}
我当前使用的表配置:
serverless.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
字段搜索
Q:
1> 需要对不同字段的查询进行哪些更改?[这是二级索引,在查询中不使用id
)
2> 如何使用多个属性进行搜索?[即:{url:,ha:}
]
我正在使用的查询:
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
});
- 您已经使用了GSI,它允许您使用辅助索引。但是您应该使用
而不是query
,它允许您查询具有复合主键(分区键和排序键)的任何表或辅助索引get
- 只需使用
和过滤器表达式
,它支持u使用多个条件表达式属性值
var参数={
表名:“”,
IndexName:'URI_1',
KeyConditionExpression:'url=:url',
FilterExpression:'ha=:ha',
表达式属性值:{
“:url”:”http://something.com',
“:哈”:“aaaa”
}
};
查询(参数,函数(错误,数据){
if(err)console.log(err);//发生错误
else console.log(数据);//响应成功
});
抓住了重点,但是如果我只有url
并找到所有包含此url
的文档,该怎么办?我在ConditionExpression
中遇到错误,所以我使用了KeyConditionExpression
,但遇到了这个错误:查询条件缺少关键架构元素
,我猜这是因为没有传递主键。它在我的情况下有效。我还没有找到失败的原因,可以肯定的是,你已经使用了GSI,它允许你在没有主键的情况下使用二级索引。顺便问一下,你的DynamoDB API版本是什么?我猜2012-08-10
,无论如何,这只在我传递url
时起作用,但是如果我现在在答案中也传递了:ha
,其抛出错误查询键条件不受支持
{
message:"One of the required keys was not given a value",
code:"ValidationException,
...
}