-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic-bulk-writer.js
64 lines (51 loc) · 1.45 KB
/
elastic-bulk-writer.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
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
var elasticsearch = require('elasticsearch');
var fs = require('fs');
var JSONStream = require('JSONStream');
var sentiment = require('sentiment');
var stream = fs.createReadStream('data/backup/stocktwits_messages_dec_2015.json', {
encoding: 'utf8'
});
var log4js = require('log4js');
var logger = log4js.getLogger('ES-BULK');
logger.setLevel('INFO');
var i = 0;
var parser = JSONStream.parse();
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'info'
});
stream.pipe(parser);
parser.on('data', function(obj) {
i++;
if (i == 1000) {
stream.pause();
logger.info('Stream Paused at ' + i);
setTimeout(function(){
logger.info('Stream Reset at' + i);
i = 0;
stream.resume();
},1000);
}
var arr = [];
logger.info("Stream Read at " + i);
var customSentiment = sentiment(obj.body);
var newObj = {
obj: obj,
customSentiment: customSentiment
}
arr.push({ "index" : { "_index" : "testbulk", "_type" : "block", "_id" : obj.id } })
arr.push(newObj);
processLine(arr);
});
function processLine(line) { // here's where we do something with a line
logger.info("Processing Record...");
client.bulk({
requestTimeout: 300000000,
body: line
}, function(error, response) {
if (error) {
logger.error(error);
}
});
logger.info("Record Created: " + line[1].obj.id);
}