【问题标题】:Sort DynamoDB data scan using Lambda使用 Lambda 对 DynamoDB 数据扫描进行排序
【发布时间】:2020-02-06 06:08:41
【问题描述】:

我在 DynamoDB 中创建了一个表,主键是一个存储为字符串的数字。当我使用 DynamoClient 的 GetItem 时,项目的顺序是随机的。

这是我的 Lambda 函数;

'use strict';
const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
    // Bring in the documentClient library
    const documentClient = new AWS.DynamoDB.DocumentClient(); 

    // instanialize 
    let responseBody = "";
    let statusCode = 0;

    // extract variables that we need to use in db
    // const { country, visits } = JSON.parse(event.body);

    const params = {
        TableName: "age_distribution"
    };

    try {
        const data = await documentClient.scan(params).promise(); 
        // add data into the db
        responseBody = JSON.stringify(data.Items); 
        // hold the response in a string type
        statusCode = 200;
    } catch (err) {
        responseBody = `Unable to get record: ${err}`;
        statusCode = 403;
    }

    const response = {
        statusCode: statusCode,
        headers: {
            "Content-Type" : " application/json",
            "access-control-allow-origin" : "*"
        },
        body: responseBody 
        //send back the responseBody
    };

    // return the response
    return response;
};

这是示例响应

[{"country":"63","visits":3},{"country":"92","visits":4},{"country":"64","visits":3},{"country":"90","visits":5},{"country":"79","visits":11},{"country":"66","visits":10},{"country":"80","visits":20},{"country":"62","visits":4},{"country":"87","visits":6},{"country":"75","visits":22},{"country":"76","visits":16},{"country":"97","visits":1},{"country":"84","visits":13},{"country":"60","visits":2},{"country":"61","visits":4},{"country":"89","visits":7},{"country":"94","visits":1},{"country":"98","visits":1},{"country":"82","visits":17},{"country":"77","visits":16},{"country":"91","visits":4},{"country":"81","visits":18},{"country":"88","visits":10},{"country":"96","visits":1},{"country":"71","visits":18},{"country":"95","visits":1},{"country":"78","visits":21},{"country":"83","visits":12},{"country":"68","visits":14},{"country":"65","visits":6},{"country":"70","visits":12},{"country":"85","visits":9},{"country":"69","visits":13},{"country":"74","visits":12},{"country":"93","visits":3},{"country":"86","visits":10},{"country":"67","visits":6},{"country":"72","visits":11},{"country":"73","visits":26}]

我想对项目进行排序,从 60 开始到 98 结束

【问题讨论】:

    标签: javascript amazon-dynamodb


    【解决方案1】:

    DynamoDb 中的排序是针对索引的排序键值进行的

    From the docs:

    查询结果始终按排序键值排序。如果排序键的数据类型为Number,则以数字顺序返回结果;否则,结果将按 UTF-8 字节的顺序返回。默认情况下,排序顺序是升序。要反转顺序,请将 ScanIndexForward 参数设置为 false

    所以您可能想要做的是创建一个 GSI(全局二级索引),其中包含对您有意义的排序键(范围)我经常在此索引定义中使用时间戳:

        GlobalSecondaryIndexes:
            - IndexName: productKey-timestamp-index
            KeySchema:
              - AttributeName: productKey
                KeyType: HASH
              - AttributeName: timestamp
                KeyType: RANGE
    

    然后,当我查询此索引时,我会根据时间戳获取数据升序系列。

    如果我希望它降序,那么我使用以下查询:

     let params = {
            TableName: ORDERS_TABLE,
            Limit: 10,
            IndexName: 'productKey-timestamp-index',
            Key: {
                "productKey": productKey
            },
            KeyConditionExpression: 'productKey= :productKey',
            ExpressionAttributeValues: {
                ':productKey': productKey
            },
            ScanIndexForward: false
        };
    
        db.query(params).then(console.log)
    

    注意:设置ScanIndexForward: false 会通过排序键反转顺序,即在这种情况下为timestamp

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多