簡體   English   中英

將消息從kafka消費者推送到mongodb

[英]push messages from kafka consumer to mongodb

我在活動中使用“ kafka-node”創建了kafka消費者

consumer.on('message' ()=>{
connecting to mongodb and inserting to a collection.
})

mongo.js文件,用於創建與mongo的連接並返回對象

const MongoClient = require('mongodb').MongoClient, assert = require('assert');

const url = 'mongodb://root:****@ds031257.mlab.com:31257/kafka-node';

let _db;

 const connectDB =  (callback) => {
     try {
         MongoClient.connect(url, { useNewUrlParser: true }, (err, database) => {
             console.log('message' + database)
             _db = database.db('kafka-node');
             return callback(err);
         })
     } catch (e) {
         throw e;
     }
 }

 const getDB = () => _db;

 const close = () => _db.close();
 module.exports = { connectDB, getDB, close }

Consumer.js用於創建使用者並將消息推送到mongodb

let kafka = require('kafka-node');
let MongoDB = require('./mongo');
let Consumer = kafka.Consumer,
    // The client specifies the ip of the Kafka producer and uses
    // the zookeeper port 2181
    client = new kafka.KafkaClient({ kafkaHost: 'localhost:9093, localhost:9094, localhost:9095' });
// The consumer object specifies the client and topic(s) it subscribes to
consumer = new Consumer(
    client, [{ topic: 'infraTopic', partitions: 3 }], { autoCommit: false });


consumer.on('ready', function () {
    console.log('consumer is ready');
});

consumer.on('error', function (err) {
    console.log('consumer is in error state');
    console.log(err);
})
client.refreshMetadata(['infraTopic'], (err) => {
    if (err) {
        console.warn('Error refreshing kafka metadata', err);
    }
});
consumer.on('message', function (message) {
    // grab the main content from the Kafka message
    console.log(message);
    MongoDB.connectDB((err) => {
        if (err) throw err
        // Load db & collections
        const db = MongoDB.getDB();
        const collectionKafka = db.collection('sampleCollection');
        try {
            collectionKafka.insertOne(
                {
                    timestamp: message.value,
                    topic: message.topic
                },
                function (err, res) {
                    if (err) {
                        database.close();
                        return console.log(err);
                    }
                    // Success
                }
            )
        } catch (e) {
            throw e
        }
    })
});

這是從kafka使用者向mongodb推送消息的正確方法嗎? 通過這種設置,它可以一直工作到所有消息都被寫入為止,一旦到達EOL,它就會拋出“無法讀取null的'db'屬性”

這是從kafka使用者向mongodb推送消息的正確方法嗎?

我想這是一種方法,但是我不會用正確的方法來稱呼它:)

更好的方法是使用Kafka Connect。 它是Apache Kafka的一部分,旨在完全按照您的意圖進行-將數據從Kafka流式傳輸到目標系統(您也可以將其用於將其他系統中的數據流式傳輸 Kafka中)。

MongoDB有一個出色的連接器,其中包含全面的文檔 ,這些文檔將完全按照您的意圖進行。

如果需要在寫入之前處理數據,則遵循的模式是使用Kafka Streams,KSQL或要使用的任何處理工具進行處理-但將其寫回Kafka主題 然后,Kafka Connect會讀取該主題並將其流式傳輸到您的目標。 這樣,您就可以將責任分離開來,並創建一個更加簡單但具有彈性和可擴展性的系統。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM