Skip to content

Commit 14e75db

Browse files
committed
Add example
1 parent 006a8da commit 14e75db

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

example/async.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const StreamUtils = require('@tilfin/stream-utils');
2+
const KSL = require('../lib');
3+
const PromisedLife = require('promised-lifestream');
4+
5+
exports.handler = async function (event) {
6+
console.log('event: ', JSON.stringify(event, null, 2));
7+
8+
const result = [];
9+
10+
await PromisedLife([
11+
KSL.reader(event, { isAgg: false }),
12+
KSL.parseJSON({ flatArray: false }),
13+
StreamUtils.map(function(data, cb) {
14+
result.push(data);
15+
cb(null, data)
16+
})
17+
])
18+
19+
console.dir(result);
20+
}
21+
22+
exports.handler(require('./data'))
23+
.then(() => {
24+
console.info('<<< END >>>')
25+
})
26+
.catch(err => {
27+
console.error('Failed:', err)
28+
})

example/data.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"Records": [
3+
{
4+
"kinesis": {
5+
"kinesisSchemaVersion": "1.0",
6+
"partitionKey": "485bd0f3c7f8b6ec3ddc6ada18abcf8d",
7+
"sequenceNumber": "49565441425403787852263465772329791873704550161163747330",
8+
"data": "W3siaWQiOjEsInZhbHVlIjoxMH0seyJpZCI6MiwidmFsdWUiOjIwfSx7ImlkIjozLCJ2YWx1ZSI6MzB9LHsiaWQiOjQsInZhbHVlIjoxNX0seyJpZCI6NSwidmFsdWUiOjI1fV0=",
9+
"approximateArrivalTimestamp": 1473002069.264
10+
},
11+
"eventSource": "aws:kinesis",
12+
"eventVersion": "1.0",
13+
"eventID": "shardId-000000000000:49565441425403787852263465772329791873704550161163747330",
14+
"eventName": "aws:kinesis:record",
15+
"invokeIdentityArn": "arn:aws:iam::986450800000:role/lambda_basic_execution",
16+
"awsRegion": "ap-northeast-1",
17+
"eventSourceARN": "arn:aws:kinesis:ap-northeast-1:986450800000:stream/kpl-stream"
18+
},
19+
{
20+
"kinesis": {
21+
"kinesisSchemaVersion": "1.0",
22+
"partitionKey": "485bd0f3c7f8b6ec3ddc6ada18abcf8d",
23+
"sequenceNumber": "49565441425403787852263465772329791873704550161163747330",
24+
"data": "W3siaWQiOjEsInZhbHVlIjoxMH0seyJpZCI6MiwidmFsdWUiOjIwfSx7ImlkIjozLCJ2YWx1ZSI6MzB9LHsiaWQiOjQsInZhbHVlIjoxNX0seyJpZCI6NSwidmFsdWUiOjI1fV0=",
25+
"approximateArrivalTimestamp": 1473002069.264
26+
},
27+
"eventSource": "aws:kinesis",
28+
"eventVersion": "1.0",
29+
"eventID": "shardId-000000000000:49565441425403787852263465772329791873704550161163747330",
30+
"eventName": "aws:kinesis:record",
31+
"invokeIdentityArn": "arn:aws:iam::986450800000:role/lambda_basic_execution",
32+
"awsRegion": "ap-northeast-1",
33+
"eventSourceARN": "arn:aws:kinesis:ap-northeast-1:986450800000:stream/kpl-stream"
34+
},
35+
{
36+
"kinesis": {
37+
"kinesisSchemaVersion": "1.0",
38+
"partitionKey": "485bd0f3c7f8b6ec3ddc6ada18abcf8d",
39+
"sequenceNumber": "49565441425403787852263465772329791873704550161163747330",
40+
"data": "W3siaWQiOjEsInZhbHVlIjoxMH0seyJpZCI6MiwidmFsdWUiOjIwfSx7ImlkIjozLCJ2YWx1ZSI6MzB9LHsiaWQiOjQsInZhbHVlIjoxNX0seyJpZCI6NSwidmFsdWUiOjI1fV0=",
41+
"approximateArrivalTimestamp": 1473002069.264
42+
},
43+
"eventSource": "aws:kinesis",
44+
"eventVersion": "1.0",
45+
"eventID": "shardId-000000000000:49565441425403787852263465772329791873704550161163747330",
46+
"eventName": "aws:kinesis:record",
47+
"invokeIdentityArn": "arn:aws:iam::986450800000:role/lambda_basic_execution",
48+
"awsRegion": "ap-northeast-1",
49+
"eventSourceARN": "arn:aws:kinesis:ap-northeast-1:986450800000:stream/kpl-stream"
50+
}
51+
]
52+
}

example/normal.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const StreamUtils = require('@tilfin/stream-utils');
2+
const KSL = require('../lib');
3+
4+
exports.handler = function (event, context, callback) {
5+
console.log('event: ', JSON.stringify(event, null, 2));
6+
7+
const result = [];
8+
const stream = KSL.reader(event, { isAgg: false });
9+
10+
stream.on('end', () => {
11+
console.dir(result);
12+
callback();
13+
});
14+
15+
stream.on('error', err => {
16+
callback(err);
17+
});
18+
19+
stream
20+
.pipe(KSL.parseJSON({ flatArray: false }))
21+
.pipe(StreamUtils.map(function(data, cb) {
22+
result.push(data);
23+
cb(null, data)
24+
}));
25+
}
26+
27+
exports.handler(require('./data'), null, function (err) {
28+
if (err) console.error('Failed:', err)
29+
else console.info('<<< END >>>')
30+
})

0 commit comments

Comments
 (0)