我想从
JSON对象序列化数据,并通过网络将其发送到kafka作为结束.现在我有一个avro模式在一个文件,确定发送到kafka为日志记录系统必需的字段:
{"namespace": "com.company.wr.messages","type": "record","name": "Log","fields": [ {"name": "timestamp","type": "long"},{"name": "source","type": "string"},{"name": "version",{"name": "ipAddress",{"name": "name",{"name": "level",{"name": "errorCode",{"name": "message","type": "string"} ] }
我正在使用一个节点包’avro-schema’,我尝试过其他的,但是没有一个工作正常,我只需要从节点js以一个avro的方式序列化.
解决方法
与
avsc
:
var avro = require('avsc'); // Parse the schema. var logType = avro.parse({ "namespace": "com.company.wr.messages","fields": [ {"name": "timestamp","type": "string"} ] }); // A sample log record. var obj = { timestamp: 2313213,source: 'src',version: '1.0',ipAddress: '0.0.0.0',name: 'foo',level: 'INFO',errorCode: '',message: '' }; // And its corresponding Avro encoding. var buf = logType.toBuffer(obj);