ruby和avro二进制数据发布到二进制json http rest端点

时间:2018-05-04 16:45:20

标签: ruby binary avro

所以我的端点接受vnd.kafka.binary.v1+json

的二进制json内容类型

我已创建了avro对象,但如何格式化数据以正确调用端点?

  envelope = Hash.new
  # add some stuff to envelope hash
  .....
  envDatumWriter = Avro::IO::DatumWriter.new(envSchema)
  envBuffer = StringIO.new
  envEncoder = Avro::IO::BinaryEncoder.new(envBuffer)
  envDatumWriter.write(envelope,envEncoder)

我应该这样做:envBuffer.string.to_json_raw_object并将其发布到端点吗?

1 个答案:

答案 0 :(得分:1)

我正走在正确的道路上。将它发送到端点只是意味着我必须在端点上做一些特定于设计的额外事情。例如Base64编码以及json。

  envelope = Hash.new
  # add some stuff to envelope hash
  .....
  env_datum_writer = Avro::IO::DatumWriter.new(env_schema)
  env_buffer = StringIO.new("".force_encoding("BINARY"))
  env_encoder = Avro::IO::BinaryEncoder.new(env_buffer)
  env_datum_writer.write(envelope,env_encoder)
  a = Base64.encode64(env_buffer.string.each_byte.to_a.join)
  p_records = Hash.new
  p_records["value"] = a
  p_records_array = [p_records]