【问题标题】:Serializing data with Avro in node js在节点 js 中使用 Avro 序列化数据
【发布时间】:2014-05-21 01:29:14
【问题描述】:

我想序列化来自 JSON 对象的数据,并以 kafka 为结尾通过网络发送。现在我在一个文件中有一个 avro 模式,它确定了为日志系统发送到 kafka 所需的字段:

{"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"}
       ]
}

我正在使用节点包 'avro-schema',我尝试了其他的但都没有正常工作,我只需要从节点 js 以 avro 方式序列化。

【问题讨论】:

    标签: json node.js serialization avro


    【解决方案1】:

    以下是我们为类似用例所做的示例,我们将 Avro 记录发送到另一个队列 (Amazon Kinesis),以适应您的架构。我们将它与 node-avro-io 0.2.0 和 stream-to-arry 2.0.2 一起使用。

    var avro = require('node-avro-io');
    var toArray = require('stream-to-array');
    var schema = {
        "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"}
        ]
    };
    var writer = new avro.DataFile.Writer(schema, "snappy");
    toArray(writer, function(err, arr) {
        var dataBuffer = Buffer.concat(arr);
        // Send dataBuffer to Kafka here
    });
    var record = {
        "timestamp": 123,
        "source": "example.com",
        "version": "HTTP 1.1",
        "ipAddress": "123.123.123.123",
        "name": "Jim",
        "level": "INFO",
        "errorCode": "200",
        "message": "foo"
    };
    writer.append(record).end();
    

    在撰写本文时,node-avro-io 的示例用于序列化/反序列化文件系统上的 Avro 文件。此示例使用 stream-to-array 包作为从基于流的 node-avro-io 包中获取Buffer 的快捷方式。 Buffer 可以作为 Kafka 生产者中的消息发送到您的队列。

    其他一些 node.js 包,例如 avronode 和 Collective's node-avro,是 C++ 库的包装器。我对这些软件包没有那么成功。这是 node-avro 的 Avro C++ 库安装说明的 tl:dr(为其构建 .deb 包)。它可能对任何 C++ 包装程序包都有帮助。

    sudo apt-get install -y libboost-all-dev cmake checkinstall
    ssh clone git@github.com:apache/avro.git
    cd avro
    git checkout release-1.7.7
    cd lang/c++
    cmake -G "Unix Makefiles"
    sudo checkinstall -y \
        --install=no \
        --pkgname="avro-cpp" \
        --pkgrelease="1.7.7" \
        --maintainer="me@example.com" \
        --addso=yes
    

    对于 Collective 的 node-avro,我必须从 Ubuntu 14.04 上的 bin/install-and-run-tests 脚本中删除 export CXXFLAGS="-fcxx-exceptions" 行。

    【讨论】:

      【解决方案2】:

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

      您可以找到更多关于可用的各种编码方法的信息here

      【讨论】:

      • 太棒了。正是我需要的。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2020-08-04
      • 2016-06-10
      • 2023-03-11
      • 2023-03-03
      • 2018-12-10
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多