TypeError:类型为'mappingproxy'的对象不是JSON可序列化的

时间:2019-07-11 18:56:36

标签: python apache-kafka avro confluent-schema-registry confluent-kafka

我尝试使用schema-registry使用confluent-kafka-python的AvroProducer发布avro消息。但是代码无法序列化枚举类型。下面是代码和错误跟踪。非常感谢您的帮助。

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer
from example_schema.schema_classes import SCHEMA as value_schema
from example_schema.com.acme import *
import json

def function():
    avroProducer = AvroProducer({ 'bootstrap.servers': 'localhost:9092', 'schema.registry.url': 'http://localhost:8081' },  default_value_schema=value_schema)
    print(avroProducer)
    obj = Test()
    obj.name = 'vinay'
    obj.age = 11
    obj.sex = 'm'
    obj.myenum = Suit.CLUBS
    print(str(obj))
    avroProducer.produce(topic='test_topic',value=json.dumps(obj))
    avroProducer.flush()

function()

  File "main.py", line 16, in function
    avroProducer.produce(topic='test_topic',value=json.dumps(obj))
  File "/home/priv/anaconda3/lib/python3.6/site-packages/confluent_kafka/avro/__init__.py", line 80, in produce
    value = self._serializer.encode_record_with_schema(topic, value_schema, value)
  File "/home/priv/anaconda3/lib/python3.6/site-packages/confluent_kafka/avro/serializer/message_serializer.py", line 105, in encode_record_with_schema
    schema_id = self.registry_client.register(subject, schema)
  File "/home/priv/anaconda3/lib/python3.6/site-packages/confluent_kafka/avro/cached_schema_registry_client.py", line 216, in register
    body = {'schema': json.dumps(avro_schema.to_json())}
  File "/home/priv/anaconda3/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/home/priv/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/priv/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/priv/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'mappingproxy' is not JSON serializable

Avro架构-

{
    "type": "record",
    "name": "Test",
    "namespace": "com.acme",
    "fields": [{
            "name": "name",
            "type": "string"
        }, {
            "name": "age",
            "type": "int"
        }, {
            "name": "sex",
            "type": "string"
        }, {
            "name": "myenum",
            "type": ["null", {
                    "type": "enum",
                    "name": "Suit",
                    "symbols": ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
                }
            ]
        }
    ]
}

3 个答案:

答案 0 :(得分:0)

由于您正在使用AvroProducer,因此请勿json.dumps

如果查看生产者的示例,则发送的对象是字典,而不是JSON字符串

https://github.com/confluentinc/confluent-kafka-python/blob/master/examples/avro-cli.py

答案 1 :(得分:0)

按照此link,Confluent-Kafka python API与avro-python3 1.9.0兼容。对我有用的解决方案是将avro-python3 API从1.9.0降级到1.8.2。

答案 2 :(得分:-1)

如果您没有将avro-python3 API从1.9.0降级到1.8.2的选项,则可能必须放弃enum类型,而将其替换为string

"type": ["null", {
                "type": "enum",
                "name": "Suit",
                "symbols": ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
            }
        ]

更改为

"type": ["null", "string"]