Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从kafka consumer读取时,如何从用于avro记录的模式注册表中查找模式id_Python_Apache Kafka_Kafka Consumer Api_Confluent Schema Registry_Kafka Python - Fatal编程技术网

Python 从kafka consumer读取时,如何从用于avro记录的模式注册表中查找模式id

Python 从kafka consumer读取时,如何从用于avro记录的模式注册表中查找模式id,python,apache-kafka,kafka-consumer-api,confluent-schema-registry,kafka-python,Python,Apache Kafka,Kafka Consumer Api,Confluent Schema Registry,Kafka Python,我们使用schema注册表来存储模式,消息被序列化到avro并推送到kafka主题 想知道,当从消费者读取数据时,如何找到模式id,avro记录是为其序列化的。我们需要这个模式id,以跟踪是否将新列添加到表中的更改。若添加或删除了新列,将在schema registry中生成一个新的schema id,以及如何在consumer中获取该id consumer = KafkaConsumer(bootstrap_servers = conf['BOOTSTRAP_SERVERS'],

我们使用schema注册表来存储模式,消息被序列化到avro并推送到kafka主题

想知道,当从消费者读取数据时,如何找到模式id,avro记录是为其序列化的。我们需要这个模式id,以跟踪是否将新列添加到表中的更改。若添加或删除了新列,将在schema registry中生成一个新的schema id,以及如何在consumer中获取该id

consumer = KafkaConsumer(bootstrap_servers = conf['BOOTSTRAP_SERVERS'],
                        auto_offset_reset = conf['AUTO_OFFSET'],
                        enable_auto_commit = conf['AUTO_COMMIT'],
                        auto_commit_interval_ms = conf['AUTO_COMMIT_INTERVAL']
                        )
consumer.subscribe(conf['KAFKA_TOPICS'])

for message in consumer:
    print(message.key)
从上面的代码中,message.key打印该特定记录的密钥,我们如何找到消费者用于反序列化该记录的对应模式id

curl -X GET http://localhost:8081/subjects/helpkit_internal.helpkit_support.agents-value/versions/2

{"subject":"helpkit_internal.helpkit_support.agents-value","version":2,"id":33,"schema":"{\"type\":\"record\",\"name\":\"Envelope\",\"namespace\":\"helpkit_internal.helpkit_support.agents\",\"fields\":[{\"name\":\"before\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"Value\",\"fields\":[{\"name\":\"id\",\"type\":\"long\"},{\"name\":\"user_id\"
在这里,我们希望从消费者那里获得id值
“id”:33


请对此提出建议

实际上,您可以做的是获取给定主题的最新架构id:

使用


使用

按主题名称获取架构


谢谢你的回复。但在我的场景中,获取最新版本并没有帮助。我们正在进行实时摄取,你能帮我实现下面的一个吗,比如说,卡夫卡制作人正在为一个表运行,表在3000条记录之后发生了变化,基本上在3000条记录之后的源代码中添加了一个新列。我的卡夫卡制作人将推送3000条模式id为1的记录,并从3001条记录推送模式id为2的记录。在我的消费者中,我想从第一条记录开始消费,在这种情况下,我不能获取最新版本的架构id,因为3000条记录之后添加的列中不会有数据。@HemanthKumar您的架构的兼容模式是什么?兼容模式是向前的。因为会经常增加新的列。
from confluent_kafka.avro.cached_schema_registry_client import CachedSchemaRegistryClient

sr = CachedSchemaRegistryClient({
    'url': 'http://localhost:8081',
    'ssl.certificate.location': '/path/to/cert',  # optional
    'ssl.key.location': '/path/to/key'  # optional
})

value_schema = sr.get_latest_schema("helpkit_internal.helpkit_support.agents-value")[1]
key_schema= sr.get_latest_schema("helpkit_internal.helpkit_support.agents-key")[1]
from schema_registry.client import SchemaRegistryClient


sr = SchemaRegistryClient('localhost:8081')
my_schema = sr.get_schema(subject='shelpkit_internal.helpkit_support.agents-value', version='latest')