我有一个User表,并且我想使用以下查询来获取数据,出于教育目的,我使用的是类似SQL的查询。
SELECT * FROM User
WHERE (gender = "Male")
AND (age between 25-30 OR height between 5.4-5.9 OR city="India, US")
我正在尝试使用Node.js在AWS Lambda中创建上述查询
任何反馈将不胜感激。
答案 0 :(得分:3)
这是您使用awscli的方法:
aws dynamodb scan \
--table-name User \
--filter-expression "(gender = :gender) AND ((age BETWEEN :age1 AND :age2) OR (height BETWEEN :h1 AND :h2) OR (city = :city))" \
--expression-attribute-values '{":gender":{"S":"Male"}, ":age1":{"N":"25"}, ":age2":{"N":"30"}, ":h1":{"N":"5.4"}, ":h2":{"N":"5.9"}, ":city":{"S":"India, US"}}'
以下是使用低级AWS JavaScript SDK函数的方法:
const AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});
const ddb = new AWS.DynamoDB();
const params = {
TableName: 'User',
FilterExpression: '(gender = :gender) AND ((age BETWEEN :age1 AND :age2) OR (height BETWEEN :h1 AND :h2) OR (city = :city))',
ExpressionAttributeValues: {
':gender': {S: 'Male'},
':age1': {N: '25'},
':age2': {N: '30'},
':h1': {N: '5.4'},
':h2': {N: '5.9'},
':city' : {S: 'India, US'},
},
};
ddb.scan(params, (err, data) => {
if (err) {
console.log('Error:', err);
} else {
for (const item of data.Items) {
console.log(item);
};
}
});
最后,这是使用高级DynamoDB DocumentClient的方法,这使属性映射更加简单:
const AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});
const dc = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'User',
FilterExpression: '(gender = :gender) AND ((age BETWEEN :age1 AND :age2) OR (height BETWEEN :h1 AND :h2) OR (city = :city))',
ExpressionAttributeValues: {
':gender': 'Male',
':age1': 25,
':age2': 30,
':h1': 5.4,
':h2': 5.9,
':city': 'India, US',
}
};
dc.scan(params, (err, data) => {
if (err) {
console.log('Error:', err);
} else {
for (const item of data.Items) {
console.log(item);
};
}
});
请注意,这些是表scans,因此表中的所有项目都将被访问。