gpt4 book ai didi

python - python kafka 库的编码/格式问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:47 25 4
gpt4 key购买 nike

我一直在尝试使用 python kafka现在暂时无法使用库,无法让制作人工作。

经过一番研究,我发现 kafka 向消费者发送(我猜也期望)一个额外的 5 字节 header (一个 0 字节,一个 long 包含模式注册表的模式 id)。我已经成功地通过简单地剥离第一个字节来让消费者工作。

在编写生产者时我应该在前面添加类似的 header 吗?

下面出现的异常:

    [2016-09-14 13:32:48,684] ERROR Task hdfs-sink-0 threw an uncaught and unrecoverable exception (org.apache.kafka.connect.runtime.WorkerTask:142)
org.apache.kafka.connect.errors.DataException: Failed to deserialize data to Avro:
at io.confluent.connect.avro.AvroConverter.toConnectData(AvroConverter.java:109)
at org.apache.kafka.connect.runtime.WorkerSinkTask.convertMessages(WorkerSinkTask.java:357)
at org.apache.kafka.connect.runtime.WorkerSinkTask.poll(WorkerSinkTask.java:226)
at org.apache.kafka.connect.runtime.WorkerSinkTask.iteration(WorkerSinkTask.java:170)
at org.apache.kafka.connect.runtime.WorkerSinkTask.execute(WorkerSinkTask.java:142)
at org.apache.kafka.connect.runtime.WorkerTask.doRun(WorkerTask.java:140)
at org.apache.kafka.connect.runtime.WorkerTask.run(WorkerTask.java:175)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.kafka.common.errors.SerializationException: Error deserializing Avro message for id -1
Caused by: org.apache.kafka.common.errors.SerializationException: Unknown magic byte!

我正在使用 kafka 和 python-kafka 的最新稳定版本。

编辑

消费者

from kafka import KafkaConsumer
import avro.io
import avro.schema
import io
import requests
import struct

# To consume messages
consumer = KafkaConsumer('hadoop_00',
group_id='my_group',
bootstrap_servers=['hadoop-master:9092'])

schema_path = "resources/f1.avsc"
for msg in consumer:
value = bytearray(msg.value)
schema_id = struct.unpack(">L", value[1:5])[0]
response = requests.get("http://hadoop-master:8081/schemas/ids/" + str(schema_id))
schema = response.json()["schema"]
schema = avro.schema.parse(schema)
bytes_reader = io.BytesIO(value[5:])
# bytes_reader = io.BytesIO(msg.value)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
temp = reader.read(decoder)
print(temp)

制作人

from kafka import KafkaProducer
import avro.schema
import io
from avro.io import DatumWriter

producer = KafkaProducer(bootstrap_servers="hadoop-master")

# Kafka topic
topic = "hadoop_00"

# Path to user.avsc avro schema
schema_path = "resources/f1.avsc"
schema = avro.schema.parse(open(schema_path).read())
range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range:
producer.send(topic, b'{"f1":"value_' + str(i))

最佳答案

我可以让我的 python 生产者使用 Schema-Registry 向 Kafka-Connect 发送消息:

...
import avro.datafile
import avro.io
import avro.schema
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='kafka:9092')
with open('schema.avsc') as f:
schema = avro.schema.Parse(f.read())

def post_message():
bytes_writer = io.BytesIO()
# Write the Confluent "Magic Byte"
bytes_writer.write(bytes([0]))
# Should get or create the schema version with Schema-Registry
...
schema_version = 1
bytes_writer.write(
int.to_bytes(schema_version, 4, byteorder='big'))

# and then the standard Avro bytes serialization
writer = avro.io.DatumWriter(schema)
encoder = avro.io.BinaryEncoder(bytes_writer)
writer.write({'key': 'value'}, encoder)
producer.send('topic', value=bytes_writer.getvalue())

有关“Magic Byte”的文档: https://github.com/confluentinc/schema-registry/blob/4.0.x/docs/serializer-formatter.rst

关于python - python kafka 库的编码/格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com