简体   繁体   中英

Impossible to read Kafka message value

I'm using a simple producer in order to produce a message in kafka. This message is a json object.

This is the code of my producer :

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();
    });
  });

But then when i try to read the content of this message I always get

[Object object]

This is the code of my consumer :

'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);
});

Every method I try in the handler return the same result : [Object object]

Problem lies in below shown code:

producer.send([
      { topic: topic, partition: p, messages: message, attributes: a }
    ], function (err, result) {
  expect(result).not.toBeNull();
  expect(err).toBeNull();
  done();
});

From the documentation as shown below, you cannot supply object to messages . Supplying JSON.stringfy(message) to messages will work.

{
   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)
}

您的生产者消息可能需要具有以下值:

JSON.stringify(message)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM