【发布时间】:2021-03-26 10:43:17
【问题描述】:
在许多AWS DynamoDB 数据中,我想使用node.js 搜索特定的键值数据。
我的数据库单键示例如下。
{
"device_id" : "abcde12345", // Primary Key, String
"setting": {
"left_motor": 30,
"right_motor": 30,
"motor_name": "STRING_WHAT_I_WANT_TO_CHECK"
},
"another_fields1" : "1234",
"another_fields2": {
"key1" : 1,
"key2" : "2",
"key3" : { ... }
}
}
我收集所有项目的motor_name的代码如下。
// require modules
var fs = require('fs'); // used for save json-result.
var AWS = require('aws-sdk');
var config = require('./config_file'); // secret data
var schedule = require('node-schedule');
AWS.config.update({
region: config.dynamodb.region,
endpoint: config.dynamodb.endpoint,
accessKeyId: config.dynamodb.access_key,
secretAccessKey: config.dynamodb.secret_key
});
var documentClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: config.dynamodb.device_table
};
// I set run once at 9 o'clock, but after do single-time, I kill pm2 process.
// (just for run once only)
var motor_name_list = {}; // result will be saved.
var j = schedule.scheduleJob("0 0 9 * * *", function() {
documentClient.scan(params, onScan);
});
function onScan(err, data) {
if(err) {
console.log(err);
}
else { // READ SUCCESS
/*
'single_item' sample is like upper json-code
But, I reference only one value, setting.motor_name
*/
data.Items.forEach(function(single_item) {
// process scan success datas
var motor_name = single_item.setting.motor_name;
if(motor_name_list.hasOwnProperty(motor_name))
motor_name_list[motor_name] += 1;
else
motor_name_list[motor_name] = 1;
});
if(typeof data.LastEvaluatedKey != 'undefined') {
params.ExclusiveStartKey = data.LastEvaluatedKey;
// Because of Read Limit per minute, I delay next-step for one minute.
// (maybe, ReadCapacityUnits)
setTimeout(function() {
documentClient.scan(params, onScan);
}, 60 * 1000);
}
else {
// save motor_name_list as a file (fs.writeFileSync...)
}
} // END OF READ SUCCESS
}
由于延迟,需要很多时间。 (实际上是 10~15 分钟)。
(单个onScan 读取 300~500 项,我有将近 5000 项。
有没有什么方法可以只扫描单个项目而不改变我的 json 格式?
感谢您阅读我的问题。
【问题讨论】:
标签: node.js amazon-web-services amazon-dynamodb