Lambda - 通过 Lambda 查询 DynamoDB
Lambda - Querying DynamoDB through Lambda
- 我有一个名为 Customers 的 table,其属性为 CustId(分区键)、Fname、Lname、Dob。
- 我使用以下参数在 Lname 上创建了一个名为 LastNameIndex 的二级索引:
{
TableName: 'Customers'
AttributeDefinitions: [
{
AttributeName: 'Lname',
AttributeType: 'S'
}
],
GlobalSecondaryIndexUpdates: [
{
Create: {
IndexName: "LastNameIndex",
KeySchema: [
{AttributeName: "Lname", KeyType: "HASH"}
],
Projection: {
"ProjectionType": "ALL"
},
ProvisionedThroughput: {
"ReadCapacityUnits": 1,"WriteCapacityUnits": 1
}
}
}
]
}
- Lambda 函数(片段)- 我想获取所有包含 Lname=Connors 的记录
params = {
TableName: "Customers",
IndexName: "LastNameIndex",
ExpressionAttributeNames: {
"#FN": "Fname",
"#LN": "Lname",
"#DB": "Dob",
},
ExpressionAttributeValues: {
":a": {
S: "Connors"
}
},
KeyConditionExpression: "Lname = :a",
ProjectionExpression: "#FN, #LN, #DB"
};
- 运行 查询
ddb.query(params).promise().then(
function(data) {
console.log("Customer:" + data.Item)
return data.Item;
},
function() {
console.log("No records found...")
}
);
- 我有一个 Lname = Connors 的记录。
- 但是查询 return 我没有任何记录 - 知道参数有什么问题吗?
query
操作 returns 多个项目而不是 getItem
之类的单个项目操作。所以我认为返回 data.Items 而不是 data.Item 应该可以正常工作。
ddb.query(params).promise().then(
function(data) {
console.log("Customer:" + data.Items)
return data.Items;
},
function() {
console.log("No records found...")
}
);
- 我有一个名为 Customers 的 table,其属性为 CustId(分区键)、Fname、Lname、Dob。
- 我使用以下参数在 Lname 上创建了一个名为 LastNameIndex 的二级索引:
{
TableName: 'Customers'
AttributeDefinitions: [
{
AttributeName: 'Lname',
AttributeType: 'S'
}
],
GlobalSecondaryIndexUpdates: [
{
Create: {
IndexName: "LastNameIndex",
KeySchema: [
{AttributeName: "Lname", KeyType: "HASH"}
],
Projection: {
"ProjectionType": "ALL"
},
ProvisionedThroughput: {
"ReadCapacityUnits": 1,"WriteCapacityUnits": 1
}
}
}
]
}
- Lambda 函数(片段)- 我想获取所有包含 Lname=Connors 的记录
params = {
TableName: "Customers",
IndexName: "LastNameIndex",
ExpressionAttributeNames: {
"#FN": "Fname",
"#LN": "Lname",
"#DB": "Dob",
},
ExpressionAttributeValues: {
":a": {
S: "Connors"
}
},
KeyConditionExpression: "Lname = :a",
ProjectionExpression: "#FN, #LN, #DB"
};
- 运行 查询
ddb.query(params).promise().then(
function(data) {
console.log("Customer:" + data.Item)
return data.Item;
},
function() {
console.log("No records found...")
}
);
- 我有一个 Lname = Connors 的记录。
- 但是查询 return 我没有任何记录 - 知道参数有什么问题吗?
query
操作 returns 多个项目而不是 getItem
之类的单个项目操作。所以我认为返回 data.Items 而不是 data.Item 应该可以正常工作。
ddb.query(params).promise().then(
function(data) {
console.log("Customer:" + data.Items)
return data.Items;
},
function() {
console.log("No records found...")
}
);