我有传感器每4秒向DynamoDB表发送数据,我想只检索特定传感器的最新值。表中的主键是传感器ID和SysDt时间戳。我已指定一个索引,将传感器ID用作分区键,使用GWID(网关ID)作为排序键。我无法仅从表中检索最新值。
这是我的剧本:
AWS.config.update({
region: "us-west-2",
endpoint: 'https://dynamodb.us-west-2.amazonaws.com',
accessKeyId: "Fakeaccesskey",
secretAccessKey: "Fakesecretkey"
});
var docClient = new AWS.DynamoDB.DocumentClient();
function readItemtesttwovalues() {
var table = "TA_Log1";
var indexidgwid = "ID-GWID-index";
var ID = "BB00000013";
var GWID = "AA00000001";
var params = {
TableName: table,
IndexName: indexidgwid,
Key:{
"ID": ID,
"GWID": GWID
},
ScanIndexForward: false,
Limit: 1
};
docClient.query(params, function(err, data) {
if (err) {
document.getElementById('testtwostatus').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('testtwostatus').innerHTML = data.Item.Status;
document.getElementById('testtwotemp').innerHTML = data.Item.Temp;
document.getElementById('testtwotempmap').innerHTML = data.Item.Temp;
document.getElementById('testtwohum').innerHTML = data.Item.Hum;
document.getElementById('testtwohummap').innerHTML = data.Item.Hum;
document.getElementById('testtwobat').innerHTML = data.Item.BatV;
document.getElementById('testtwobatmap').innerHTML = data.Item.BatV;
var temp = Number(data.Item.Temp);
}
});
}
我已阅读this,但我无法弄清楚如何去做。
我感谢你的帮助。
答案 0 :(得分:0)
我能够弄清楚,所以我想把它张贴在这里给任何寻找答案的人。
我定义params
的方式不正确。这是有效的Javascript代码:
AWS.config.update({
region: "us-west-2",
endpoint: 'https://dynamodb.us-west-2.amazonaws.com',
accessKeyId: "Fakeaccesskey",
secretAccessKey: "Fakesecretkey"
});
var docClient = new AWS.DynamoDB.DocumentClient();
function readItemtesttwovalues() {
var params = {
TableName: "TA_Log1",
IndexName: "ID-GWID-index",
KeyConditionExpression: "ID = :id and GWID = :gwid",
ExpressionAttributeValues: {
":id": "BB00000013",
":gwid": "AA00000001"
},
ScanIndexForward: false,
Limit: 1
};
docClient.query(params, function(err, data) {
if (err) {
document.getElementById('testtwostatus').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('testtwostatus').innerHTML = data.Item.Status;
document.getElementById('testtwotemp').innerHTML = data.Item.Temp;
document.getElementById('testtwotempmap').innerHTML = data.Item.Temp;
document.getElementById('testtwohum').innerHTML = data.Item.Hum;
document.getElementById('testtwohummap').innerHTML = data.Item.Hum;
document.getElementById('testtwobat').innerHTML = data.Item.BatV;
document.getElementById('testtwobatmap').innerHTML = data.Item.BatV;
var temp = Number(data.Item.Temp);
}
});
}