我正在使用一个简单的生产者,以便在kafka中生成一条消息。 此消息是json对象。
这是我的制作人的代码:
const kafka = require('kafka-node');
const Producer = kafka.Producer;
const KeyedMessage = kafka.KeyedMessage;
const Offset = kafka.Offset;
const Client = kafka.Client;
const p = 0;
const a = 0;
const topic = 'claims';
const client = new Client('192.168.99.100:2181');
const producer = new Producer(client, { requireAcks: 1 });
producer.on('error', function (err) {
done.fail(err);
});
producer.on('ready', function () {
const message = {status: 'Created', id_claims: '12345'};
producer.send([
{ topic: topic, partition: p, messages: message, attributes: a }
], function (err, result) {
expect(result).not.toBeNull();
expect(err).toBeNull();
done();
});
});
但是当我尝试阅读此消息的内容时,我总是得到
[对象]
这是我的消费者的代码:
'use strict';
const util = require('util')
const kafka = require('kafka-node');
const Consumer = kafka.Consumer;
const Offset = kafka.Offset;
const Client = kafka.Client;
const topic = 'claims';
const client = new Client('192.168.99.100:2181');
const topics = [
{topic: topic, partition: 0}
];
const options = { autoCommit: false, fetchMaxWaitMs: 1000, fetchMaxBytes: 1024 * 1024 };
const consumer = new Consumer(client, topics, options);
const offset = new Offset(client);
consumer.on('message', function (message) {
console.log(JSON.stringify(message.value));
console.log("Value: %j", message.value);
console.log("Value: %o", message.value);
console.log('Value: ' + util.inspect(message.value, 10));
console.log(util.inspect(message.value, {showHidden: false, depth: null}));
});
consumer.on('error', function (err) {
console.log(err);
});
我在处理程序中尝试的每个方法都返回相同的结果:[Object object]
答案 0 :(得分:2)
问题出在下面显示的代码中:
producer.send([
{ topic: topic, partition: p, messages: message, attributes: a }
], function (err, result) {
expect(result).not.toBeNull();
expect(err).toBeNull();
done();
});
无法从documentation如下所示向object
提供messages
。向邮件提供JSON.stringfy(message)
即可。
{
topic: 'topicName',
messages: ['message body'], // multi messages should be a array, single message can be just a string or a KeyedMessage instance
key: 'theKey', // string or buffer, only needed when using keyed partitioner
partition: 0, // default 0
attributes: 2, // default: 0
timestamp: Date.now() // <-- defaults to Date.now() (only available with kafka v0.10 and KafkaClient only)
}
答案 1 :(得分:0)
您的制作人消息可能需要具有以下值:
JSON.stringify(message)