1

我正在尝试通过 IBM APIConnect 工具包与 Apache Kafka 进行交互。我已经安装了 API Connect 的 DataSource 选项卡中提供的 Kafka 连接器,并创建了一个引用 Kafka 数据库的空白模型,并将以下代码放在 /common/model/Kafka-Model.js 中。此外,我在本地运行了 Apache Kafka。

    'use strict';
     var kafka = require('kafka-node');
     var connector = require('loopback-connector-kafka');
     module.exports = function(KafkaModel) {
    function KafkaCreate(){
//var assert = require('assert');
var kafka = require('kafka-node');
var client = new kafka.Client('localhost:2181');
var topic = 'topicTest12';
var consumer = new kafka.HighLevelConsumer(client, [ { topic: topic } ], {});
var producer = new kafka.HighLevelProducer(client);
var connector = require('loopback-connector-kafka');
var settings = {
connectionString: 'localhost:2181'
}
var dataSource = { settings: settings };
var payloads= [
{ topic: 'topicTest12', messages: 'This is the First Message I am sending', partition: 0 },
];
connector.initialize(dataSource);
consumer.on('message', function (messages) {
//assert.equal(messages.value, 'Hallo');
console.log(messages);
process.exit();
});
consumer.on('error', function (error) {
console.log('Consumer error: ', error);
});
producer.createTopics([ topic ], function (err) {
if (err) {
console.log('Got error when create topic:', err);
return;
}
else {
producer.on('ready', function(){
producer.send(payloads, function(err, data){
console.log(data)
});
});
}
setTimeout(function loop() {
dataSource.connector.send(topic, payloads, function (err) {
if (err) console.log('Got error when send message:', err);
else console.log('Message send');
});
}, 1000);
})
};
KafkaModel.remoteMethod('KafkaCreate',{
returns: { arg:'status',type: 'messages' },
http: { verb: 'post', 'path': '/replaceOrCreate' }
})
};

当我在邮递员中使用 http://localhost:4001/api/KafkaModels/replaceOrCreate URL 和 POST 方法执行上述代码时,我收到以下错误

"message": "Cannot read property 'apply' of undefined", "stack": "TypeError: Cannot read property 'apply' of undefined\n at SharedMethod.invoke.

我不确定这是否是从 IBM API Connect 与 Kafka 进行交互的正确方法。请建议我们是否可以通过任何其他方式来做到这一点。!!。

4

0 回答 0