如何创建有效的Avro格式文件nodejs

时间:2016-06-13 20:21:17

标签: node.js google-bigquery avro

我正在尝试创建一个有效的avro文件,以便上传到google-bigquery。

var avro = require('node-avro-io').DataFile.AvroFile();
var schema = {
            "name": "data",
            "type": "record",
            "fields": [
                {"name":"key","type": "string"},
                {"name":"value","type": "string"},
                {"name":"flag","type": "boolean"},
                {"name":"subrecord","type":"record","fields":[
                    {"name":"key","type":"string"},
                    {"name":"value","type":["string","int","null"]}
                ]}
            ]
};
var writer = avro.open("myAvroFile.avro", schema, { flags: 'w' , codec: 'deflate'});
writer
    .append({ key:"john", value:"hive", flag: true, subrecord: { key: "preference", value: 2}})
    .append({ key:"eric", value:"lola", flag: true, subrecord: { key: "postcode", value: null}})
    .end({ key:"fred", value:"wonka", flag: false, subrecord: { key: "city", value: "michigan"}});

这里是myAvroFile.avro

Obj�avro.codec�deflate�avro.schema�{"name":"data","type":"record","fields":[{"name":"key","type":"string"},{"name":"value","type":"string"},{"name":"flag","type":"boolean"},{"name":"subrecord","type":"record","fields":[{"name":"key","type":"string"},{"name":"value","type":["string","int","null"]}]}]} �3�ä0�x���A� @0O� )�T�%H4��:�*Uy�>P0��%�05k��n�d�T�������\����I�3�ä0�x�

但是当我尝试将其上传到大查询时,它会失败:

The Apache Avro library failed to parse file

当我尝试从以下链接上传解压缩的avro fli时,我成功了。 https://cloud.google.com/bigquery/docs/yob1900.avro.zip

为什么myAvroFile.avro无效? 如何使用node-avro-io库创建有效的avro文件?

1 个答案:

答案 0 :(得分:1)

看起来评论的限制非常小。我会发布一个答案。

这是我使用avro-tools获得的错误消息:

线程中的异常" main" org.apache.avro.SchemaParseException:"记录"不是定义的名称。 "子记录的类型"字段必须是已定义的名称或{" type":...}表达式。

我发现avro-tools可以方便地播放avro文件。这个link解释了如何使用它。

"子记录"的类型字段提交需要是一个架构。根据Avro specification

  

type:定义模式的JSON对象,或命名a的JSON字符串   记录定义(必填)。

所以你应该改变它:

dropdown-box