-
Notifications
You must be signed in to change notification settings - Fork 6
/
producer.js
39 lines (37 loc) · 881 Bytes
/
producer.js
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
var X = require('node-xml')
, kafka = require('kafka');
var producer = new kafka.Producer({topic:'hamlet'}).connect(function() {
function send(i,line) {
setTimeout(function() {
line.TIMESTAMP = (new Date().getTime());
producer.send(JSON.stringify(line));
},1000*i + Math.floor(300*Math.random()));
}
var parser = new X.SaxParser(function(c) {
var i = 0;
var line = {};
var field = [];
c.onStartElementNS(function(e) {
field.push(e);
switch(e) {
case 'SPEECH':
line = {};
break;
}
});
c.onEndElementNS(function(e) {
field.pop();
switch(e) {
case 'SPEECH':
delete line['SPEECH'];
delete line['SCENE'];
send(i++,line);
break;
}
});
c.onCharacters(function(e) {
line[field[field.length-1]] = (line[field[field.length-1]]||"") + e;
});
});
parser.parseFile("hamlet.xml");
});