使用单独的索引 table 和 Node.js 从 dynamodb table 获取项目
Get item from dynamodb table using separate index table with Node.js
我正在使用 Particle Electron 和 AWS 创建一个气象站。我已经设法将返回的数据发送到 DynamoDB table "weather",其中包含具有以下模式的所有天气数据(包含样本值):
Item{13}
deviceId: 540056000a51343334363138 (String) (Primary Partition Key)
tm: 1458754711 (Number) (Primary Sort Key)
batSoC: 89 (String)
batV: 4.01 (String)
hum: 27.9 (String)
lat: 41.2083 (String)
lon: -73.3439 (String)
pres: 968.4 (String)
temp: 19.8 (String)
uvI: 0.1 (String)
wDir: 0 (String)
wGst: 0.0 (String)
wSpd: 0.0 (String)
以及一个单独的 "weather_index" table,它仅包含 deviceId
和 tm
属性,用于写入主 [=56] 的最新数据=](有点像原子计数器,但用于定期更新的 unix 时间戳值)。因此,如果上面的 "weather_index" 项目是最新条目,则 "weather_index" table 中的项目将如下所示:
Item{2}
deviceIdString: 540056000a51343334363138 (String) (Primary Partition Key)
tmNumber: 1458754711 (Number)
我目前正在尝试在 Node.js 中编写一个非常基本的 Web 前端(在这个项目之前,我没有任何经验,所以我仍在学习)并且不知道如何至:
- 执行 DynamoDB getItem,其中包含通过先前的 getItem 检索的参数。喜欢:
latestTime = getItem(weather_index, deviceId) // 获取最近一次天气观测的时间"tm",存入"latestTime"
// 其中 "weather_index" 是 table 名称
currentWeather = getItem(deviceId, tm) // 获取指定 "tm" 值的天气观测值并将其存储在 "currentWeather" 中
// 其中 "tm" 是最近观察的 unix 时间戳
然后我希望能够将各个值打印到 terminal/webpage/carrier pigeon/etc...(类似于 currentWeather.deviceId
、currentWeather.tm
、currentWeather.batSoC
,等等...
我有以下无法正常工作的代码:
/*
* Module dependencies
*/
var AWS = require('aws-sdk')
// weathermon_dev credentials
AWS.config.update({accessKeyId: 'REDACTED for obvious reasons', secretAccessKey: 'This bit too'});
// Select AWS region
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();
// db.listTables(function(err,data) {
// console.log(data.TableNames);
// });
var time = Date.now() / 1000;
time = Math.round(time);
//console.log("Time: ");
//console.log(time);
time = Math.round(time);
var deviceId = "540056000a51343334363138"
var params = {
Key: {
deviceId: {S: deviceId}
},
TableName: 'weather_index'
};
var timeJson;
db.getItem(params, function(err,data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
var timeJson = JSON.parse(data);
})
// var timeJson = JSON.parse(data);
// var itemTime = timeJson.item;
console.log("timeJSON: " + timeJson);
// console.log("itemTime: " + itemTime);
var params = {
Key: {
deviceId: {S: deviceId},
time: {N: 'tm'}
},
TableName: 'weather'
};
db.getItem(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
})
如有任何帮助,我们将不胜感激。
您需要了解 NodeJS 异步调用的工作原理。在执行第二个 getItem()
.
之前,您需要等到第一个 getItem()
的回调被调用
我在这里重写了代码的相关部分,以向您展示我在说什么,但我建议您尝试理解为什么需要以这种方式编写代码,而不仅仅是 copy/pasting它。
var deviceId = "540056000a51343334363138"
var params = {
Key: {
deviceId: {S: deviceId}
},
TableName: 'weather_index'
};
var timeJson;
db.getItem(params, function(err,data) {
if (err) console.log(err); // an error occurred
else {
console.log(data); // successful response
var timeJson = JSON.parse(data);
console.log("timeJSON: " + timeJson);
// Inside this callback we have the weather_index tm value,
// so query the weather table here.
var params = {
Key: {
deviceId: {S: deviceId},
time: {N: 'tm'}
},
TableName: 'weather'
};
db.getItem(params, function(err, data) {
if (err) console.log(err); // an error occurred
else {
console.log(data); // successful response
// TODO: Use the database response data here
}
});
}
});
我正在使用 Particle Electron 和 AWS 创建一个气象站。我已经设法将返回的数据发送到 DynamoDB table "weather",其中包含具有以下模式的所有天气数据(包含样本值):
Item{13}
deviceId: 540056000a51343334363138 (String) (Primary Partition Key)
tm: 1458754711 (Number) (Primary Sort Key)
batSoC: 89 (String)
batV: 4.01 (String)
hum: 27.9 (String)
lat: 41.2083 (String)
lon: -73.3439 (String)
pres: 968.4 (String)
temp: 19.8 (String)
uvI: 0.1 (String)
wDir: 0 (String)
wGst: 0.0 (String)
wSpd: 0.0 (String)
以及一个单独的 "weather_index" table,它仅包含 deviceId
和 tm
属性,用于写入主 [=56] 的最新数据=](有点像原子计数器,但用于定期更新的 unix 时间戳值)。因此,如果上面的 "weather_index" 项目是最新条目,则 "weather_index" table 中的项目将如下所示:
Item{2}
deviceIdString: 540056000a51343334363138 (String) (Primary Partition Key)
tmNumber: 1458754711 (Number)
我目前正在尝试在 Node.js 中编写一个非常基本的 Web 前端(在这个项目之前,我没有任何经验,所以我仍在学习)并且不知道如何至:
- 执行 DynamoDB getItem,其中包含通过先前的 getItem 检索的参数。喜欢:
latestTime = getItem(weather_index, deviceId) // 获取最近一次天气观测的时间"tm",存入"latestTime" // 其中 "weather_index" 是 table 名称
currentWeather = getItem(deviceId, tm) // 获取指定 "tm" 值的天气观测值并将其存储在 "currentWeather" 中 // 其中 "tm" 是最近观察的 unix 时间戳
然后我希望能够将各个值打印到 terminal/webpage/carrier pigeon/etc...(类似于 currentWeather.deviceId
、currentWeather.tm
、currentWeather.batSoC
,等等...
我有以下无法正常工作的代码:
/*
* Module dependencies
*/
var AWS = require('aws-sdk')
// weathermon_dev credentials
AWS.config.update({accessKeyId: 'REDACTED for obvious reasons', secretAccessKey: 'This bit too'});
// Select AWS region
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();
// db.listTables(function(err,data) {
// console.log(data.TableNames);
// });
var time = Date.now() / 1000;
time = Math.round(time);
//console.log("Time: ");
//console.log(time);
time = Math.round(time);
var deviceId = "540056000a51343334363138"
var params = {
Key: {
deviceId: {S: deviceId}
},
TableName: 'weather_index'
};
var timeJson;
db.getItem(params, function(err,data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
var timeJson = JSON.parse(data);
})
// var timeJson = JSON.parse(data);
// var itemTime = timeJson.item;
console.log("timeJSON: " + timeJson);
// console.log("itemTime: " + itemTime);
var params = {
Key: {
deviceId: {S: deviceId},
time: {N: 'tm'}
},
TableName: 'weather'
};
db.getItem(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
})
如有任何帮助,我们将不胜感激。
您需要了解 NodeJS 异步调用的工作原理。在执行第二个 getItem()
.
getItem()
的回调被调用
我在这里重写了代码的相关部分,以向您展示我在说什么,但我建议您尝试理解为什么需要以这种方式编写代码,而不仅仅是 copy/pasting它。
var deviceId = "540056000a51343334363138"
var params = {
Key: {
deviceId: {S: deviceId}
},
TableName: 'weather_index'
};
var timeJson;
db.getItem(params, function(err,data) {
if (err) console.log(err); // an error occurred
else {
console.log(data); // successful response
var timeJson = JSON.parse(data);
console.log("timeJSON: " + timeJson);
// Inside this callback we have the weather_index tm value,
// so query the weather table here.
var params = {
Key: {
deviceId: {S: deviceId},
time: {N: 'tm'}
},
TableName: 'weather'
};
db.getItem(params, function(err, data) {
if (err) console.log(err); // an error occurred
else {
console.log(data); // successful response
// TODO: Use the database response data here
}
});
}
});