【问题标题】:How to decode data from kinesis.getRecords in Javascript SDK?如何在 Javascript SDK 中解码来自 kinesis.getRecords 的数据?
【发布时间】:2019-05-28 15:30:26
【问题描述】:

我正在尝试从 Kinesis 数据流中获取数据:

function getRecord(shard_iterator) {

    var getRecParams = {
        ShardIterator: shard_iterator
    };

    kinesis.getRecords(getRecParams, function(err, result) {
            // Loop through all the packages
            for (var record in result.Records) {
                console.log(JSON.stringify(result.Records[record].Data));
                break; // just to see the first one
            }
            //if (result.NextShardIterator) getRecord(result.NextShardIterator);
    });
}

我看到的结果:

{"type":"Buffer","data":[123,34,73,110,112,117....,125]}

Form AWS CLI 我知道data 应该是base64 编码的,但这里有些不同。那么如何从我看到的data 数组中获取信息呢?

请注意它不是 NodeJS,而是浏览器中的 Javascript。

【问题讨论】:

    标签: amazon-kinesis aws-sdk-js


    【解决方案1】:

    解决方案,最好在文档中包含它:

    var decoder = new TextDecoder("utf-8");
    function getRecord(shard_iterator) {
    
        var getRecParams = {
            ShardIterator: shard_iterator
        };
    
        kinesis.getRecords(getRecParams, function(err, result) {
            if (err) {
                console.log("Error in getRecords() from the Kinesis stream.");
                console.log(err);
            } else {
                try {
                    // Loop through all the packages
                    for (var record in result.Records) {
                        data = result.Records[record].Data
                        decoded = JSON.parse(decoder.decode(data));
                        console.log(decoded);
                    }
                } catch(err) {
                    console.log("Error parsing the package.");
                    console.log(err);
                }
                if (result.NextShardIterator) getRecord(result.NextShardIterator);
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-08
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多