2

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

The result I see:

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

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?

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

Putnik
  • 5,925
  • 7
  • 38
  • 58

2 Answers2

4

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);
        }
    });
}
Putnik
  • 5,925
  • 7
  • 38
  • 58
0

I was having a similar issue. In my case I just had to change JSON.stringify(result.Records[record].Data) to result.Records[record].Data.toString()

To understand obj.toString() vs JSON.stringify(obj), here is the link: What's the difference in using toString() compared to JSON.stringify()?

nirojshrestha019
  • 2,068
  • 1
  • 10
  • 14