Serializing data with Avro in node js

前端 未结 2 948
借酒劲吻你
借酒劲吻你 2021-02-08 04:14

I would like to serialize data from a JSON object and send it throught the network with kafka as an end. Now I have an avro schema in a file, that determinate the fields necessa

2条回答
  •  执笔经年
    2021-02-08 04:23

    With avsc:

    var avro = require('avsc');
    
    // Parse the schema.
    var logType = avro.parse({
      "namespace": "com.company.wr.messages",
      "type": "record",
      "name": "Log",
      "fields": [
        {"name": "timestamp", "type": "long"},
        {"name": "source", "type": "string"},
        {"name": "version", "type": "string"},
        {"name": "ipAddress", "type": "string"},
        {"name": "name", "type": "string"},
        {"name": "level", "type": "string"},
        {"name": "errorCode", "type": "string"},
        {"name": "message", "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);
    

    You can find more information on the various encoding methods available here.

提交回复
热议问题