Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby Avro:记录架构报告数据无效_Ruby On Rails_Json_Ruby_Serialization_Avro - Fatal编程技术网

Ruby on rails Ruby Avro:记录架构报告数据无效

Ruby on rails Ruby Avro:记录架构报告数据无效,ruby-on-rails,json,ruby,serialization,avro,Ruby On Rails,Json,Ruby,Serialization,Avro,我正在尝试使用avro和模式序列化JSON。这不是一个rails应用程序(尽管有标签,但我需要注意),因此我也可以使用map,但以下两项工作都不需要 schema = { 'type' => 'record', 'name' => 'test', 'fields' => [ { 'name' => 'field_one', 'type' => 'string' },

我正在尝试使用avro和模式序列化JSON。这不是一个rails应用程序(尽管有标签,但我需要注意),因此我也可以使用map,但以下两项工作都不需要

schema = { 'type' => 'record', 'name' => 'test', 'fields' => [ { 'name' => 'field_one', 'type' => 'string' },
                                                               { 'name' => 'field_two', 'type' => 'string'},
                                                               { 'name' => 'field_three', 'type' => 'string'},
                                                               { 'name' => 'field_four', 'type' => 'string' } ] }.to_json
message = {'field_one' => 'why',
             'field_two' => 'this',
             'field_three' => 'no',
             'field_four' => 'worky?'}.to_json
schema = Avro::Schema.parse(schema)
dw = Avro::IO::DatumWriter.new(schema)
buffer = StringIO.new()
encoder = Avro::IO::BinaryEncoder.new(buffer)
dw.write(message, encoder)
puts buffer.read

返回:'数据。。。不是模式的示例…'

将上面的架构替换为以下内容:

schema = { 'type' => 'map', 'values' => 'string' }.to_json
结果为:“StringObject的未定义方法'keys'”

不确定,为什么我不能用string:string键:值创建一个简单的映射。Avro的ruby文档非常糟糕;很少有实际的例子和大量的占位符模板,这给我们留下了很多想象。我希望能够将这个序列化字符串对象放入类型为application/json的http请求中,这可能是一个问题,因为avro希望是二进制的。

需要“avro”
require 'avro'
require 'json'

schema = { 'type' => 'record', 'name' => 'Test', 'fields' => [ { 'name' => 'field_one', 'type' => 'string' },
                                                               { 'name' => 'field_two', 'type' => 'string'},
                                                               { 'name' => 'field_three', 'type' => 'string'},
                                                               { 'name' => 'field_four', 'type' => 'string' } ] }.to_json

message = {'field_one' => 'why',
             'field_two' => 'this',
             'field_three' => 'no',
             'field_four' => 'worky?'}

schema = Avro::Schema.parse(schema)
writer = Avro::IO::DatumWriter.new(schema)
buffer = StringIO.new
writer = Avro::DataFile::Writer.new(buffer, writer, schema)
writer << message
writer.close # important

result = buffer.string

puts result
需要“json” 模式={'type'=>'record','name'=>'Test','fields'=>[{'name'=>'field\u one','type'=>'string'}, {'name'=>'field_two','type'=>'string'}, {'name'=>'字段三','type'=>'string'}, {'name'=>'field\u four','type'=>'string'}]}.to\u json 消息={'field_one'=>'为什么', 'field_two'=>'this', '字段三'=>'否', 'field_four'=>'worky?'} schema=Avro::schema.parse(schema) writer=Avro::IO::DatumWriter.new(架构) buffer=StringIO.new writer=Avro::DataFile::writer.new(缓冲区、writer、架构)
作者不确定是否有输入错误,但未设置
消息
,并且从未使用过
json\u hash
。唉,我无法向你隐瞒我的缺点……你愿意嫁给我吗?如果有一个有用的ruby编码员来帮助家里的人,那就太方便了。:)