Last active
October 2, 2019 07:56
-
-
Save hakanilter/913b08d6319da5a7378a409964ad45c7 to your computer and use it in GitHub Desktop.
Indexing data from Kafka to ElasticSearch with Node.js in 30 lines :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var elasticsearch = require('elasticsearch'); | |
var elastic = new elasticsearch.Client({ | |
host: 'localhost:9200', | |
log: 'info' | |
}); | |
var kafka = require('kafka-node'), | |
HighLevelConsumer = kafka.HighLevelConsumer, | |
client = new kafka.Client(), | |
consumer = new HighLevelConsumer( | |
client, | |
[ | |
{ topic: 'network-data', maxNum: 1 } | |
], | |
{ | |
groupId: 'nodejs-group', | |
fromOffset: 'latest' | |
} | |
); | |
consumer.on('message', function (message) { | |
var data = JSON.parse(message.value); | |
elastic.index({ | |
index: 'network-data', | |
type: 'doc', | |
body: data | |
}, function (error, response) { | |
if (error !== undefined) console.log(error); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment