繁体   English   中英

从 JSON 消息格式中删除 Avro 类型密钥

[英]Remove Avro type keys from JSON message format

我正在尝试创建一个脚本来反序列化来自 Kafka 的一些 Avro 消息。

消息的格式如下:

{
  "value": {
    "value1": {
      "string": "AAAA"
    }
  }
}

我需要它是这样的

{
  "value": {
    "value1":  "AAAA"
  }
}

基本上,删除该字符串。

我有他们两个的模式。

我需要从使用模式序列化的消息移动到使用另一个模式反序列化的消息。

我试图用 python avro/fastavro 做一些事情,但我没有成功。

我不能只删除它并格式化,因为我需要重新格式化的 Avro 要复杂得多。 所以,我需要一些可以根据我的模式重新格式化这些 avros 的东西。

我无法从问题中判断您是尝试在模式之间进行转换还是只是删除该string提示。 如果您只是想删除提示,您可以这样做:

from fastavro import json_reader, json_writer

schema = {...}

with open('some-file', 'r') as fo:
    avro_reader = json_reader(fo, schema)
    records = [record for record in avro_reader]

with open('some-other-file', 'w') as out:
    json_writer(out, schema, records, write_union_type=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM