以下是我们为类似用例所做的示例,我们将 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" 行。