प्रोजेक्ट Apache Kafka में एकीकृत किया जा रहा है Node.js

Apache Kafka किसी प्रोजेक्ट में एकीकृत करने से Node.js आपको वास्तविक समय के एप्लिकेशन बनाने की अनुमति मिलती है जो काफ्का की डेटा प्रोसेसिंग क्षमताओं का लाभ उठाते हैं। Apache Kafka किसी Node.js प्रोजेक्ट में एकीकृत करने के तरीके पर यहां एक बुनियादी मार्गदर्शिका दी गई है:

चरण 1: काफ्का लाइब्रेरी स्थापित करें Node.js

Node.js अपनी प्रोजेक्ट निर्देशिका में एक टर्मिनल खोलें ।

लाइब्रेरी स्थापित करने के लिए निम्नलिखित कमांड चलाएँ kafkajs, Node.js इसके लिए एक लाइब्रेरी Apache Kafka:। npm install kafkajs

चरण 2: काफ्का के साथ बातचीत करने के लिए कोड लिखें Node.js

kafkajs लाइब्रेरी को अपने कोड में आयात करें Node.js:

const { Kafka } = require('kafkajs');

इसके लिए कॉन्फ़िगरेशन पैरामीटर परिभाषित करें Kafka Broker:

const kafka = new Kafka({  
  clientId: 'your-client-id',  
  brokers: ['broker1:port1', 'broker2:port2'], // Replace with actual addresses and ports  
});  

संदेश भेजने के लिए एक बनाएं producer:

const producer = kafka.producer();  
  
const sendMessage = async() => {  
  await producer.connect();  
  await producer.send({  
    topic: 'your-topic',  
    messages: [{ value: 'Hello Kafka!' }],  
  });  
  await producer.disconnect();  
};  
  
sendMessage();  

संदेश प्राप्त करने के लिए एक बनाएं consumer:

const consumer = kafka.consumer({ groupId: 'your-group-id' });  
  
const consumeMessages = async() => {  
  await consumer.connect();  
  await consumer.subscribe({ topic: 'your-topic', fromBeginning: true });  
  
  await consumer.run({  
    eachMessage: async({ topic, partition, message }) => {  
      console.log(`Received message: ${message.value}`);  
    },  
  });  
};  
  
consumeMessages();  

 

नोट: 'your-client-id', 'broker1:port1', 'your-topic', और जैसे मानों को 'your-group-id' अपनी वास्तविक परियोजना जानकारी से बदलें ।

ध्यान रखें कि आपकी विशिष्ट आवश्यकताओं के आधार पर एकीकृत करना अधिक जटिल हो सकता है Apache Kafka । कॉन्फ़िगरेशन विकल्पों और कार्यात्मकताओं के बारे में अधिक समझने के लिए Node.js आधिकारिक दस्तावेज़ Apache Kafka और लाइब्रेरी का संदर्भ लेना सुनिश्चित करें। kafkajs