简体   繁体   English

在使用node.js的lambda中,我如何解析kinesis流中的数据

[英]In lambda using node.js how can I parse the data from a kinesis stream

I have the following lambda function set up with kinesis as its event source. 我将以下lambda函数设置为kinesis作为其事件源。 The data is coming through as a stream, here I am parsing this to come through as a string eg {id=2, sourceIp=220.220.1.220 } . 数据以流形式传递,在这里我将其解析为字符串,例如{id=2, sourceIp=220.220.1.220 } I want to convert this to an object in node.js. 我想将它转换为node.js中的对象。 How can I do this? 我怎样才能做到这一点?

exports.handler = (event, context) => {

    event.Records.forEach(function(record) {
        var data = new Buffer(record.kinesis.data, 'base64').toString('ascii');
        console.log('data: ', data)
    });
};

我想你想要做的是:

var data = JSON.parse(new Buffer(record.kinesis.data, 'base64'));

I'm not familiar with the actual output of Kinesis but I'm a bit confused by the example you've given. 我不熟悉Kinesis的实际输出,但我对你给出的例子感到有些困惑。 The test example test even in lambda shows the Kinesis object looking like this 即使在lambda中测试示例测试也显示Kinesis对象看起来像这样

"kinesis": {
    "partitionKey": "partitionKey-3",
    "data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4=",
    "kinesisSchemaVersion": "1.0",
    "sequenceNumber": "49545115243490985018280067714973144582180062593244200961"
}

Which means the parsed string should look something like "id=2,sourceIp=220.220.1.220" from your example. 这意味着解析后的字符串应该与您的示例中的"id=2,sourceIp=220.220.1.220" If that is the case you could do something convoluted like this to get the data into a proper object. 如果是这种情况,您可以像这样做一些令人费解的事情,以将数据转换为适当的对象。

var json = {};
data.split(',').map(function(each){
    return each.split('=');
}).forEach(function(pair){
    json[pair[0]] = pair[1];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM