-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDataConsumer.agent.nut
144 lines (130 loc) · 5.55 KB
/
DataConsumer.agent.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#require "AWSRequestV4.class.nut:1.0.2"
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
// AWSKinesisStreams.Consumer library sample.
// Periodically reads new data records from all shards of the specified preconfigured
// AWS Kinesis Stream.
// Records are read every 15 seconds and printed to the log.
const READ_DATA_PERIOD_SEC = 15.0;
const RECORDS_LIMIT = 10;
class DataConsumer {
_awsConsumer = null;
_shardIterators = null;
constructor(awsRegion, awsAccessKeyId, awsSecretAccessKey, awsKinesisStreamName) {
_awsConsumer = AWSKinesisStreams.Consumer(
awsRegion, awsAccessKeyId, awsSecretAccessKey, awsKinesisStreamName);
_shardIterators = {};
}
// Periodically reads latest records from the specified shard
function readRecords(shardId, options) {
imp.wakeup(READ_DATA_PERIOD_SEC, function () {
_awsConsumer.getRecords(
options,
function (error, records, millisBehindLatest, nextOptions) {
if (error) {
server.error("Data reading failed: " + error.details);
readData(shardId);
} else {
if (records.len() > 0) {
server.log("Data read successfully:");
foreach (record in records) {
server.log(format("data: %s, timestamp: %d",
http.jsonencode(record.data), record.timestamp));
}
} else {
server.log("No new data records");
}
if (nextOptions) {
_shardIterators[shardId] = nextOptions.shardIterator;
readRecords(shardId, nextOptions);
} else {
// the current shard has been closed,
// shard iterators need to be reinitialized
_shardIterators[shardId] = null;
initShardIterators();
}
}
}.bindenv(this));
}.bindenv(this));
}
// Starts data reading from the specified shard
function readData(shardId) {
local shardIterator = _shardIterators[shardId];
if (shardIterator) {
readRecords(
shardId,
{ "shardIterator" : shardIterator, "limit" : RECORDS_LIMIT });
}
}
// Initializes shard iterator for the specified shard and starts data reading
function initShardIterator(shardId) {
_shardIterators[shardId] <- null;
_awsConsumer.getShardIterator(
shardId,
AWS_KINESIS_STREAMS_SHARD_ITERATOR_TYPE.LATEST,
null,
function (error, shardIterator) {
if (error) {
server.error("getShardIterator failed: " + error.details);
} else {
_shardIterators[shardId] = shardIterator;
readData(shardId);
}
}.bindenv(this));
}
// Initializes shard iterators for all stream shards
function initShardIterators() {
_awsConsumer.getShards(function (error, shardIds) {
if (error) {
server.error("getShards failed: " + error.details);
} else {
foreach (id in shardIds) {
if (!(id in _shardIterators)) {
initShardIterator(id);
}
}
}
}.bindenv(this));
}
// Starts the application:
// - obtains the stream shards
// - initializes shards iterators
// - reads records from all the shards
function start() {
initShardIterators();
}
}
// RUNTIME
// ---------------------------------------------------------------------------------
// AWS KINESIS CONSTANTS
// ----------------------------------------------------------
const AWS_KINESIS_REGION = "<YOUR_AWS_REGION>";
const AWS_KINESIS_ACCESS_KEY_ID = "<YOUR_AWS_ACCESS_KEY_ID>";
const AWS_KINESIS_SECRET_ACCESS_KEY = "<YOUR_AWS_SECRET_ACCESS_KEY>";
const AWS_KINESIS_STREAM_NAME = "testStream";
// Start application
dataConsumer <- DataConsumer(
AWS_KINESIS_REGION, AWS_KINESIS_ACCESS_KEY_ID, AWS_KINESIS_SECRET_ACCESS_KEY, AWS_KINESIS_STREAM_NAME);
dataConsumer.start();