Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在NodeJS中使用apachekafka将记录添加到MongoDb中?_Node.js_Mongodb_Apache Kafka - Fatal编程技术网

Node.js 如何在NodeJS中使用apachekafka将记录添加到MongoDb中?

Node.js 如何在NodeJS中使用apachekafka将记录添加到MongoDb中?,node.js,mongodb,apache-kafka,Node.js,Mongodb,Apache Kafka,我正在试验apachekafka、NodeJS和MongoDB。下面我有一个简单的程序,可以简单地将我的名字添加到mongodb中进行练习。我试图把它和卡夫卡融合在一起,但它并没有按照我希望的方式工作。请参阅下面的代码 index.html <!DOCTYPE html> <html> <head> </head> <body> <form method="post" action="/addname"> <l

我正在试验apachekafka、NodeJS和MongoDB。下面我有一个简单的程序,可以简单地将我的名字添加到mongodb中进行练习。我试图把它和卡夫卡融合在一起,但它并没有按照我希望的方式工作。请参阅下面的代码

index.html

<!DOCTYPE html>
<html>
<head>
</head>

 <body>
 <form method="post" action="/addname">
 <label>Enter Your Name</label><br>
 <input type="text" name="firstName" placeholder="Enter first name..." 
required>
 <input type="text" name="lastName" placeholder="Enter last name..." 
   required>
 <input type="submit" value="Add Name">
 </form>
 </body>
</html>
}))

producer.js

const kafka = require('kafka-node'),
uuid = require('uuid');

const client = new kafka.Client("localhost:2181", 8, {
   sessionTimeout: 300,
   spinDelay: 100,
   retries: 2
});

const producer = new kafka.HighLevelProducer(client);
producer.on("ready", function() {
  console.log("Kafka Producer is connected and ready.");
});

// For this demo we just log producer errors to the console.
producer.on("error", function(error) {
   console.error(error);
});

const KafkaService = {
   sendRecord: ({ type, userId, sessionId, data }, callback = () => {}) => {
      if (!userId) {
          return callback(new Error(`A userId must be provided.`));
      }

      const event = {
          id: uuid.v4(),
          timestamp: Date.now(),
          userId: userId,
          sessionId: sessionId,
          type: type,
          data: data
      };

       const buffer = new Buffer.from(JSON.stringify(event));

       // Create a new payload
       const record = [
           {
               topic: "webevents.dev",
               messages: buffer,
               attributes: 1 /* Use GZip compression for the payload */
           }
       ];

       //Send record to Kafka and log result/error
       producer.send(record, callback);
   }
};
module.exports = KafkaService;
我得到下面的错误。这就是我的问题所在。我相信我需要在我的测试对象中更改我的类型,然后它可能会工作。我不确定。它还可能有其他的东西。

我所要做的就是指向我的虚拟盒子而不是本地主机,因为我的卡夫卡在Ubuntu盒子里

const client = new kafka.Client("10.10.205.17:2181, 8, {
   sessionTimeout: 300,
   spinDelay: 100,
   retries: 2
});

你必须解决这个问题吗?@Sudheernuna我的答案如下。我的答案中的IP地址是虚构的。您需要将其更改为您的Ubuntu box IP。您对节点中的Kafka连接流有何想法?
const kafka = require('kafka-node'),
uuid = require('uuid');

const client = new kafka.Client("localhost:2181", 8, {
   sessionTimeout: 300,
   spinDelay: 100,
   retries: 2
});

const producer = new kafka.HighLevelProducer(client);
producer.on("ready", function() {
  console.log("Kafka Producer is connected and ready.");
});

// For this demo we just log producer errors to the console.
producer.on("error", function(error) {
   console.error(error);
});

const KafkaService = {
   sendRecord: ({ type, userId, sessionId, data }, callback = () => {}) => {
      if (!userId) {
          return callback(new Error(`A userId must be provided.`));
      }

      const event = {
          id: uuid.v4(),
          timestamp: Date.now(),
          userId: userId,
          sessionId: sessionId,
          type: type,
          data: data
      };

       const buffer = new Buffer.from(JSON.stringify(event));

       // Create a new payload
       const record = [
           {
               topic: "webevents.dev",
               messages: buffer,
               attributes: 1 /* Use GZip compression for the payload */
           }
       ];

       //Send record to Kafka and log result/error
       producer.send(record, callback);
   }
};
module.exports = KafkaService;
const client = new kafka.Client("10.10.205.17:2181, 8, {
   sessionTimeout: 300,
   spinDelay: 100,
   retries: 2
});