如何使用 DocumentClient.get 在 Amazons DynamoDB 中通过非主键属性查询 table
How to query table by non primary key attribute in Amazons DynamoDB with DocumentClient.get
我似乎在使用 AWS.DynamoDB.DocumentClient().get().
查询数据时遇到了很多困难
我正在使用无服务器并使用此架构设置我的 serverless.yml
:
resources:
Resources:
ShortUrlsTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: longUrl
AttributeType: S
- AttributeName: shortPath
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: longUrlIndex
KeySchema:
- AttributeName: longUrl
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
Projection:
ProjectionType: ALL
- IndexName: shortPathIndex
KeySchema:
- AttributeName: shortPath
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
我想要做的是使用 longUrl
或 shortPath
.
在数据库中搜索 shortUrlItem
到目前为止我已经设置好了:
dynamoDb = new AWS.DynamoDB.DocumentClient()
app.get("/:longUrl", (req, res) => {
const {longUrl} = req.params
const getParams = {
TableName: SHORT_URLS_TABLE,
Key: {longUrl},
}
dynamoDb.get(getParams, (error, result) => {
res.send({...error, ...result})
})
})
我似乎得到的只是返回给我的错误消息:
"message":"The provided key element does not match the schema","code":"ValidationException","time":"2018-08-17T20:39:27.765Z","requestId":"4RKNVG7ET1ORVF10H71M7AUABRVV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":21.513795782119505,"TableName":"short-urls-table-dev"
我似乎无法确定我是否正确查询或正确设置我的架构以使二级索引成为我 table 中的可搜索键。
我可以看到两个错误
1:你的getParams是错误的。您在 PK 上发出 get
请求,但您在参数部分提供了 GSI 密钥。应该是
const getParams = {
TableName: SHORT_URLS_TABLE,
Key: {
id: id, // Because id is the attribute of your HASH key.
}
}
这是错误的原因。您的哈希键不在属性 longUrl 上。
2: 无论如何,你不能在 GSI 上发出 get
请求。它没有 GSI 的设计。 GSI 不强制唯一性,因此同一个 GSI 哈希键中可以有多个项目,因此您只能 query
而不是 get
。
您正在尝试做的事情类似于
const queryParams = {
TableName: SHORT_URLS_TABLE,
IndexName: 'longUrlIndex',
KeyConditionExpression: 'longUrl = :longUrlValue',
ExpressionAttributeValues: {
'longUrlValue': longUrl
}
};
dynamoDb.query(queryParams, (error, result) => {
res.send({...error, ...result})
})
我似乎在使用 AWS.DynamoDB.DocumentClient().get().
查询数据时遇到了很多困难我正在使用无服务器并使用此架构设置我的 serverless.yml
:
resources:
Resources:
ShortUrlsTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: longUrl
AttributeType: S
- AttributeName: shortPath
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: longUrlIndex
KeySchema:
- AttributeName: longUrl
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
Projection:
ProjectionType: ALL
- IndexName: shortPathIndex
KeySchema:
- AttributeName: shortPath
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
我想要做的是使用 longUrl
或 shortPath
.
shortUrlItem
到目前为止我已经设置好了:
dynamoDb = new AWS.DynamoDB.DocumentClient()
app.get("/:longUrl", (req, res) => {
const {longUrl} = req.params
const getParams = {
TableName: SHORT_URLS_TABLE,
Key: {longUrl},
}
dynamoDb.get(getParams, (error, result) => {
res.send({...error, ...result})
})
})
我似乎得到的只是返回给我的错误消息:
"message":"The provided key element does not match the schema","code":"ValidationException","time":"2018-08-17T20:39:27.765Z","requestId":"4RKNVG7ET1ORVF10H71M7AUABRVV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":21.513795782119505,"TableName":"short-urls-table-dev"
我似乎无法确定我是否正确查询或正确设置我的架构以使二级索引成为我 table 中的可搜索键。
我可以看到两个错误
1:你的getParams是错误的。您在 PK 上发出 get
请求,但您在参数部分提供了 GSI 密钥。应该是
const getParams = {
TableName: SHORT_URLS_TABLE,
Key: {
id: id, // Because id is the attribute of your HASH key.
}
}
这是错误的原因。您的哈希键不在属性 longUrl 上。
2: 无论如何,你不能在 GSI 上发出 get
请求。它没有 GSI 的设计。 GSI 不强制唯一性,因此同一个 GSI 哈希键中可以有多个项目,因此您只能 query
而不是 get
。
您正在尝试做的事情类似于
const queryParams = {
TableName: SHORT_URLS_TABLE,
IndexName: 'longUrlIndex',
KeyConditionExpression: 'longUrl = :longUrlValue',
ExpressionAttributeValues: {
'longUrlValue': longUrl
}
};
dynamoDb.query(queryParams, (error, result) => {
res.send({...error, ...result})
})