如何在 Javascript SDK 中解码来自 kinesis.getRecords 的数据? [英] How to decode data from kinesis.getRecords in Javascript SDK?

查看:37
本文介绍了如何在 Javascript SDK 中解码来自 kinesis.getRecords 的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get data from Kinesis data stream:

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 数组中获取信息?

Form AWS CLI I know data should be base64-encoded, but here is something different. So how can I get info from the data array I see?

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

Pls note it's not NodeJS but Javascript in browser.

推荐答案

解决方案,如果将其包含在文档中会很不错:

Solution, would be nice to have it in doc:

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);
        }
    });
}

这篇关于如何在 Javascript SDK 中解码来自 kinesis.getRecords 的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆