将 json 对象反序列化为 Faust 记录,其中 json 在键名中有连字符

Deserializing json object to Faust record where json has hyphenations in key names

我有一个 JSON 对象看起来像

{ 
  "timestamp": "2020-06-24T15:32:56.775518Z",
  "record-type": "data",
  "operation": "load",
  "partition-key-type": "primary-key",
  "schema-name": "legacy",
  "table-name": "test"
}

我正在尝试将记录反序列化为

class MetadataModel(faust.Record):
    timestamp: str
    record-type: str  # does not work, hyphens in name not allowed
    schema_name: str  # does not work
    tableName: str    # does not work 

我可能遗漏了一些简单的东西,但是我如何从键中有连字符的 json 对象转到 python 对象。任何帮助将不胜感激!

您可以使用 faust.models.fields 中的字段 类 在 JSON 中提供自定义名称。

import faust
from faust.models.fields import StringField


class MetadataModel(faust.Record):
    timestamp: str
    record_type: str = StringField(input_name='record-type')
    operation: str
    partition_key_type: str = StringField(input_name='partition-key-type')
    schema_name: str = StringField(input_name='schema-name')
    table_name: str = StringField(input_name='table-name')


json_bytes = b"""
    { 
      "timestamp": "2020-06-24T15:32:56.775518Z",
      "record-type": "data",
      "operation": "load",
      "partition-key-type": "primary-key",
      "schema-name": "legacy",
      "table-name": "test"
    }
"""

print(MetadataModel.loads(json_bytes, default_serializer='json'))

输出:

<MetadataModel: timestamp='2020-06-24T15:32:56.775518Z', record_type='data', operation='load', partition_key_type='primary-key', schema_name='legacy', table_name='test'>