简体   繁体   English

如何使用 Python 在 Kafka Schema Registry 中以编程方式注册 Avro Schema

[英]How to programatically register Avro Schema in Kafka Schema Registry using Python

I put data and schema to kafka and schema registry with python.我使用 python 将数据和模式放入 kafka 和模式注册表。

from confluent_kafka import avro

from confluent_kafka.avro import AvroProducer



value_schema_str = """

    {

       "type":"record",

       "name":"myrecord",

       "fields":[

          {

             "name":"ID",

             "type":[

                "null",

                "int"

             ],

             "default":null

          },

          {

             "name":"PRODUCT",

             "type":[

                "null",

                "string"

             ],

             "default":null

          },

          {

             "name":"QUANTITY",

             "type":[

                "null",

                "int"

             ],

             "default":null

          },

          {

             "name":"PRICE",

             "type":[

                "null",

                "int"

             ],

             "default":null

          }

       ]

    }

    """



key_schema_str = """

    {

       "type":"record",

       "name":"key_schema",

       "fields":[

          {

             "name":"ID",

             "type":"int"

          }

       ]

    }

    """





def delivery_report(err, msg):

    """ Called once for each message produced to indicate delivery result.

        Triggered by poll() or flush(). """

    if err is not None:

        print('Message delivery failed: {}'.format(err))

    else:

        print('Message delivered to {} [{}]'.format(msg.topic(), msg.partition()))





if __name__ == '__main__':

    value_schema = avro.loads(value_schema_str)

    key_schema = avro.loads(key_schema_str)

    value = {"ID": 199 , "PRODUCT":"Yagiz Gulbahar", "QUANTITY":1453,"PRICE":61}

    key = {"ID": 199}





    avroProducer = AvroProducer({

        'bootstrap.servers': '10.0.0.0:9092',

        'on_delivery': delivery_report,

        'schema.registry.url': 'http://10.0.0.0:8081'

    }, default_key_schema=key_schema, default_value_schema=value_schema)



    avroProducer.produce(topic='ersin_test_2', key=key, value=value)

    avroProducer.flush()

Can I put just schema without data?我可以只放置没有数据的架构吗?

thanks in advance提前致谢

You can also register a schema programatically using confluent-kafka-python or python-schema-registry-client .您还可以使用confluent-kafka-pythonpython-schema-registry-client以编程方式注册模式。


Using confluent-kafka-python使用confluent-kafka-python

from confluent_kafka.avro import SchemaRegistryClient, Schema


schema_str = """
    {
        "type": "record",
        "name": "user",
        "fields": [
            {"name": "firstName", "type": "string"},
            {"name": "age",  "type": "int"}
        ]
    }
"""

avro_schema = Schema(schema_str, 'AVRO')
sr = SchemaRegistryClient("http://schema-registry-host:8081")    
_schema_id = client.register_schema("yourSubjectName", avro_schema)

Using python-schema-registry-client使用python-schema-registry-client

from schema_registry.client import SchemaRegistryClient, schema


schema_ = schema.AvroSchema({
    "type": "record",
    "name": "user",
    "fields": [
        {"name": "firstName", "type": "string"},
        {"name": "age",  "type": "int"}
    ]
})


my_schema = sr.register("yourSubjectName", schema_)

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

相关问题 Python 使用 AVRO 的 Kafka 消费者消息反序列化,没有模式注册表 - 问题 - Python Kafka consumer message deserialisation using AVRO, without schema registry - problem 从kafka使用者读取时,如何从用于avro记录的模式注册表中查找模式ID - How to find the schema id from schema registry used for avro records, when reading from kafka consumer 如何使用 python 发布 kafka 模式 - How to post a kafka schema using python 如何使用 python 从具有模式 ID 和版本的融合模式注册表中获取模式 - How to get schema from confluent schema registry with schema id and version using python Confluent-Kafka:Python使用者中使用模式处理的Avro序列化混淆 - Confluent-Kafka: Avro Serialization Confusions with Schema Handling in Python Consumers Python double 使用 avro 模式失去精度 - Python double loses precision using avro schema 使用 Python 将 XSD(XML Schema)转换为 AVSC(Avro Schema) - Convert XSD (XML Schema) to AVSC (Avro Schema) using Python 如何在python中提取avro文件的模式 - How to extract schema for avro file in python Python Avro,如何将数据写入修改后的模式? - Python Avro, how to write data to a modified schema? 无法在 python 中使用 register() 注册模式 - Unable to register schema using register() in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM