代码之家  ›  专栏  ›  技术社区  ›  Hamza Ahmad

从DynamoDB表中获取最新值

  •  0
  • Hamza Ahmad  · 技术社区  · 6 年前

    我让传感器每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 但我不知道该怎么做。

    我感谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hamza Ahmad    6 年前

    我能找到答案,所以我想我会把它贴在这里,让任何人都能找到答案。

    我定义 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);
            }
        });
    }