var kafka = require('kafka-node');
Producer = kafka.Producer;
KeyedMessage = kafka.KeyedMessage;
client = new kafka.Client();
producer = new Producer(client);
km = new KeyedMessage('key', 'message');
payloads = [
{ topic: 'topic1', messages: 'hi', partition: 0 },
{ topic: 'topic2', messages: ['hello', 'world', km] }
];
producer.on('ready', function ()
{
producer.send(payloads, function (err, data)
{
console.log(data);
});
});
producer.on('error', function (err) {});
//该代码的错误
baaz@bit:~$ node kafka1.js
/home/baaz/node_modules/kafka-node/lib/codec/snappy.js:18
var SNAPPY_MAGIC = Buffer.from(SNAPPY_MAGIC_BYTES).toString('hex');
^
TypeError: this is not a typed array.
at Function.from (native)
at Object.<anonymous> (/home/baaz/node_modules/kafka-node/lib/codec/snappy.js:18:29)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/home/baaz/node_modules/kafka-node/lib/codec/index.js:4:19)
at Module._compile (module.js:425:26)
答案 0 :(得分:1)
简短答案: 将节点版本提高到4.5以上或 使用“ kafka-node”:“ 1.6.2”或更小
详细回答 在节点4.5及更高版本中,节点不赞成使用通过构造函数创建缓冲区的旧方法,因此,低于4.5的所有节点js版本都没有新的支持。而kafka-node版本> 1.6.2使用buffer.from来创建缓冲区,因此存在兼容性问题。
欢呼 编码愉快